constraint_theory_core/lib.rs
1//! Constraint Theory Core - High-Performance Geometric Engine
2//!
3//! This crate provides the core mathematical operations for the SuperInstance
4//! Constraint Theory system, including:
5//! - Pythagorean snapping (Phi-Folding Operator)
6//! - Ricci flow evolution
7//! - Holonomy transport
8//! - Sheaf cohomology
9//! - Rigidity percolation
10//!
11//! # Performance
12//!
13//! - Target: <100ns per tile operation
14//! - SIMD: AVX2/AVX-512 for x86_64, NEON for ARM
15//! - Memory: Zero-allocation hot paths
16//!
17//! # Example
18//!
19//! ```
20//! use constraint_theory_core::{PythagoreanManifold, snap};
21//!
22//! let manifold = PythagoreanManifold::new(200);
23//! let vec = [0.6f32, 0.8];
24//! let (snapped, noise) = snap(&manifold, vec);
25//! assert!(noise < 0.01);
26//! ```
27//!
28//! # SIMD Batch Processing
29//!
30//! For high-throughput applications, use SIMD batch processing:
31//!
32//! ```
33//! use constraint_theory_core::PythagoreanManifold;
34//!
35//! let manifold = PythagoreanManifold::new(200);
36//! let vectors = vec![[0.6, 0.8], [0.8, 0.6], [0.1, 0.99]];
37//! let results = manifold.snap_batch_simd(&vectors);
38//!
39//! for (snapped, noise) in results {
40//! println!("Snapped: {:?}, Noise: {}", snapped, noise);
41//! }
42//! ```
43
44#![deny(missing_docs)]
45#![warn(unused_extern_crates)]
46
47pub mod cohomology;
48pub mod curvature;
49pub mod gauge;
50pub mod kdtree;
51pub mod manifold;
52pub mod percolation;
53pub mod simd;
54pub mod tile;
55
56#[cfg(test)]
57mod edge_case_tests;
58
59// Re-export key types
60pub use curvature::{ricci_flow_step, RicciFlow};
61pub use manifold::{snap, PythagoreanManifold, PythagoreanTriple};
62pub use percolation::{FastPercolation, RigidityResult};
63pub use tile::{ConstraintBlock, Origin, Tile};
64
65/// Version information
66pub const VERSION: &str = env!("CARGO_PKG_VERSION");
67
68/// Core error type
69#[derive(Debug, Clone, Copy)]
70pub enum CTErr {
71 /// Invalid input dimension
72 InvalidDimension,
73 /// Manifold not initialized
74 ManifoldEmpty,
75 /// Numerical instability detected
76 NumericalInstability,
77}
78
79impl std::fmt::Display for CTErr {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 match self {
82 Self::InvalidDimension => write!(f, "Invalid input dimension"),
83 Self::ManifoldEmpty => write!(f, "Manifold is empty"),
84 Self::NumericalInstability => write!(f, "Numerical instability detected"),
85 }
86 }
87}
88
89impl std::error::Error for CTErr {}
90
91/// Result type for constraint theory operations
92pub type CTResult<T> = Result<T, CTErr>;
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_snap_accuracy() {
100 let manifold = PythagoreanManifold::new(200);
101
102 // Test 3-4-5 triple
103 let vec = [0.6f32, 0.8];
104 let (snapped, noise) = snap(&manifold, vec);
105
106 assert!(noise < 0.001, "Noise should be near zero for exact triple");
107 assert!((snapped[0] - 0.6).abs() < 0.01);
108 assert!((snapped[1] - 0.8).abs() < 0.01);
109 }
110
111 #[test]
112 fn test_version() {
113 assert!(!VERSION.is_empty());
114 }
115}