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 information_geometry;
97pub mod optimal_transport;
98pub mod product_manifold;
99pub mod spherical;
100pub mod utils;
101
102// New theoretical CS modules
103pub mod homology;
104pub mod optimization;
105pub mod spectral;
106pub mod tensor_networks;
107pub mod tropical;
108
109// Re-exports for convenience - Core
110pub use error::{MathError, Result};
111pub use information_geometry::{FisherInformation, KFACApproximation, NaturalGradient};
112pub use optimal_transport::{
113 GromovWasserstein, SinkhornSolver, SlicedWasserstein, TransportPlan, WassersteinConfig,
114};
115pub use product_manifold::{CurvatureType, ProductManifold, ProductManifoldConfig};
116pub use spherical::{SphericalConfig, SphericalSpace};
117
118// Re-exports - Tropical Algebra
119pub use tropical::{LinearRegionCounter, TropicalNeuralAnalysis};
120pub use tropical::{Tropical, TropicalMatrix, TropicalPolynomial, TropicalSemiring};
121
122// Re-exports - Tensor Networks
123pub use tensor_networks::{CPConfig, CPDecomposition, TuckerConfig, TuckerDecomposition};
124pub use tensor_networks::{DenseTensor, TensorTrain, TensorTrainConfig};
125pub use tensor_networks::{TensorNetwork, TensorNode};
126
127// Re-exports - Spectral Methods
128pub use spectral::ScaledLaplacian;
129pub use spectral::{ChebyshevExpansion, ChebyshevPolynomial};
130pub use spectral::{FilterType, GraphFilter, SpectralFilter};
131pub use spectral::{GraphWavelet, SpectralClustering, SpectralWaveletTransform};
132
133// Re-exports - Homology
134pub use homology::{BirthDeathPair, PersistenceDiagram, PersistentHomology};
135pub use homology::{BottleneckDistance, WassersteinDistance as HomologyWasserstein};
136pub use homology::{Filtration, Simplex, SimplicialComplex, VietorisRips};
137
138// Re-exports - Optimization
139pub use optimization::{BoundsCertificate, NonnegativityCertificate};
140pub use optimization::{Monomial, Polynomial, Term};
141pub use optimization::{SOSDecomposition, SOSResult};
142
143/// Prelude module for convenient imports
144pub mod prelude {
145 pub use crate::error::*;
146 pub use crate::homology::*;
147 pub use crate::information_geometry::*;
148 pub use crate::optimal_transport::*;
149 pub use crate::optimization::*;
150 pub use crate::product_manifold::*;
151 pub use crate::spectral::*;
152 pub use crate::spherical::*;
153 pub use crate::tensor_networks::*;
154 pub use crate::tropical::*;
155}
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160
161 #[test]
162 fn test_crate_version() {
163 let version = env!("CARGO_PKG_VERSION");
164 assert!(!version.is_empty());
165 }
166}