ruvector_math/optimal_transport/
mod.rs

1//! Optimal Transport Algorithms
2//!
3//! This module provides implementations of optimal transport distances and solvers:
4//!
5//! - **Sliced Wasserstein Distance**: O(n log n) via random 1D projections
6//! - **Sinkhorn Algorithm**: Log-stabilized entropic regularization
7//! - **Gromov-Wasserstein**: Cross-space structure comparison
8//!
9//! ## Theory
10//!
11//! Optimal transport measures the minimum "cost" to transform one probability
12//! distribution into another. The Wasserstein distance (Earth Mover's Distance)
13//! is defined as:
14//!
15//! W_p(μ, ν) = (inf_{γ ∈ Π(μ,ν)} ∫∫ c(x,y)^p dγ(x,y))^{1/p}
16//!
17//! where Π(μ,ν) is the set of all couplings with marginals μ and ν.
18//!
19//! ## Use Cases in Vector Search
20//!
21//! - Cross-lingual document retrieval (comparing embedding distributions)
22//! - Image region matching (comparing feature distributions)
23//! - Time series pattern matching
24//! - Document similarity via word embedding distributions
25
26mod sliced_wasserstein;
27mod sinkhorn;
28mod gromov_wasserstein;
29mod config;
30
31pub use sliced_wasserstein::SlicedWasserstein;
32pub use sinkhorn::{SinkhornSolver, TransportPlan};
33pub use gromov_wasserstein::GromovWasserstein;
34pub use config::WassersteinConfig;
35
36/// Trait for optimal transport distance computations
37pub trait OptimalTransport {
38    /// Compute the optimal transport distance between two point clouds
39    fn distance(&self, source: &[Vec<f64>], target: &[Vec<f64>]) -> f64;
40
41    /// Compute the optimal transport distance with weights
42    fn weighted_distance(
43        &self,
44        source: &[Vec<f64>],
45        source_weights: &[f64],
46        target: &[Vec<f64>],
47        target_weights: &[f64],
48    ) -> f64;
49}