constraint-theory-core 2.2.1

Deterministic manifold snapping with O(log n) KD-tree indexing — maps continuous vectors to exact Pythagorean coordinates
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Constraint Theory Core - High-Performance Geometric Engine
//!
//! This crate provides the core mathematical operations for the SuperInstance
//! Constraint Theory system, implementing the Grand Unified Constraint Theory (GUCT).
//!
//! # Modules
//!
//! | Module | Description |
//! |--------|-------------|
//! | [`manifold`] | Pythagorean snapping with O(log N) KD-tree lookup |
//! | [`hidden_dimensions`] | Exact encoding via k = ⌈log₂(1/ε)⌉ formula |
//! | [`quantizer`] | Constraint-preserving quantization (TurboQuant, BitNet, PolarQuant) |
//! | [`holonomy`] | Consistency verification around cycles |
//! | [`cache`] | Thread-safe lattice caching for performance |
//! | [`kdtree`] | Spatial indexing for fast nearest neighbor queries |
//! | [`simd`] | SIMD-optimized batch processing (AVX2) |
//! | [`csp`] | Constraint satisfaction engine (Variable, Constraint, ConstraintProblem) |
//! | [`ac3`] | AC-3 arc consistency algorithm |
//! | [`backtracking`] | Backtracking solvers (MRV, LCV, FC, MAC) |
//! | [`cdcl`] | Conflict-Driven Clause Learning (1-UIP) |
//! | [`puzzle`] | Built-in puzzles (N-Queens, Sudoku 4x4, graph coloring) |
//! | [`sudoku`] | 9x9 Sudoku solver with AC-3 + MRV + FC pipeline |
//!
//! # Core Concepts
//!
//! ## Pythagorean Manifold
//!
//! The `PythagoreanManifold` is the primary data structure, representing a discrete
//! set of exact Pythagorean coordinates on the unit circle. It enables deterministic
//! projection of continuous vectors to exact rational ratios.
//!
//! ## Hidden Dimensions Formula
//!
//! The number of hidden dimensions for precision ε:
//! ```text
//! k = ⌈log₂(1/ε)⌉
//! ```
//!
//! This formula determines the additional dimensions needed to represent
//! constraints exactly without floating-point errors.
//!
//! ## Quantization Modes
//!
//! The `PythagoreanQuantizer` supports multiple quantization modes:
//! - **Ternary (BitNet)**: {-1, 0, 1} for LLM weights, 16x memory reduction
//! - **Polar (PolarQuant)**: Exact unit norm preservation for embeddings
//! - **Turbo (TurboQuant)**: Near-optimal distortion for vector databases
//! - **Hybrid**: Auto-select mode based on input characteristics
//!
//! # Performance
//!
//! | Operation | Complexity | Notes |
//! |-----------|------------|-------|
//! | Single snap | O(log N) | KD-tree lookup |
//! | Batch snap | O(n log N) | SIMD optimized |
//! | Holonomy check | O(n²) | Spectral method |
//! | Lattice cache | O(1) | Thread-safe |
//!
//! # Example
//!
//! ```
//! use constraint_theory_core::{PythagoreanManifold, snap};
//!
//! let manifold = PythagoreanManifold::new(200);
//! let vec = [0.6f32, 0.8];
//! let (snapped, noise) = snap(&manifold, vec);
//! assert!(noise < 0.01);
//! ```
//!
//! # Hidden Dimensions Example
//!
//! ```
//! use constraint_theory_core::hidden_dimensions::{hidden_dim_count, lift_to_hidden};
//!
//! // Compute hidden dimensions for precision 1e-10
//! let k = hidden_dim_count(1e-10);
//! assert_eq!(k, 34);
//!
//! // Lift a point to higher dimensions
//! let point = vec![0.6, 0.8];
//! let lifted = lift_to_hidden(&point, k);
//! assert_eq!(lifted.len(), 36); // 2 visible + 34 hidden
//! ```
//!
//! # Quantization Example
//!
//! ```
//! use constraint_theory_core::quantizer::{PythagoreanQuantizer, QuantizationMode};
//!
//! // Create a quantizer for embeddings (unit norm preservation)
//! let quantizer = PythagoreanQuantizer::for_embeddings();
//! let vector = vec![0.6, 0.8, 0.0, 0.0];
//! let result = quantizer.quantize(&vector);
//!
//! // Verify unit norm is preserved
//! assert!(result.check_unit_norm(0.1));
//! ```
//!
//! # SIMD Batch Processing
//!
//! For high-throughput applications, use SIMD batch processing:
//!
//! ```
//! use constraint_theory_core::PythagoreanManifold;
//!
//! let manifold = PythagoreanManifold::new(200);
//! let vectors = vec![[0.6, 0.8], [0.8, 0.6], [0.1, 0.99]];
//! let results = manifold.snap_batch_simd(&vectors);
//!
//! for (snapped, noise) in results {
//!     println!("Snapped: {:?}, Noise: {}", snapped, noise);
//! }
//! ```
//!
//! # Error Handling
//!
//! For consensus-critical applications, validate inputs before snapping:
//!
//! ```
//! use constraint_theory_core::PythagoreanManifold;
//!
//! let manifold = PythagoreanManifold::new(200);
//!
//! // Validate input for consensus-critical code
//! if let Err(reason) = manifold.validate_input([f32::NAN, 0.0]) {
//!     println!("Invalid input: {}", reason);
//! }
//! ```
//!
//! # Feature Flags
//!
//! - `simd`: Enable SIMD optimizations (enabled automatically on supported platforms)

#![deny(missing_docs)]
#![warn(unused_extern_crates)]
#![warn(clippy::all)]

pub mod ac3;
pub mod backtracking;
pub mod cache;
pub mod cdcl;
pub mod cohomology;
pub mod csp;
pub mod curvature;
pub mod gauge;
pub mod hidden_dimensions;
pub mod holonomy;
pub mod kdtree;
pub mod manifold;
pub mod percolation;
pub mod puzzle;
pub mod quantizer;
pub mod simd;
pub mod sudoku;
pub mod tile;

#[cfg(test)]
mod edge_case_tests;

// Re-export key types
pub use cache::{clear_global_cache, global_cache, CachedLattice, LatticeCache};
pub use curvature::{ricci_flow_step, RicciFlow};
pub use hidden_dimensions::{
    hidden_dim_count, holographic_accuracy, lift_to_hidden, precision_from_hidden_dims,
    project_to_visible, HiddenDimensionConfig,
};
pub use holonomy::{compute_holonomy, verify_holonomy, HolonomyChecker, HolonomyResult};
pub use manifold::{snap, PythagoreanManifold, PythagoreanTriple};
pub use percolation::{FastPercolation, RigidityResult};
pub use quantizer::{PythagoreanQuantizer, QuantizationMode, QuantizationResult, Rational};
pub use tile::{ConstraintBlock, Origin, Tile};

/// Core error type for constraint theory operations
///
/// This enum represents all possible errors that can occur during
/// constraint theory operations. All fallible operations return `CTResult<T>`.
///
/// # Error Categories
///
/// - **Input Validation**: `NaNInput`, `InfinityInput`, `ZeroVector`, `InvalidDimension`
/// - **State Errors**: `ManifoldEmpty`, `BufferSizeMismatch`
/// - **Numerical**: `NumericalInstability`, `Overflow`, `DivisionByZero`
///
/// # Example
///
/// ```
/// use constraint_theory_core::{CTErr, CTResult};
///
/// fn process_input(x: f32, y: f32) -> CTResult<([f32; 2], f32)> {
///     if !x.is_finite() || !y.is_finite() {
///         return Err(CTErr::InvalidDimension);
///     }
///     Ok(([x, y], 0.0))
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CTErr {
    /// Invalid input dimension - expected 2D vector
    InvalidDimension,
    /// Manifold not initialized - call `new()` first
    ManifoldEmpty,
    /// Numerical instability detected - input may contain NaN or Infinity
    NumericalInstability,
    /// Input vector is zero length - cannot normalize
    ZeroVector,
    /// Input contains NaN values
    NaNInput,
    /// Input contains Infinity values
    InfinityInput,
    /// Batch size mismatch - input and output buffers have different lengths
    BufferSizeMismatch,
    /// Numerical overflow detected - value exceeds f32::MAX
    Overflow,
    /// Division by zero attempted
    DivisionByZero,
    /// Invalid density parameter - must be positive
    InvalidDensity,
    /// Threshold out of valid range
    InvalidThreshold,
}

impl std::fmt::Display for CTErr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidDimension => write!(f, "Invalid input dimension - expected 2D vector. Provide input as [x, y] where both are finite numbers."),
            Self::ManifoldEmpty => write!(f, "Manifold is empty - initialize with new() before performing operations."),
            Self::NumericalInstability => write!(f, "Numerical instability detected - input values may cause precision loss. Consider normalizing input vectors."),
            Self::ZeroVector => write!(f, "Input vector is zero length - cannot normalize. Provide a non-zero vector [x, y]."),
            Self::NaNInput => write!(f, "Input contains NaN values. Ensure all input values are valid numbers."),
            Self::InfinityInput => write!(f, "Input contains Infinity values. Ensure all input values are finite."),
            Self::BufferSizeMismatch => write!(f, "Input and output buffers have different lengths. Ensure buffers are pre-allocated with matching sizes."),
            Self::Overflow => write!(f, "Numerical overflow detected - computed value exceeds f32::MAX. Consider scaling down input values."),
            Self::DivisionByZero => write!(f, "Division by zero attempted - this is an internal error. Please report this issue."),
            Self::InvalidDensity => write!(f, "Invalid density parameter - must be a positive integer. Recommended range: 50-500."),
            Self::InvalidThreshold => write!(f, "Invalid threshold - must be between 0.0 and 1.0 inclusive."),
        }
    }
}

impl std::error::Error for CTErr {}

/// Result type for constraint theory operations
///
/// This is a type alias for `Result<T, CTErr>` used throughout the library.
///
/// # Example
///
/// ```
/// use constraint_theory_core::{CTResult, CTErr};
///
/// fn fallible_operation() -> CTResult<f32> {
///     Ok(1.0)
/// }
/// ```
pub type CTResult<T> = Result<T, CTErr>;

/// Version information string
///
/// Matches the crate version from Cargo.toml.
///
/// # Example
///
/// ```
/// use constraint_theory_core::VERSION;
/// println!("Constraint Theory Core v{}", VERSION);
/// ```
/// Crate version string
/// Crate version string
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Crate version as semver components
/// Major version component
/// Major version component
pub const VERSION_MAJOR: usize = 2;
/// Minor version component
/// Minor version component
pub const VERSION_MINOR: usize = 2;
/// Patch version component
/// Patch version component
pub const VERSION_PATCH: usize = 1;

/// Hidden dimensions required for target precision
///
/// Computes k = ⌈log₂(1/ε)⌉
///
/// # Arguments
///
/// * `epsilon` - Target precision (must be > 0)
///
/// # Returns
///
/// Number of hidden dimensions needed
///
/// # Example
///
/// ```
/// use constraint_theory_core::hidden_dimensions;
///
/// let k = hidden_dimensions(0.01);
/// assert_eq!(k, 7);
/// ```
pub fn hidden_dimensions(epsilon: f32) -> usize {
    if epsilon <= 0.0 {
        return usize::MAX;
    }
    (1.0 / epsilon).log2().ceil() as usize
}

/// Compute maximum angular error for a manifold
///
/// # Arguments
///
/// * `state_count` - Number of valid states in the manifold
///
/// # Returns
///
/// Maximum angular deviation in radians
///
/// # Example
///
/// ```
/// use constraint_theory_core::max_angular_error_for_states;
///
/// let error = max_angular_error_for_states(1000);
/// assert!(error < 0.01);  // ~0.36 degrees
/// ```
pub fn max_angular_error_for_states(state_count: usize) -> f32 {
    if state_count == 0 {
        return std::f32::consts::PI;
    }
    std::f32::consts::PI / state_count as f32
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_snap_accuracy() {
        let manifold = PythagoreanManifold::new(200);

        // Test 3-4-5 triple
        let vec = [0.6f32, 0.8];
        let (snapped, noise) = snap(&manifold, vec);

        assert!(noise < 0.001, "Noise should be near zero for exact triple");
        assert!((snapped[0] - 0.6).abs() < 0.01);
        assert!((snapped[1] - 0.8).abs() < 0.01);
    }

    #[test]
    fn test_version() {
        assert!(!VERSION.is_empty());
        assert_eq!(VERSION_MAJOR, 2);
        assert_eq!(VERSION_MINOR, 2);
        assert_eq!(VERSION_PATCH, 1);
    }

    #[test]
    fn test_hidden_dimensions() {
        assert_eq!(hidden_dimensions(0.1), 4);
        assert_eq!(hidden_dimensions(0.01), 7);
        assert_eq!(hidden_dimensions(0.001), 10);
        assert_eq!(hidden_dimensions(0.0001), 14);
    }

    #[test]
    fn test_max_angular_error() {
        let error = max_angular_error_for_states(1000);
        assert!(error > 0.0);
        assert!(error < 0.01); // ~0.36 degrees
    }

    #[test]
    fn test_cterr_display() {
        assert!(!CTErr::InvalidDimension.to_string().is_empty());
        assert!(!CTErr::ManifoldEmpty.to_string().is_empty());
        assert!(!CTErr::NumericalInstability.to_string().is_empty());
        assert!(!CTErr::ZeroVector.to_string().is_empty());
        assert!(!CTErr::NaNInput.to_string().is_empty());
        assert!(!CTErr::InfinityInput.to_string().is_empty());
        assert!(!CTErr::BufferSizeMismatch.to_string().is_empty());
        assert!(!CTErr::Overflow.to_string().is_empty());
        assert!(!CTErr::DivisionByZero.to_string().is_empty());
        assert!(!CTErr::InvalidDensity.to_string().is_empty());
        assert!(!CTErr::InvalidThreshold.to_string().is_empty());
    }

    #[test]
    fn test_cterr_actionable_messages() {
        // Verify error messages contain actionable guidance
        let zero_msg = CTErr::ZeroVector.to_string();
        assert!(
            zero_msg.contains("Provide"),
            "Error message should suggest action"
        );

        let nan_msg = CTErr::NaNInput.to_string();
        assert!(
            nan_msg.contains("Ensure"),
            "Error message should suggest action"
        );

        let density_msg = CTErr::InvalidDensity.to_string();
        assert!(
            density_msg.contains("Recommended"),
            "Error message should provide guidance"
        );
    }
}