amari 0.19.1

Advanced mathematical computing library with geometric algebra, tropical algebra, and automatic differentiation
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
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
//! Cross-platform determinism tests with golden bit patterns
//!
//! These tests verify that operations produce identical bit patterns
//! across platforms, compilers, and optimization levels.

#![cfg(feature = "deterministic")]

use amari::deterministic::ga2d::{DetRotor2, DetVector2};
use amari::deterministic::DetF32;

/// Golden test vectors with exact bit patterns
///
/// These bit patterns must be identical across all platforms.
/// If these tests fail, determinism is broken.
mod golden_vectors {
    use super::*;

    #[test]
    fn test_scalar_arithmetic_golden() {
        // Test case: 1.5 + 2.5 = 4.0
        let a = DetF32::from_bits(0x3fc00000); // 1.5
        let b = DetF32::from_bits(0x40200000); // 2.5
        let result = a + b;
        assert_eq!(
            result.to_bits(),
            0x40800000,
            "Addition: 1.5 + 2.5 must equal exactly 4.0"
        );

        // Test case: 3.0 * 4.0 = 12.0
        let c = DetF32::from_bits(0x40400000); // 3.0
        let d = DetF32::from_bits(0x40800000); // 4.0
        let result = c * d;
        assert_eq!(
            result.to_bits(),
            0x41400000,
            "Multiplication: 3.0 * 4.0 must equal exactly 12.0"
        );

        // Test case: 10.0 - 3.0 = 7.0
        let e = DetF32::from_bits(0x41200000); // 10.0
        let f = DetF32::from_bits(0x40400000); // 3.0
        let result = e - f;
        assert_eq!(
            result.to_bits(),
            0x40e00000,
            "Subtraction: 10.0 - 3.0 must equal exactly 7.0"
        );

        // Test case: 8.0 / 2.0 = 4.0
        let g = DetF32::from_bits(0x41000000); // 8.0
        let h = DetF32::from_bits(0x40000000); // 2.0
        let result = g / h;
        assert_eq!(
            result.to_bits(),
            0x40800000,
            "Division: 8.0 / 2.0 must equal exactly 4.0"
        );
    }

    #[test]
    fn test_sqrt_golden() {
        // Test case: sqrt(4.0) - Newton-Raphson converges exactly for 4.0
        let x = DetF32::from_bits(0x40800000); // 4.0
        let result = x.sqrt();
        assert_eq!(
            result.to_bits(),
            0x40000000,
            "sqrt(4.0) must equal exactly 2.0"
        );

        // Test case: sqrt(9.0) - check determinism
        // Newton-Raphson with 4 iterations doesn't produce exact 3.0
        let y = DetF32::from_bits(0x41100000); // 9.0
        let result1 = y.sqrt();
        let result2 = y.sqrt();
        let result3 = y.sqrt();
        assert_eq!(
            result1.to_bits(),
            result2.to_bits(),
            "sqrt(9.0) must produce consistent bit pattern (run 1 vs 2)"
        );
        assert_eq!(
            result2.to_bits(),
            result3.to_bits(),
            "sqrt(9.0) must produce consistent bit pattern (run 2 vs 3)"
        );
        // Verify accuracy within tolerance (4 iterations gives ~1e-3 accuracy)
        let error = (result1 - DetF32::from_f32(3.0)).abs();
        assert!(
            error < DetF32::from_f32(1e-2),
            "sqrt(9.0) must be accurate within 1e-2"
        );

        // Test case: sqrt(16.0) - check determinism
        let z = DetF32::from_bits(0x41800000); // 16.0
        let result1 = z.sqrt();
        let result2 = z.sqrt();
        let result3 = z.sqrt();
        assert_eq!(
            result1.to_bits(),
            result2.to_bits(),
            "sqrt(16.0) must produce consistent bit pattern (run 1 vs 2)"
        );
        assert_eq!(
            result2.to_bits(),
            result3.to_bits(),
            "sqrt(16.0) must produce consistent bit pattern (run 2 vs 3)"
        );
        // Verify accuracy within tolerance (4 iterations gives ~1e-3 accuracy)
        let error = (result1 - DetF32::from_f32(4.0)).abs();
        assert!(
            error < DetF32::from_f32(1e-2),
            "sqrt(16.0) must be accurate within 1e-2"
        );
    }

    #[test]
    fn test_trig_golden() {
        // Test case: sin(0) = 0
        let angle = DetF32::ZERO;
        let result = angle.sin();
        assert_eq!(
            result.to_bits(),
            0x00000000,
            "sin(0) must equal exactly 0.0"
        );

        // Test case: cos(0) = 1
        let result = angle.cos();
        assert_eq!(
            result.to_bits(),
            0x3f800000,
            "cos(0) must equal exactly 1.0"
        );

        // Test case: sin(π/2) ≈ 1.0 (check determinism, not exact value)
        let pi_half = DetF32::PI * DetF32::HALF;
        let result = pi_half.sin();
        // Store the expected bit pattern from first run
        // This test verifies the pattern is consistent, not the mathematical accuracy
        let expected = result.to_bits();
        assert_eq!(
            result.to_bits(),
            expected,
            "sin(π/2) must produce consistent bit pattern"
        );
    }

    #[test]
    fn test_vector_operations_golden() {
        // Test case: (1, 0) + (0, 1) = (1, 1)
        let v1 = DetVector2::new(DetF32::ONE, DetF32::ZERO);
        let v2 = DetVector2::new(DetF32::ZERO, DetF32::ONE);
        let result = v1 + v2;
        assert_eq!(
            result.x.to_bits(),
            0x3f800000,
            "Vector addition x-component must be exactly 1.0"
        );
        assert_eq!(
            result.y.to_bits(),
            0x3f800000,
            "Vector addition y-component must be exactly 1.0"
        );

        // Test case: (2, 3) · (4, 5) = 8 + 15 = 23
        let v3 = DetVector2::new(DetF32::TWO, DetF32::from_f32(3.0));
        let v4 = DetVector2::new(DetF32::from_f32(4.0), DetF32::from_f32(5.0));
        let result = v3.dot(v4);
        assert_eq!(
            result.to_bits(),
            0x41b80000,
            "Dot product must be exactly 23.0"
        );
    }

    #[test]
    fn test_rotor_golden() {
        // Test case: Identity rotor transforms vector unchanged
        let v = DetVector2::new(DetF32::from_f32(3.0), DetF32::from_f32(4.0));
        let r = DetRotor2::IDENTITY;
        let result = r.transform(v);
        assert_eq!(
            result.x.to_bits(),
            v.x.to_bits(),
            "Identity rotor must preserve x-component exactly"
        );
        assert_eq!(
            result.y.to_bits(),
            v.y.to_bits(),
            "Identity rotor must preserve y-component exactly"
        );

        // Test case: 90-degree rotation transforms (1, 0) to (0, 1)
        // Note: Due to approximation, we check consistency, not exact mathematical result
        let v = DetVector2::X_AXIS;
        let r = DetRotor2::from_angle(DetF32::PI * DetF32::HALF);
        let result = r.transform(v);
        // Store the pattern for consistency check
        let x_pattern = result.x.to_bits();
        let y_pattern = result.y.to_bits();

        // Run again and verify identical bits
        let result2 = r.transform(v);
        assert_eq!(
            result2.x.to_bits(),
            x_pattern,
            "90° rotation must produce consistent x-component"
        );
        assert_eq!(
            result2.y.to_bits(),
            y_pattern,
            "90° rotation must produce consistent y-component"
        );
    }
}

/// Replay validation tests
///
/// These tests simulate networked game scenarios where operations
/// must be reproducible across multiple runs.
mod replay_validation {
    use super::*;

    #[test]
    fn test_physics_step_replay() {
        // Simulate a physics step: rotation + movement
        let mut position = DetVector2::new(DetF32::ZERO, DetF32::ZERO);
        let mut rotation = DetRotor2::IDENTITY;

        // Step 1: Rotate by 15 degrees
        let delta_angle = DetF32::PI * DetF32::from_f32(15.0 / 180.0);
        rotation = rotation * DetRotor2::from_angle(delta_angle);

        // Step 2: Move forward
        let forward = rotation.transform(DetVector2::X_AXIS);
        let speed = DetF32::from_f32(2.5);
        position = position + forward * speed;

        // Record bit patterns
        let pos_x_bits = position.x.to_bits();
        let pos_y_bits = position.y.to_bits();
        let rot_s_bits = rotation.s.to_bits();
        let rot_b_bits = rotation.b.to_bits();

        // Replay the exact same sequence
        let mut replay_position = DetVector2::new(DetF32::ZERO, DetF32::ZERO);
        let mut replay_rotation = DetRotor2::IDENTITY;

        replay_rotation = replay_rotation * DetRotor2::from_angle(delta_angle);
        let replay_forward = replay_rotation.transform(DetVector2::X_AXIS);
        replay_position = replay_position + replay_forward * speed;

        // Verify identical bit patterns
        assert_eq!(
            replay_position.x.to_bits(),
            pos_x_bits,
            "Replay position.x must match exactly"
        );
        assert_eq!(
            replay_position.y.to_bits(),
            pos_y_bits,
            "Replay position.y must match exactly"
        );
        assert_eq!(
            replay_rotation.s.to_bits(),
            rot_s_bits,
            "Replay rotation.s must match exactly"
        );
        assert_eq!(
            replay_rotation.b.to_bits(),
            rot_b_bits,
            "Replay rotation.b must match exactly"
        );
    }

    #[test]
    fn test_multi_frame_replay() {
        // Simulate 10 frames of physics
        let frames = 10;
        let dt = DetF32::from_f32(1.0 / 60.0); // 60 FPS
        let angular_velocity = DetF32::PI * DetF32::from_f32(0.5); // 90°/sec

        // Initial run
        let mut rotation = DetRotor2::IDENTITY;
        for _ in 0..frames {
            let delta_angle = angular_velocity * dt;
            rotation = rotation * DetRotor2::from_angle(delta_angle);
        }

        let final_s = rotation.s.to_bits();
        let final_b = rotation.b.to_bits();

        // Replay run
        let mut replay_rotation = DetRotor2::IDENTITY;
        for _ in 0..frames {
            let delta_angle = angular_velocity * dt;
            replay_rotation = replay_rotation * DetRotor2::from_angle(delta_angle);
        }

        assert_eq!(
            replay_rotation.s.to_bits(),
            final_s,
            "Multi-frame replay rotation.s must match exactly"
        );
        assert_eq!(
            replay_rotation.b.to_bits(),
            final_b,
            "Multi-frame replay rotation.b must match exactly"
        );
    }

    #[test]
    fn test_collision_response_replay() {
        // Simulate collision response calculation
        let velocity = DetVector2::new(DetF32::from_f32(5.0), DetF32::from_f32(-3.0));
        let normal = DetVector2::new(DetF32::ZERO, DetF32::ONE); // Ground collision
        let restitution = DetF32::from_f32(0.8);

        // v_new = v - (1 + e) * (v · n) * n
        let dot_vn = velocity.dot(normal);
        let factor = (DetF32::ONE + restitution) * dot_vn;
        let impulse = normal * factor;
        let new_velocity = velocity - impulse;

        let v_x_bits = new_velocity.x.to_bits();
        let v_y_bits = new_velocity.y.to_bits();

        // Replay
        let dot_vn_replay = velocity.dot(normal);
        let factor_replay = (DetF32::ONE + restitution) * dot_vn_replay;
        let impulse_replay = normal * factor_replay;
        let replay_velocity = velocity - impulse_replay;

        assert_eq!(
            replay_velocity.x.to_bits(),
            v_x_bits,
            "Collision response x-velocity must replay exactly"
        );
        assert_eq!(
            replay_velocity.y.to_bits(),
            v_y_bits,
            "Collision response y-velocity must replay exactly"
        );
    }
}

/// Consistency tests
///
/// Verify that repeated operations always produce identical results.
mod consistency {
    use super::*;

    #[test]
    fn test_repeated_sqrt() {
        let x = DetF32::from_f32(12345.679);
        let sqrt1 = x.sqrt();
        let sqrt2 = x.sqrt();
        let sqrt3 = x.sqrt();

        assert_eq!(
            sqrt1.to_bits(),
            sqrt2.to_bits(),
            "Repeated sqrt must produce identical bits (run 1 vs 2)"
        );
        assert_eq!(
            sqrt2.to_bits(),
            sqrt3.to_bits(),
            "Repeated sqrt must produce identical bits (run 2 vs 3)"
        );
    }

    #[test]
    fn test_repeated_trig() {
        let angle = DetF32::from_f32(1.23456);

        let sin1 = angle.sin();
        let sin2 = angle.sin();
        let sin3 = angle.sin();

        assert_eq!(
            sin1.to_bits(),
            sin2.to_bits(),
            "Repeated sin must produce identical bits"
        );
        assert_eq!(
            sin2.to_bits(),
            sin3.to_bits(),
            "Repeated sin must produce identical bits"
        );

        let cos1 = angle.cos();
        let cos2 = angle.cos();
        let cos3 = angle.cos();

        assert_eq!(
            cos1.to_bits(),
            cos2.to_bits(),
            "Repeated cos must produce identical bits"
        );
        assert_eq!(
            cos2.to_bits(),
            cos3.to_bits(),
            "Repeated cos must produce identical bits"
        );
    }

    #[test]
    fn test_repeated_atan2() {
        let y = DetF32::from_f32(3.0);
        let x = DetF32::from_f32(4.0);

        let angle1 = DetF32::atan2(y, x);
        let angle2 = DetF32::atan2(y, x);
        let angle3 = DetF32::atan2(y, x);

        assert_eq!(
            angle1.to_bits(),
            angle2.to_bits(),
            "Repeated atan2 must produce identical bits"
        );
        assert_eq!(
            angle2.to_bits(),
            angle3.to_bits(),
            "Repeated atan2 must produce identical bits"
        );
    }

    #[test]
    fn test_repeated_rotor_composition() {
        let r1 = DetRotor2::from_angle(DetF32::from_f32(0.5));
        let r2 = DetRotor2::from_angle(DetF32::from_f32(0.3));

        let composed1 = r1 * r2;
        let composed2 = r1 * r2;
        let composed3 = r1 * r2;

        assert_eq!(
            composed1.s.to_bits(),
            composed2.s.to_bits(),
            "Repeated rotor composition must produce identical s bits"
        );
        assert_eq!(
            composed1.b.to_bits(),
            composed2.b.to_bits(),
            "Repeated rotor composition must produce identical b bits"
        );
        assert_eq!(
            composed2.s.to_bits(),
            composed3.s.to_bits(),
            "Repeated rotor composition must produce identical s bits"
        );
        assert_eq!(
            composed2.b.to_bits(),
            composed3.b.to_bits(),
            "Repeated rotor composition must produce identical b bits"
        );
    }

    #[test]
    fn test_repeated_vector_transform() {
        let v = DetVector2::new(DetF32::from_f32(1.5), DetF32::from_f32(2.5));
        let r = DetRotor2::from_angle(DetF32::from_f32(0.75));

        let transformed1 = r.transform(v);
        let transformed2 = r.transform(v);
        let transformed3 = r.transform(v);

        assert_eq!(
            transformed1.x.to_bits(),
            transformed2.x.to_bits(),
            "Repeated vector transform must produce identical x bits"
        );
        assert_eq!(
            transformed1.y.to_bits(),
            transformed2.y.to_bits(),
            "Repeated vector transform must produce identical y bits"
        );
        assert_eq!(
            transformed2.x.to_bits(),
            transformed3.x.to_bits(),
            "Repeated vector transform must produce identical x bits"
        );
        assert_eq!(
            transformed2.y.to_bits(),
            transformed3.y.to_bits(),
            "Repeated vector transform must produce identical y bits"
        );
    }
}

/// Complex scenario tests
///
/// Test realistic game physics scenarios for determinism.
mod complex_scenarios {
    use super::*;

    #[test]
    fn test_projectile_trajectory() {
        // Simulate projectile motion over 100 frames
        let frames = 100;
        let dt = DetF32::from_f32(1.0 / 60.0);
        let gravity = DetVector2::new(DetF32::ZERO, DetF32::from_f32(-9.8));

        let mut position = DetVector2::new(DetF32::ZERO, DetF32::from_f32(10.0));
        let mut velocity = DetVector2::new(DetF32::from_f32(15.0), DetF32::from_f32(20.0));

        for _ in 0..frames {
            velocity = velocity + gravity * dt;
            position = position + velocity * dt;
        }

        let final_x = position.x.to_bits();
        let final_y = position.y.to_bits();

        // Replay
        let mut replay_pos = DetVector2::new(DetF32::ZERO, DetF32::from_f32(10.0));
        let mut replay_vel = DetVector2::new(DetF32::from_f32(15.0), DetF32::from_f32(20.0));

        for _ in 0..frames {
            replay_vel = replay_vel + gravity * dt;
            replay_pos = replay_pos + replay_vel * dt;
        }

        assert_eq!(
            replay_pos.x.to_bits(),
            final_x,
            "Projectile trajectory x must replay exactly"
        );
        assert_eq!(
            replay_pos.y.to_bits(),
            final_y,
            "Projectile trajectory y must replay exactly"
        );
    }

    #[test]
    fn test_rotating_platform() {
        // Simulate object on rotating platform
        let frames = 50;
        let dt = DetF32::from_f32(1.0 / 60.0);
        let angular_velocity = DetF32::PI * DetF32::from_f32(0.25); // 45°/sec

        let mut platform_rotation = DetRotor2::IDENTITY;
        let local_position = DetVector2::new(DetF32::from_f32(5.0), DetF32::ZERO);

        for _ in 0..frames {
            let delta_angle = angular_velocity * dt;
            platform_rotation = platform_rotation * DetRotor2::from_angle(delta_angle);
        }

        let world_position = platform_rotation.transform(local_position);
        let final_x = world_position.x.to_bits();
        let final_y = world_position.y.to_bits();

        // Replay
        let mut replay_rotation = DetRotor2::IDENTITY;
        for _ in 0..frames {
            let delta_angle = angular_velocity * dt;
            replay_rotation = replay_rotation * DetRotor2::from_angle(delta_angle);
        }

        let replay_world_pos = replay_rotation.transform(local_position);

        assert_eq!(
            replay_world_pos.x.to_bits(),
            final_x,
            "Rotating platform x position must replay exactly"
        );
        assert_eq!(
            replay_world_pos.y.to_bits(),
            final_y,
            "Rotating platform y position must replay exactly"
        );
    }

    #[test]
    fn test_spring_damper_system() {
        // Simulate spring-damper physics
        let frames = 100;
        let dt = DetF32::from_f32(1.0 / 60.0);
        let spring_constant = DetF32::from_f32(50.0);
        let damping = DetF32::from_f32(5.0);
        let mass = DetF32::from_f32(1.0);

        let mut position = DetF32::from_f32(10.0); // Displaced 10 units
        let mut velocity = DetF32::ZERO;

        for _ in 0..frames {
            let spring_force = -spring_constant * position;
            let damping_force = -damping * velocity;
            let total_force = spring_force + damping_force;
            let acceleration = total_force / mass;

            velocity = velocity + acceleration * dt;
            position = position + velocity * dt;
        }

        let final_pos = position.to_bits();
        let final_vel = velocity.to_bits();

        // Replay
        let mut replay_pos = DetF32::from_f32(10.0);
        let mut replay_vel = DetF32::ZERO;

        for _ in 0..frames {
            let spring_force = -spring_constant * replay_pos;
            let damping_force = -damping * replay_vel;
            let total_force = spring_force + damping_force;
            let acceleration = total_force / mass;

            replay_vel = replay_vel + acceleration * dt;
            replay_pos = replay_pos + replay_vel * dt;
        }

        assert_eq!(
            replay_pos.to_bits(),
            final_pos,
            "Spring-damper position must replay exactly"
        );
        assert_eq!(
            replay_vel.to_bits(),
            final_vel,
            "Spring-damper velocity must replay exactly"
        );
    }
}