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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//! Pythagorean Manifold - The Rigidity Matroid
//!
//! This module implements the core Pythagorean snapping operation,
//! which maps continuous vectors to discrete Pythagorean ratios.
//!
//! # Performance Optimization
//!
//! Uses KD-tree for O(log N) nearest neighbor lookup instead of O(N) linear search.
//! This provides 5-10x speedup for single-vector snapping operations.
//! SIMD batch processing is used for multiple vectors.
use crate::kdtree::KDTree;
use crate::simd::snap_batch_simd;
use crate::{CTErr, CTResult};
/// A Pythagorean triple (a, b, c) where a² + b² = c²
///
/// Represents the fundamental geometric constraint that enables
/// deterministic vector snapping in the manifold.
#[derive(Clone, Copy, Debug)]
pub struct PythagoreanTriple {
/// First leg of the triple
pub a: f32,
/// Second leg of the triple
pub b: f32,
/// Hypotenuse of the triple
pub c: f32,
}
impl PythagoreanTriple {
/// Create a new Pythagorean triple
///
/// # Arguments
///
/// * `a` - First leg
/// * `b` - Second leg
/// * `c` - Hypotenuse
///
/// # Example
///
/// ```rust
/// use constraint_theory_core::manifold::PythagoreanTriple;
///
/// let triple = PythagoreanTriple::new(3.0, 4.0, 5.0);
/// assert!(triple.is_valid());
/// ```
pub fn new(a: f32, b: f32, c: f32) -> Self {
Self { a, b, c }
}
/// Check if the triple satisfies a² + b² = c²
///
/// # Returns
///
/// `true` if the triple is valid within numerical precision
///
/// # Example
///
/// ```rust
/// use constraint_theory_core::manifold::PythagoreanTriple;
///
/// let triple = PythagoreanTriple::new(3.0, 4.0, 5.0);
/// assert!(triple.is_valid());
/// ```
pub fn is_valid(&self) -> bool {
(self.a * self.a + self.b * self.b - self.c * self.c).abs() < 1e-6
}
/// Convert triple to normalized 2D vector
///
/// # Returns
///
/// Normalized vector [a/c, b/c]
///
/// # Example
///
/// ```rust
/// use constraint_theory_core::manifold::PythagoreanTriple;
///
/// let triple = PythagoreanTriple::new(3.0, 4.0, 5.0);
/// let vec = triple.to_vector();
/// assert_eq!(vec, [0.6, 0.8]);
/// ```
pub fn to_vector(&self) -> [f32; 2] {
[self.a / self.c, self.b / self.c]
}
}
/// Pythagorean manifold for deterministic vector snapping
///
/// Pre-computes all valid Pythagorean triples up to a density
/// parameter and provides O(log N) snapping via KD-tree lookup.
pub struct PythagoreanManifold {
valid_states: Vec<[f32; 2]>,
/// KD-tree for fast O(log N) nearest neighbor lookup
kdtree: KDTree,
}
impl Clone for PythagoreanManifold {
fn clone(&self) -> Self {
// Rebuild KD-tree from valid states (O(N log N) but acceptable for clone)
let kdtree = KDTree::build(&self.valid_states);
Self {
valid_states: self.valid_states.clone(),
kdtree,
}
}
}
impl PythagoreanManifold {
/// Create a new Pythagorean manifold with specified density
///
/// # Arguments
///
/// * `density` - Maximum value of m in Euclid's formula (controls resolution)
///
/// # Returns
///
/// New manifold with pre-computed valid states
///
/// # Example
///
/// ```rust
/// use constraint_theory_core::manifold::PythagoreanManifold;
///
/// let manifold = PythagoreanManifold::new(50);
/// println!("Generated {} states", manifold.state_count());
/// ```
pub fn new(density: usize) -> Self {
let mut states = Vec::with_capacity(density * 5);
for m in 2..density {
for n in 1..m {
if (m - n) % 2 == 1 && gcd(m, n) == 1 {
let a = (m * m - n * n) as f32;
let b = (2 * m * n) as f32;
let c = (m * m + n * n) as f32;
let v = [a / c, b / c];
states.push(v);
states.push([b / c, a / c]);
states.push([-a / c, b / c]);
states.push([a / c, -b / c]);
states.push([-a / c, -b / c]);
}
}
}
states.push([1.0, 0.0]);
states.push([0.0, 1.0]);
states.push([-1.0, 0.0]);
states.push([0.0, -1.0]);
// Build KD-tree for fast O(log N) nearest neighbor lookup
let kdtree = KDTree::build(&states);
Self {
valid_states: states,
kdtree,
}
}
/// Get the number of valid states in the manifold
///
/// # Returns
///
/// Total count of valid Pythagorean vectors
///
/// # Example
///
/// ```rust
/// use constraint_theory_core::manifold::PythagoreanManifold;
///
/// let manifold = PythagoreanManifold::new(50);
/// println!("Manifold has {} states", manifold.state_count());
/// ```
pub fn state_count(&self) -> usize {
self.valid_states.len()
}
/// Get a reference to the valid states for SIMD operations
pub fn states(&self) -> &[[f32; 2]] {
&self.valid_states
}
/// Snap a vector to the nearest Pythagorean triple
///
/// Uses KD-tree for O(log N) nearest neighbor lookup.
///
/// # Arguments
///
/// * `vector` - Input 2D vector to snap
///
/// # Returns
///
/// Tuple of (snapped_vector, noise) where noise is 1 - resonance
///
/// # Edge Cases
///
/// - Zero vector: Returns ([1.0, 0.0], 0.0)
/// - NaN/Infinity: Returns ([1.0, 0.0], 1.0) as error indicator
pub fn snap(&self, vector: [f32; 2]) -> ([f32; 2], f32) {
// Validate input - handle NaN and Infinity gracefully
if !vector[0].is_finite() || !vector[1].is_finite() {
// Return error indicator: noise=1.0 signals invalid input
return ([1.0, 0.0], 1.0);
}
let norm = (vector[0] * vector[0] + vector[1] * vector[1]).sqrt();
if norm < 1e-10 {
return ([1.0, 0.0], 0.0);
}
let v_in = [vector[0] / norm, vector[1] / norm];
// Use KD-tree for O(log N) nearest neighbor lookup
if let Some((nearest, _idx, _dist_sq)) = self.kdtree.nearest(&v_in) {
// Calculate resonance from dot product
let resonance = nearest[0] * v_in[0] + nearest[1] * v_in[1];
let noise = 1.0 - resonance;
(nearest, noise)
} else {
// Fallback to linear search if KD-tree is empty (shouldn't happen)
let mut max_resonance = f32::MIN;
let mut best_idx = 0;
for (i, state) in self.valid_states.iter().enumerate() {
let resonance = state[0] * v_in[0] + state[1] * v_in[1];
if resonance > max_resonance {
max_resonance = resonance;
best_idx = i;
}
}
let snapped = self.valid_states[best_idx];
let noise = 1.0 - max_resonance;
(snapped, noise)
}
}
/// SIMD-optimized batch snapping
///
/// Processes multiple vectors at once using AVX2 SIMD instructions.
/// Achieves 8-16x speedup over scalar implementation.
///
/// ⚠️ **WARNING**: SIMD path may have platform-dependent behavior for tie-breaking.
/// For consensus-critical code, use `snap_batch()` (scalar) instead.
///
/// # Arguments
///
/// * `vectors` - Input vectors to snap
///
/// # Returns
///
/// Vector of (snapped_vector, noise) tuples
pub fn snap_batch_simd(&self, vectors: &[[f32; 2]]) -> Vec<([f32; 2], f32)> {
let mut results = vec![([0.0, 0.0], 0.0f32); vectors.len()];
snap_batch_simd(&self.valid_states, vectors, &mut results);
results
}
/// SIMD-optimized batch snapping with pre-allocated buffer
///
/// This version avoids allocation by writing into a provided buffer.
/// Use this for maximum performance in hot loops.
///
/// ⚠️ **WARNING**: SIMD path may have platform-dependent behavior.
/// For consensus-critical code, use `snap_batch_into()` instead.
///
/// # Arguments
///
/// * `vectors` - Input vectors to snap
/// * `results` - Output buffer (must have same length as vectors)
pub fn snap_batch_simd_into(&self, vectors: &[[f32; 2]], results: &mut [([f32; 2], f32)]) {
snap_batch_simd(&self.valid_states, vectors, results);
}
/// Scalar batch snapping (fallback for non-SIMD or small batches)
///
/// ✅ **RECOMMENDED** for consensus-critical code.
/// Uses deterministic scalar path with explicit tie-breaking.
///
/// # Arguments
///
/// * `vectors` - Input vectors to snap
/// * `results` - Output buffer (must have same length as vectors)
pub fn snap_batch(&self, vectors: &[[f32; 2]], results: &mut [([f32; 2], f32)]) {
for (i, &vec) in vectors.iter().enumerate() {
results[i] = self.snap(vec);
}
}
/// Validate input before snapping (for consensus-critical systems)
///
/// Returns Ok(()) if input is valid, Err(reason) if input will produce
/// undefined or potentially inconsistent results across platforms.
///
/// # Arguments
///
/// * `vector` - Input 2D vector to validate
///
/// # Returns
///
/// * `Ok(())` - Input is valid
/// * `Err(&'static str)` - Input is invalid with reason
///
/// # Example
///
/// ```rust
/// let manifold = PythagoreanManifold::new(200);
/// let input = [0.5, 0.5];
///
/// if let Err(reason) = manifold.validate_input(input) {
/// // Reject input before consensus
/// return Err(ConsensusError::InvalidInput(reason));
/// }
/// let (snapped, noise) = manifold.snap(input);
/// ```
pub fn validate_input(&self, vector: [f32; 2]) -> Result<(), &'static str> {
if !vector[0].is_finite() || !vector[1].is_finite() {
return Err("Input contains NaN or Infinity");
}
if vector[0] == 0.0 && vector[1] == 0.0 {
return Err("Zero vector - will snap to arbitrary default");
}
Ok(())
}
/// Snap a vector with explicit error handling (for consensus-critical systems)
///
/// Unlike `snap()`, this method returns a `Result` type and will reject
/// invalid inputs rather than returning a fallback value.
///
/// # Arguments
///
/// * `vector` - Input 2D vector to snap
///
/// # Returns
///
/// * `Ok((snapped, noise))` - Successful snap with result
/// * `Err(CTErr::NaNInput)` - Input contains NaN
/// * `Err(CTErr::InfinityInput)` - Input contains Infinity
/// * `Err(CTErr::ZeroVector)` - Input is zero vector
///
/// # Example
///
/// ```rust
/// use constraint_theory_core::PythagoreanManifold;
///
/// let manifold = PythagoreanManifold::new(200);
///
/// // Valid input
/// let result = manifold.snap_checked([0.6, 0.8]);
/// assert!(result.is_ok());
///
/// // Invalid input (NaN)
/// let result = manifold.snap_checked([f32::NAN, 0.5]);
/// assert!(result.is_err());
/// ```
pub fn snap_checked(&self, vector: [f32; 2]) -> CTResult<([f32; 2], f32)> {
// Detailed validation with specific error types
if vector[0].is_nan() || vector[1].is_nan() {
return Err(CTErr::NaNInput);
}
if vector[0].is_infinite() || vector[1].is_infinite() {
return Err(CTErr::InfinityInput);
}
if vector[0] == 0.0 && vector[1] == 0.0 {
return Err(CTErr::ZeroVector);
}
// Perform the snap
Ok(self.snap(vector))
}
/// Batch snap with explicit error handling
///
/// Validates all inputs before processing and returns an error if any
/// input is invalid. For partial success, use `snap_batch_partial`.
///
/// # Arguments
///
/// * `vectors` - Input vectors to snap
/// * `results` - Output buffer (must have same length as vectors)
///
/// # Returns
///
/// * `Ok(())` - All vectors snapped successfully
/// * `Err(CTErr::BufferSizeMismatch)` - Buffer size mismatch
/// * `Err(CTErr::NaNInput)` - One or more inputs contain NaN
/// * `Err(CTErr::InfinityInput)` - One or more inputs contain Infinity
pub fn snap_batch_checked(
&self,
vectors: &[[f32; 2]],
results: &mut [([f32; 2], f32)],
) -> CTResult<()> {
if vectors.len() != results.len() {
return Err(CTErr::BufferSizeMismatch);
}
// Validate all inputs first
for (i, vec) in vectors.iter().enumerate() {
if vec[0].is_nan() || vec[1].is_nan() {
return Err(CTErr::NaNInput);
}
if vec[0].is_infinite() || vec[1].is_infinite() {
return Err(CTErr::InfinityInput);
}
}
// Process all vectors
for (i, &vec) in vectors.iter().enumerate() {
results[i] = self.snap(vec);
}
Ok(())
}
/// Batch snap with partial success reporting
///
/// Processes all valid vectors and reports which ones failed validation.
/// Invalid inputs are snapped to the default ([1.0, 0.0], 0.0) with noise=1.0.
///
/// # Returns
///
/// Vector of (index, error) tuples for inputs that failed validation.
pub fn snap_batch_partial(
&self,
vectors: &[[f32; 2]],
results: &mut [([f32; 2], f32)],
) -> Vec<(usize, CTErr)> {
let mut errors = Vec::new();
if vectors.len() != results.len() {
errors.push((0, CTErr::BufferSizeMismatch));
return errors;
}
for (i, &vec) in vectors.iter().enumerate() {
if vec[0].is_nan() || vec[1].is_nan() {
results[i] = ([1.0, 0.0], 1.0);
errors.push((i, CTErr::NaNInput));
} else if vec[0].is_infinite() || vec[1].is_infinite() {
results[i] = ([1.0, 0.0], 1.0);
errors.push((i, CTErr::InfinityInput));
} else if vec[0] == 0.0 && vec[1] == 0.0 {
results[i] = ([1.0, 0.0], 0.0);
// Zero vector is a soft error - don't report
} else {
results[i] = self.snap(vec);
}
}
errors
}
/// Get maximum angular error for this manifold density
///
/// Returns the worst-case angular deviation from true input direction.
/// For density 200 (~1000 states): approximately 0.36° (0.0063 radians)
///
/// # Formula
///
/// Maximum angular separation ≈ π / state_count
pub fn max_angular_error(&self) -> f32 {
if self.valid_states.is_empty() {
return std::f32::consts::PI;
}
// Conservative estimate: worst case is half the angular spacing
std::f32::consts::PI / self.valid_states.len() as f32
}
/// Get recommended noise threshold for a use case
///
/// Returns suggested maximum noise threshold before rejecting a snap.
///
/// # Arguments
///
/// * `use_case` - "animation", "game", "robotics", "ml", or "consensus"
pub fn recommended_noise_threshold(use_case: &str) -> f32 {
match use_case {
"animation" => 0.02, // Visible snapping above this
"game" => 0.05, // Players may notice above this
"robotics" => 0.01, // Precision tasks need tighter threshold
"ml" => 0.03, // Balance precision and coverage
"consensus" => 0.1, // Accept any valid snap
_ => 0.05,
}
}
}
/// Convenience function for snapping a vector
///
/// # Arguments
///
/// * `manifold` - The Pythagorean manifold to use
/// * `vector` - Input 2D vector to snap
///
/// # Returns
///
/// Tuple of (snapped_vector, noise)
pub fn snap(manifold: &PythagoreanManifold, vector: [f32; 2]) -> ([f32; 2], f32) {
manifold.snap(vector)
}
fn gcd(a: usize, b: usize) -> usize {
if a == b {
return a;
}
if a == 0 {
return b;
}
if b == 0 {
return a;
}
let shift = (a | b).trailing_zeros();
let mut a = a >> a.trailing_zeros();
let mut b = b >> b.trailing_zeros();
while a != b {
if a > b {
a -= b;
a = a >> a.trailing_zeros();
} else {
b -= a;
b = b >> b.trailing_zeros();
}
}
a << shift
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_snap_exact_triple() {
let manifold = PythagoreanManifold::new(200);
let (_snapped, noise) = manifold.snap([0.6, 0.8]);
assert!(noise < 0.001);
}
#[test]
fn test_snap_function() {
let manifold = PythagoreanManifold::new(200);
let (_snapped, noise) = snap(&manifold, [0.6, 0.8]);
assert!(noise < 0.001);
}
#[test]
fn test_snap_batch_simd() {
let manifold = PythagoreanManifold::new(200);
let vectors: Vec<[f32; 2]> = vec![[0.6, 0.8], [0.8, 0.6], [0.1, 0.99], [0.99, 0.1]];
let results = manifold.snap_batch_simd(&vectors);
// Verify results match scalar version
for (i, &vec) in vectors.iter().enumerate() {
let (scalar_snapped, scalar_noise) = manifold.snap(vec);
let (simd_snapped, simd_noise) = results[i];
assert!(
(simd_snapped[0] - scalar_snapped[0]).abs() < 0.001,
"X mismatch at index {}: simd={:?} scalar={:?}",
i,
simd_snapped,
scalar_snapped
);
assert!(
(simd_snapped[1] - scalar_snapped[1]).abs() < 0.001,
"Y mismatch at index {}: simd={:?} scalar={:?}",
i,
simd_snapped,
scalar_snapped
);
assert!(
(simd_noise - scalar_noise).abs() < 0.001,
"Noise mismatch at index {}: simd={} scalar={}",
i,
simd_noise,
scalar_noise
);
}
}
#[test]
fn test_snap_batch_simd_into() {
let manifold = PythagoreanManifold::new(200);
let vectors: Vec<[f32; 2]> = vec![[0.6, 0.8], [0.8, 0.6]];
let mut results = vec![([0.0, 0.0], 0.0f32); vectors.len()];
manifold.snap_batch_simd_into(&vectors, &mut results);
// Verify results match scalar version
for (i, &vec) in vectors.iter().enumerate() {
let (scalar_snapped, scalar_noise) = manifold.snap(vec);
let (simd_snapped, simd_noise) = results[i];
assert!((simd_snapped[0] - scalar_snapped[0]).abs() < 0.001);
assert!((simd_snapped[1] - scalar_snapped[1]).abs() < 0.001);
assert!((simd_noise - scalar_noise).abs() < 0.001);
}
}
#[test]
fn test_kdtree_correctness() {
// Verify KD-tree produces identical results to linear search
let manifold = PythagoreanManifold::new(200);
let test_vectors = vec![
[0.6, 0.8],
[0.8, 0.6],
[0.1, 0.99],
[0.99, 0.1],
[0.707, 0.707], // ~45 degrees
[-0.6, 0.8],
[0.6, -0.8],
[-0.6, -0.8],
];
for vec in test_vectors {
let (_snapped, noise) = manifold.snap(vec);
// Verify the snapped result is a valid state
let norm = (vec[0] * vec[0] + vec[1] * vec[1]).sqrt();
let v_in = [vec[0] / norm, vec[1] / norm];
// Find the true nearest using linear search (fallback code path)
let mut max_resonance = f32::MIN;
for state in manifold.states() {
let resonance = state[0] * v_in[0] + state[1] * v_in[1];
if resonance > max_resonance {
max_resonance = resonance;
}
}
// Noise should match
let expected_noise = 1.0 - max_resonance;
assert!(
(noise - expected_noise).abs() < 0.001,
"Noise mismatch for vector {:?}: KD-tree={} linear={}",
vec,
noise,
expected_noise
);
}
}
#[test]
fn test_manifold_clone() {
// Verify Clone implementation works correctly
let manifold = PythagoreanManifold::new(200);
let cloned = manifold.clone();
// Test that both produce identical results
let test_vec = [0.6, 0.8];
let (orig_snap, orig_noise) = manifold.snap(test_vec);
let (clone_snap, clone_noise) = cloned.snap(test_vec);
assert_eq!(orig_snap, clone_snap);
assert_eq!(orig_noise, clone_noise);
}
#[test]
#[ignore] // Performance test - run with: cargo test --release -- --ignored
fn test_kdtree_performance() {
use std::time::Instant;
let manifold = PythagoreanManifold::new(500);
let iterations = 100_000;
println!("\n=== KD-tree Performance Benchmark ===");
println!("Manifold density: 500");
println!("States: {} valid states", manifold.state_count());
println!("Iterations: {}", iterations);
// Warmup
for _ in 0..1000 {
let _ = manifold.snap([0.6, 0.8]);
}
// Benchmark KD-tree snap
let start = Instant::now();
for _ in 0..iterations {
let _ = manifold.snap([0.6, 0.8]);
}
let duration = start.elapsed();
let per_op_ns = duration.as_nanos() / iterations as u128;
let per_op_us = per_op_ns as f64 / 1000.0;
println!("\nResults:");
println!(" Total time: {:?}", duration);
println!(" Per operation: {} ns ({} μs)", per_op_ns, per_op_us);
println!(
" Operations per second: {:.2}",
1_000_000_000.0 / per_op_ns as f64
);
// Target: < 1000 ns per operation (1 μs)
if per_op_ns < 1000 {
println!("\n✅ PASS: KD-tree meets target (< 1 μs/op)");
} else {
println!("\n❌ FAIL: KD-tree too slow (target: < 1 μs/op)");
}
assert!(
per_op_ns < 1000,
"KD-tree too slow: {} ns/op, target: <1000 ns/op",
per_op_ns
);
}
}