ruvector_math/
lib.rs

1//! # RuVector Math
2//!
3//! Advanced mathematics for next-generation vector search and AI governance, featuring:
4//!
5//! ## Core Modules
6//!
7//! - **Optimal Transport**: Wasserstein distances, Sinkhorn algorithm, Sliced Wasserstein
8//! - **Information Geometry**: Fisher Information, Natural Gradient, K-FAC
9//! - **Product Manifolds**: Mixed-curvature spaces (Euclidean × Hyperbolic × Spherical)
10//! - **Spherical Geometry**: Geodesics on the n-sphere for cyclical patterns
11//!
12//! ## Theoretical CS Modules (New)
13//!
14//! - **Tropical Algebra**: Max-plus semiring for piecewise linear analysis and routing
15//! - **Tensor Networks**: TT/Tucker/CP decomposition for memory compression
16//! - **Spectral Methods**: Chebyshev polynomials for graph diffusion without eigendecomposition
17//! - **Persistent Homology**: TDA for topological drift detection and coherence monitoring
18//! - **Polynomial Optimization**: SOS certificates for provable bounds on attention policies
19//!
20//! ## Design Principles
21//!
22//! 1. **Pure Rust**: No BLAS/LAPACK dependencies for full WASM compatibility
23//! 2. **SIMD-Ready**: Hot paths optimized for auto-vectorization
24//! 3. **Numerically Stable**: Log-domain arithmetic, clamping, and stable softmax
25//! 4. **Modular**: Each component usable independently
26//! 5. **Mincut as Spine**: All modules designed to integrate with mincut governance
27//!
28//! ## Architecture: Mincut as Unifying Signal
29//!
30//! ```text
31//! ┌─────────────────────────────────────────────────────────────┐
32//! │                    Mincut Governance                         │
33//! │  (Structural tension meter for attention graphs)            │
34//! └───────────────────────┬─────────────────────────────────────┘
35//!                         │
36//!     ┌───────────────────┼───────────────────┐
37//!     ▼                   ▼                   ▼
38//! ┌─────────┐       ┌───────────┐       ┌───────────┐
39//! │ Tensor  │       │ Spectral  │       │   TDA     │
40//! │ Networks│       │ Methods   │       │ Homology  │
41//! │ (TT)    │       │(Chebyshev)│       │           │
42//! └─────────┘       └───────────┘       └───────────┘
43//! Compress          Smooth within       Monitor drift
44//! representations   partitions          over time
45//!
46//!     ┌───────────────────┼───────────────────┐
47//!     ▼                   ▼                   ▼
48//! ┌─────────┐       ┌───────────┐       ┌───────────┐
49//! │Tropical │       │    SOS    │       │ Optimal   │
50//! │ Algebra │       │ Certs     │       │ Transport │
51//! └─────────┘       └───────────┘       └───────────┘
52//! Plan safe         Certify policy      Measure
53//! routing paths     constraints         distributional
54//!                                       distances
55//! ```
56//!
57//! ## Quick Start
58//!
59//! ```rust
60//! use ruvector_math::optimal_transport::{SlicedWasserstein, SinkhornSolver, OptimalTransport};
61//! use ruvector_math::information_geometry::FisherInformation;
62//! use ruvector_math::product_manifold::ProductManifold;
63//!
64//! // Sliced Wasserstein distance between point clouds
65//! let sw = SlicedWasserstein::new(100).with_seed(42);
66//! let points_a = vec![vec![0.0, 0.0], vec![1.0, 0.0]];
67//! let points_b = vec![vec![0.5, 0.5], vec![1.5, 0.5]];
68//! let dist = sw.distance(&points_a, &points_b);
69//! assert!(dist > 0.0);
70//!
71//! // Sinkhorn optimal transport
72//! let solver = SinkhornSolver::new(0.1, 100);
73//! let cost_matrix = vec![vec![0.0, 1.0], vec![1.0, 0.0]];
74//! let weights_a = vec![0.5, 0.5];
75//! let weights_b = vec![0.5, 0.5];
76//! let result = solver.solve(&cost_matrix, &weights_a, &weights_b).unwrap();
77//! assert!(result.converged);
78//!
79//! // Product manifold operations (Euclidean only for simplicity)
80//! let manifold = ProductManifold::new(2, 0, 0);
81//! let point_a = vec![0.0, 0.0];
82//! let point_b = vec![3.0, 4.0];
83//! let dist = manifold.distance(&point_a, &point_b).unwrap();
84//! assert!((dist - 5.0).abs() < 1e-10);
85//! ```
86
87#![warn(missing_docs)]
88#![warn(clippy::all)]
89#![cfg_attr(not(feature = "std"), no_std)]
90
91#[cfg(not(feature = "std"))]
92extern crate alloc;
93
94// Core modules
95pub mod error;
96pub mod optimal_transport;
97pub mod information_geometry;
98pub mod spherical;
99pub mod product_manifold;
100pub mod utils;
101
102// New theoretical CS modules
103pub mod tropical;
104pub mod tensor_networks;
105pub mod spectral;
106pub mod homology;
107pub mod optimization;
108
109// Re-exports for convenience - Core
110pub use error::{MathError, Result};
111pub use optimal_transport::{
112    SlicedWasserstein, SinkhornSolver, GromovWasserstein,
113    TransportPlan, WassersteinConfig,
114};
115pub use information_geometry::{
116    FisherInformation, NaturalGradient, KFACApproximation,
117};
118pub use spherical::{SphericalSpace, SphericalConfig};
119pub use product_manifold::{ProductManifold, ProductManifoldConfig, CurvatureType};
120
121// Re-exports - Tropical Algebra
122pub use tropical::{Tropical, TropicalSemiring, TropicalPolynomial, TropicalMatrix};
123pub use tropical::{LinearRegionCounter, TropicalNeuralAnalysis};
124
125// Re-exports - Tensor Networks
126pub use tensor_networks::{DenseTensor, TensorTrain, TensorTrainConfig};
127pub use tensor_networks::{TuckerDecomposition, TuckerConfig, CPDecomposition, CPConfig};
128pub use tensor_networks::{TensorNetwork, TensorNode};
129
130// Re-exports - Spectral Methods
131pub use spectral::{ChebyshevPolynomial, ChebyshevExpansion};
132pub use spectral::{SpectralFilter, GraphFilter, FilterType};
133pub use spectral::{SpectralWaveletTransform, GraphWavelet, SpectralClustering};
134pub use spectral::ScaledLaplacian;
135
136// Re-exports - Homology
137pub use homology::{PersistenceDiagram, PersistentHomology, BirthDeathPair};
138pub use homology::{Simplex, SimplicialComplex, Filtration, VietorisRips};
139pub use homology::{BottleneckDistance, WassersteinDistance as HomologyWasserstein};
140
141// Re-exports - Optimization
142pub use optimization::{Polynomial, Monomial, Term};
143pub use optimization::{SOSDecomposition, SOSResult};
144pub use optimization::{NonnegativityCertificate, BoundsCertificate};
145
146/// Prelude module for convenient imports
147pub mod prelude {
148    pub use crate::optimal_transport::*;
149    pub use crate::information_geometry::*;
150    pub use crate::spherical::*;
151    pub use crate::product_manifold::*;
152    pub use crate::error::*;
153    pub use crate::tropical::*;
154    pub use crate::tensor_networks::*;
155    pub use crate::spectral::*;
156    pub use crate::homology::*;
157    pub use crate::optimization::*;
158}
159
160#[cfg(test)]
161mod tests {
162    use super::*;
163
164    #[test]
165    fn test_crate_version() {
166        let version = env!("CARGO_PKG_VERSION");
167        assert!(!version.is_empty());
168    }
169}