clifford 0.3.0

Geometric Algebra (Clifford Algebra) for Rust: rotors, motors, PGA for 3D rotations and rigid transforms
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
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! Projective signature types for Projective Geometric Algebra (PGA).
//!
//! This module provides signature types for Projective Geometric Algebras,
//! which extend Euclidean space with a degenerate (null) basis vector `e₀`
//! that squares to zero (`e₀² = 0`).
//!
//! # Point-Based Formulation
//!
//! This library uses the **point-based** formulation of PGA where:
//! - Grade 1 (vectors): Points in homogeneous coordinates
//! - Grade 2 (bivectors): Lines
//! - Grade 3 (trivectors): Planes (in 3D)
//!
//! This is consistent with Euclidean GA conventions and nalgebra's `Point` types.
//!
//! # Available Signatures
//!
//! - [`Projective2`]: 2D PGA, `Cl(2,0,1)` with 8 basis blades
//! - [`Projective3`]: 3D PGA, `Cl(3,0,1)` with 16 basis blades
//!
//! # Basis Vector Ordering
//!
//! Basis vectors are ordered with Euclidean vectors first, then the null vector:
//! - 2D PGA: `e₁, e₂, e₀` (indices 0, 1, 2)
//! - 3D PGA: `e₁, e₂, e₃, e₀` (indices 0, 1, 2, 3)
//!
//! # Example
//!
//! ```
//! use clifford::prelude::*;
//!
//! // 3D Projective GA
//! assert_eq!(Projective3::DIM, 4);
//! assert_eq!(Projective3::num_blades(), 16);
//!
//! // Euclidean basis vectors square to +1
//! assert_eq!(Projective3::metric(0), 1);  // e₁² = 1
//! assert_eq!(Projective3::metric(1), 1);  // e₂² = 1
//! assert_eq!(Projective3::metric(2), 1);  // e₃² = 1
//!
//! // Null basis vector squares to 0
//! assert_eq!(Projective3::metric(3), 0);  // e₀² = 0
//! ```

use super::Signature;
use typenum::{U8, U16};

/// 2D Projective signature: `Cl(2,0,1)`.
///
/// Represents 2D projective space with basis vectors `e₁`, `e₂`, and `e₀`,
/// where `e₀² = 0` is the null (degenerate) basis vector.
///
/// # Point-Based Interpretation
///
/// In the point-based formulation:
/// - **Grade 1 (vectors)**: Points `P = x·e₁ + y·e₂ + w·e₀`
/// - **Grade 2 (bivectors)**: Lines
/// - **Grade 3 (pseudoscalar)**: Oriented area
///
/// A point with `w = 0` represents an ideal point (point at infinity).
///
/// # Basis Blades (8 total)
///
/// | Index | Binary | Blade | Grade | Description |
/// |-------|--------|-------|-------|-------------|
/// | 0 | `000` | `1` | 0 | Scalar |
/// | 1 | `001` | `e₁` | 1 | x-component of point |
/// | 2 | `010` | `e₂` | 1 | y-component of point |
/// | 3 | `011` | `e₁₂` | 2 | Line component |
/// | 4 | `100` | `e₀` | 1 | Homogeneous weight |
/// | 5 | `101` | `e₀₁` | 2 | Line component |
/// | 6 | `110` | `e₀₂` | 2 | Line component |
/// | 7 | `111` | `e₀₁₂` | 3 | Pseudoscalar |
///
/// # Properties
///
/// - `e₁² = e₂² = +1` (Euclidean)
/// - `e₀² = 0` (null/degenerate)
/// - `e₁₂² = -1` (rotation generator)
///
/// # Example
///
/// ```
/// use clifford::prelude::*;
///
/// assert_eq!(Projective2::DIM, 3);
/// assert_eq!(Projective2::num_blades(), 8);
///
/// // Euclidean basis vectors
/// assert_eq!(Projective2::metric(0), 1);  // e₁² = 1
/// assert_eq!(Projective2::metric(1), 1);  // e₂² = 1
///
/// // Null basis vector
/// assert_eq!(Projective2::metric(2), 0);  // e₀² = 0
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Projective2;

impl Signature for Projective2 {
    type NumBlades = U8; // 2^3 = 8

    const P: usize = 2;
    const Q: usize = 0;
    const R: usize = 1;

    #[inline]
    fn metric(i: usize) -> i8 {
        match i {
            0 | 1 => 1, // e₁² = e₂² = 1
            2 => 0,     // e₀² = 0
            _ => panic!("basis index {i} out of range for Projective2 (DIM=3)"),
        }
    }
}

/// 3D Projective signature: `Cl(3,0,1)`.
///
/// Represents 3D projective space with basis vectors `e₁`, `e₂`, `e₃`, and `e₀`,
/// where `e₀² = 0` is the null (degenerate) basis vector.
///
/// # Point-Based Interpretation
///
/// In the point-based formulation:
/// - **Grade 1 (vectors)**: Points `P = x·e₁ + y·e₂ + z·e₃ + w·e₀`
/// - **Grade 2 (bivectors)**: Lines (Plücker coordinates)
/// - **Grade 3 (trivectors)**: Planes
/// - **Grade 4 (pseudoscalar)**: Oriented volume
///
/// A point with `w = 0` represents an ideal point (point at infinity).
///
/// # Basis Blades (16 total)
///
/// | Grade | Count | Blades | Description |
/// |-------|-------|--------|-------------|
/// | 0 | 1 | `1` | Scalar |
/// | 1 | 4 | `e₁, e₂, e₃, e₀` | Points |
/// | 2 | 6 | `e₁₂, e₁₃, e₂₃, e₀₁, e₀₂, e₀₃` | Lines |
/// | 3 | 4 | `e₁₂₃, e₀₁₂, e₀₁₃, e₀₂₃` | Planes |
/// | 4 | 1 | `e₀₁₂₃` | Pseudoscalar |
///
/// # Motors
///
/// The even subalgebra (grades 0, 2, 4) contains **motors**, which represent
/// rigid body transformations (rotation + translation). A motor `M` transforms
/// a point `P` via the sandwich product: `P' = M P M̃`.
///
/// # Properties
///
/// - `e₁² = e₂² = e₃² = +1` (Euclidean)
/// - `e₀² = 0` (null/degenerate)
/// - Bivectors `e₁₂, e₁₃, e₂₃` each square to `-1` (rotation generators)
/// - Bivectors `e₀₁, e₀₂, e₀₃` square to `0` (translation generators)
///
/// # Example
///
/// ```
/// use clifford::prelude::*;
///
/// assert_eq!(Projective3::DIM, 4);
/// assert_eq!(Projective3::num_blades(), 16);
///
/// // Euclidean basis vectors
/// assert_eq!(Projective3::metric(0), 1);  // e₁² = 1
/// assert_eq!(Projective3::metric(1), 1);  // e₂² = 1
/// assert_eq!(Projective3::metric(2), 1);  // e₃² = 1
///
/// // Null basis vector
/// assert_eq!(Projective3::metric(3), 0);  // e₀² = 0
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Projective3;

impl Signature for Projective3 {
    type NumBlades = U16; // 2^4 = 16

    const P: usize = 3;
    const Q: usize = 0;
    const R: usize = 1;

    #[inline]
    fn metric(i: usize) -> i8 {
        match i {
            0..=2 => 1, // e₁² = e₂² = e₃² = 1
            3 => 0,     // e₀² = 0
            _ => panic!("basis index {i} out of range for Projective3 (DIM=4)"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::algebra::Multivector;
    use crate::basis::Blade;
    use crate::test_utils::RELATIVE_EQ_EPS;
    use approx::relative_eq;
    use proptest::prelude::*;

    // ========================================================================
    // Basic signature tests
    // ========================================================================

    proptest! {
        #[test]
        fn projective2_euclidean_metric_positive(i in 0usize..2) {
            prop_assert_eq!(Projective2::metric(i), 1);
        }

        #[test]
        fn projective3_euclidean_metric_positive(i in 0usize..3) {
            prop_assert_eq!(Projective3::metric(i), 1);
        }
    }

    #[test]
    fn projective2_null_metric() {
        assert_eq!(Projective2::metric(2), 0);
    }

    #[test]
    fn projective3_null_metric() {
        assert_eq!(Projective3::metric(3), 0);
    }

    #[test]
    fn projective_dimensions() {
        assert_eq!(Projective2::DIM, 3);
        assert_eq!(Projective2::num_blades(), 8);

        assert_eq!(Projective3::DIM, 4);
        assert_eq!(Projective3::num_blades(), 16);
    }

    #[test]
    fn projective_pqr_consistency() {
        assert_eq!(
            Projective2::P + Projective2::Q + Projective2::R,
            Projective2::DIM
        );
        assert_eq!(
            Projective3::P + Projective3::Q + Projective3::R,
            Projective3::DIM
        );
    }

    // ========================================================================
    // Null vector behavior tests
    // ========================================================================

    #[test]
    fn pga2_null_vector_squares_to_zero() {
        // e₀ is at index 2 (basis vector index), blade index is 1 << 2 = 4
        let e0: Multivector<f64, Projective2> = Multivector::basis_vector(2);
        let e0_sq = e0 * e0;
        assert!(e0_sq.is_zero(RELATIVE_EQ_EPS));
    }

    #[test]
    fn pga3_null_vector_squares_to_zero() {
        // e₀ is at index 3 (basis vector index), blade index is 1 << 3 = 8
        let e0: Multivector<f64, Projective3> = Multivector::basis_vector(3);
        let e0_sq = e0 * e0;
        assert!(e0_sq.is_zero(RELATIVE_EQ_EPS));
    }

    #[test]
    fn pga3_euclidean_vectors_square_to_one() {
        let e1: Multivector<f64, Projective3> = Multivector::basis_vector(0);
        let e2: Multivector<f64, Projective3> = Multivector::basis_vector(1);
        let e3: Multivector<f64, Projective3> = Multivector::basis_vector(2);

        assert!(relative_eq!(
            (e1 * e1).scalar_part(),
            1.0,
            max_relative = RELATIVE_EQ_EPS
        ));
        assert!(relative_eq!(
            (e2 * e2).scalar_part(),
            1.0,
            max_relative = RELATIVE_EQ_EPS
        ));
        assert!(relative_eq!(
            (e3 * e3).scalar_part(),
            1.0,
            max_relative = RELATIVE_EQ_EPS
        ));
    }

    #[test]
    fn pga3_euclidean_bivector_squares_to_minus_one() {
        // e₁₂ should square to -1
        let e1: Multivector<f64, Projective3> = Multivector::basis_vector(0);
        let e2: Multivector<f64, Projective3> = Multivector::basis_vector(1);
        let e12 = e1 * e2;
        let e12_sq = e12 * e12;

        assert!(relative_eq!(
            e12_sq.scalar_part(),
            -1.0,
            max_relative = RELATIVE_EQ_EPS
        ));
    }

    #[test]
    fn pga3_null_bivector_squares_to_zero() {
        // e₀₁ should square to 0 (contains null vector)
        let e0: Multivector<f64, Projective3> = Multivector::basis_vector(3);
        let e1: Multivector<f64, Projective3> = Multivector::basis_vector(0);
        let e01 = e0 * e1;
        let e01_sq = e01 * e01;

        assert!(e01_sq.is_zero(RELATIVE_EQ_EPS));
    }

    // ========================================================================
    // Point representation tests
    // ========================================================================

    #[test]
    fn pga3_point_at_origin() {
        // Origin is represented as e₀ (just the homogeneous weight)
        let origin: Multivector<f64, Projective3> = Multivector::basis_vector(3);

        // Should be grade 1
        assert_eq!(origin.grade(RELATIVE_EQ_EPS), Some(1));
    }

    #[test]
    fn pga3_finite_point() {
        // Point at (1, 2, 3) is e₁ + 2e₂ + 3e₃ + e₀
        let mut point: Multivector<f64, Projective3> = Multivector::zero();
        point.set(Blade::basis_vector(0), 1.0); // e₁
        point.set(Blade::basis_vector(1), 2.0); // e₂
        point.set(Blade::basis_vector(2), 3.0); // e₃
        point.set(Blade::basis_vector(3), 1.0); // e₀ (weight = 1)

        // Should be grade 1
        assert_eq!(point.grade(RELATIVE_EQ_EPS), Some(1));
    }

    #[test]
    fn pga3_ideal_point() {
        // Ideal point (at infinity) in direction (1, 0, 0) has no e₀ component
        let mut ideal: Multivector<f64, Projective3> = Multivector::zero();
        ideal.set(Blade::basis_vector(0), 1.0); // e₁ only

        // Should be grade 1
        assert_eq!(ideal.grade(RELATIVE_EQ_EPS), Some(1));

        // No e₀ component (index 3 -> blade index 8)
        assert!(relative_eq!(
            ideal.get(Blade::basis_vector(3)),
            0.0,
            max_relative = RELATIVE_EQ_EPS
        ));
    }

    // ========================================================================
    // Property-based algebraic tests for PGA2
    // ========================================================================

    proptest! {
        #[test]
        fn pga2_geometric_product_associative(
            a in any::<Multivector<f64, Projective2>>(),
            b in any::<Multivector<f64, Projective2>>(),
            c in any::<Multivector<f64, Projective2>>(),
        ) {
            let lhs = (a * b) * c;
            let rhs = a * (b * c);
            prop_assert!(relative_eq!(lhs, rhs, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_geometric_product_distributive(
            a in any::<Multivector<f64, Projective2>>(),
            b in any::<Multivector<f64, Projective2>>(),
            c in any::<Multivector<f64, Projective2>>(),
        ) {
            let lhs = a * (b + c);
            let rhs = (a * b) + (a * c);
            prop_assert!(relative_eq!(lhs, rhs, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_scaled_null_vector_squares_to_zero(s in -100.0f64..100.0) {
            // Any scalar multiple of e₀ should square to zero
            let e0: Multivector<f64, Projective2> = Multivector::basis_vector(2);
            let scaled = e0 * s;
            let sq = scaled * scaled;
            prop_assert!(sq.is_zero(RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_reverse_involutory(a in any::<Multivector<f64, Projective2>>()) {
            prop_assert!(relative_eq!(a.reverse().reverse(), a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_reverse_antimorphism(
            a in any::<Multivector<f64, Projective2>>(),
            b in any::<Multivector<f64, Projective2>>(),
        ) {
            let lhs = (a * b).reverse();
            let rhs = b.reverse() * a.reverse();
            prop_assert!(relative_eq!(lhs, rhs, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_involute_involutory(a in any::<Multivector<f64, Projective2>>()) {
            prop_assert!(relative_eq!(a.involute().involute(), a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_outer_associative(
            a in any::<Multivector<f64, Projective2>>(),
            b in any::<Multivector<f64, Projective2>>(),
            c in any::<Multivector<f64, Projective2>>(),
        ) {
            let lhs = a.exterior(&b).exterior(&c);
            let rhs = a.exterior(&b.exterior(&c));
            prop_assert!(relative_eq!(lhs, rhs, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_one_is_multiplicative_identity(a in any::<Multivector<f64, Projective2>>()) {
            let one = Multivector::<f64, Projective2>::one();
            prop_assert!(relative_eq!(a * one, a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
            prop_assert!(relative_eq!(one * a, a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_zero_is_additive_identity(a in any::<Multivector<f64, Projective2>>()) {
            let zero = Multivector::<f64, Projective2>::zero();
            prop_assert!(relative_eq!(a + zero, a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_add_commutative(
            a in any::<Multivector<f64, Projective2>>(),
            b in any::<Multivector<f64, Projective2>>()
        ) {
            prop_assert!(relative_eq!(a + b, b + a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga2_grade_decomposition_complete(a in any::<Multivector<f64, Projective2>>()) {
            // Sum of all grade projections equals original (grades 0, 1, 2, 3)
            let sum = (0..=3)
                .map(|k| a.grade_select(k))
                .fold(Multivector::<f64, Projective2>::zero(), |acc, x| acc + x);
            prop_assert!(relative_eq!(sum, a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }
    }

    // ========================================================================
    // Property-based algebraic tests for PGA3
    // ========================================================================

    proptest! {
        #[test]
        fn pga3_geometric_product_associative(
            a in any::<Multivector<f64, Projective3>>(),
            b in any::<Multivector<f64, Projective3>>(),
            c in any::<Multivector<f64, Projective3>>(),
        ) {
            let lhs = (a * b) * c;
            let rhs = a * (b * c);
            prop_assert!(relative_eq!(lhs, rhs, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_geometric_product_distributive(
            a in any::<Multivector<f64, Projective3>>(),
            b in any::<Multivector<f64, Projective3>>(),
            c in any::<Multivector<f64, Projective3>>(),
        ) {
            let lhs = a * (b + c);
            let rhs = (a * b) + (a * c);
            prop_assert!(relative_eq!(lhs, rhs, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_scaled_null_vector_squares_to_zero(s in -100.0f64..100.0) {
            // Any scalar multiple of e₀ should square to zero
            let e0: Multivector<f64, Projective3> = Multivector::basis_vector(3);
            let scaled = e0 * s;
            let sq = scaled * scaled;
            prop_assert!(sq.is_zero(RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_reverse_involutory(a in any::<Multivector<f64, Projective3>>()) {
            prop_assert!(relative_eq!(a.reverse().reverse(), a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_reverse_antimorphism(
            a in any::<Multivector<f64, Projective3>>(),
            b in any::<Multivector<f64, Projective3>>(),
        ) {
            let lhs = (a * b).reverse();
            let rhs = b.reverse() * a.reverse();
            prop_assert!(relative_eq!(lhs, rhs, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_involute_involutory(a in any::<Multivector<f64, Projective3>>()) {
            prop_assert!(relative_eq!(a.involute().involute(), a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_outer_associative(
            a in any::<Multivector<f64, Projective3>>(),
            b in any::<Multivector<f64, Projective3>>(),
            c in any::<Multivector<f64, Projective3>>(),
        ) {
            let lhs = a.exterior(&b).exterior(&c);
            let rhs = a.exterior(&b.exterior(&c));
            prop_assert!(relative_eq!(lhs, rhs, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_one_is_multiplicative_identity(a in any::<Multivector<f64, Projective3>>()) {
            let one = Multivector::<f64, Projective3>::one();
            prop_assert!(relative_eq!(a * one, a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
            prop_assert!(relative_eq!(one * a, a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_zero_is_additive_identity(a in any::<Multivector<f64, Projective3>>()) {
            let zero = Multivector::<f64, Projective3>::zero();
            prop_assert!(relative_eq!(a + zero, a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_add_commutative(
            a in any::<Multivector<f64, Projective3>>(),
            b in any::<Multivector<f64, Projective3>>()
        ) {
            prop_assert!(relative_eq!(a + b, b + a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_grade_decomposition_complete(a in any::<Multivector<f64, Projective3>>()) {
            // Sum of all grade projections equals original (grades 0, 1, 2, 3, 4)
            let sum = (0..=4)
                .map(|k| a.grade_select(k))
                .fold(Multivector::<f64, Projective3>::zero(), |acc, x| acc + x);
            prop_assert!(relative_eq!(sum, a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        #[test]
        fn pga3_even_plus_odd_equals_original(a in any::<Multivector<f64, Projective3>>()) {
            let reconstructed = a.even() + a.odd();
            prop_assert!(relative_eq!(reconstructed, a, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
        }

        // Note: dual/undual tests are NOT included because in PGA the pseudoscalar
        // contains the null vector e₀ and is therefore not invertible (I² = 0).
        // The dual operation requires an invertible pseudoscalar.
    }

    // ========================================================================
    // nalgebra consistency tests
    // ========================================================================

    #[cfg(feature = "nalgebra-0_33")]
    mod nalgebra_consistency_0_33 {
        use super::*;
        use nalgebra_0_33 as na;

        proptest! {
            /// Euclidean subspace dot product matches nalgebra
            #[test]
            fn pga3_euclidean_dot_matches_nalgebra(
                ax in -100.0f64..100.0, ay in -100.0f64..100.0, az in -100.0f64..100.0,
                bx in -100.0f64..100.0, by in -100.0f64..100.0, bz in -100.0f64..100.0,
            ) {
                // Pure Euclidean vectors (no e₀ component) - components for e₁, e₂, e₃, e₀
                let pga_a: Multivector<f64, Projective3> =
                    Multivector::vector(&[ax, ay, az, 0.0]);
                let pga_b: Multivector<f64, Projective3> =
                    Multivector::vector(&[bx, by, bz, 0.0]);

                let na_a = na::Vector3::new(ax, ay, az);
                let na_b = na::Vector3::new(bx, by, bz);

                let pga_dot = pga_a.inner(&pga_b).scalar_part();
                let na_dot = na_a.dot(&na_b);

                prop_assert!(relative_eq!(pga_dot, na_dot, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
            }

            /// Euclidean subspace geometric product gives correct scalar + bivector
            #[test]
            fn pga3_euclidean_vectors_product(
                ax in -10.0f64..10.0, ay in -10.0f64..10.0, az in -10.0f64..10.0,
                bx in -10.0f64..10.0, by in -10.0f64..10.0, bz in -10.0f64..10.0,
            ) {
                let pga_a: Multivector<f64, Projective3> =
                    Multivector::vector(&[ax, ay, az, 0.0]);
                let pga_b: Multivector<f64, Projective3> =
                    Multivector::vector(&[bx, by, bz, 0.0]);

                let na_a = na::Vector3::new(ax, ay, az);
                let na_b = na::Vector3::new(bx, by, bz);

                // Geometric product of vectors: ab = a·b + a∧b
                let product = pga_a * pga_b;

                // Scalar part should be dot product
                let expected_scalar = na_a.dot(&na_b);
                prop_assert!(relative_eq!(
                    product.scalar_part(),
                    expected_scalar,
                    max_relative = RELATIVE_EQ_EPS
                ));

                // Should have no grade-1 or grade-3 parts (only scalar and bivector)
                prop_assert!(product.grade_select(1).is_zero(RELATIVE_EQ_EPS));
                prop_assert!(product.grade_select(3).is_zero(RELATIVE_EQ_EPS));
            }

            /// Cross product can be extracted from wedge product in 3D
            #[test]
            fn pga3_wedge_matches_cross_product(
                ax in -10.0f64..10.0, ay in -10.0f64..10.0, az in -10.0f64..10.0,
                bx in -10.0f64..10.0, by in -10.0f64..10.0, bz in -10.0f64..10.0,
            ) {
                let pga_a: Multivector<f64, Projective3> =
                    Multivector::vector(&[ax, ay, az, 0.0]);
                let pga_b: Multivector<f64, Projective3> =
                    Multivector::vector(&[bx, by, bz, 0.0]);

                let na_a = na::Vector3::new(ax, ay, az);
                let na_b = na::Vector3::new(bx, by, bz);
                let na_cross = na_a.cross(&na_b);

                // The wedge product a∧b gives a bivector
                let wedge = pga_a.exterior(&pga_b);

                // In 3D Euclidean subspace, the bivector components are related to cross product:
                // e₁₂ component = ax*by - ay*bx = (a×b)_z
                // e₁₃ component = ax*bz - az*bx = -(a×b)_y
                // e₂₃ component = ay*bz - az*by = (a×b)_x
                //
                // Blade indices: e₁₂ = 0b0011 = 3, e₁₃ = 0b0101 = 5, e₂₃ = 0b0110 = 6
                let biv_12 = wedge.get(Blade::from_index(0b0011)); // e₁₂
                let biv_13 = wedge.get(Blade::from_index(0b0101)); // e₁₃
                let biv_23 = wedge.get(Blade::from_index(0b0110)); // e₂₃

                prop_assert!(relative_eq!(biv_23, na_cross.x, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
                prop_assert!(relative_eq!(-biv_13, na_cross.y, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
                prop_assert!(relative_eq!(biv_12, na_cross.z, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
            }
        }
    }

    #[cfg(feature = "nalgebra-0_34")]
    mod nalgebra_consistency_0_34 {
        use super::*;
        use nalgebra_0_34 as na;

        proptest! {
            /// Euclidean subspace dot product matches nalgebra
            #[test]
            fn pga3_euclidean_dot_matches_nalgebra(
                ax in -100.0f64..100.0, ay in -100.0f64..100.0, az in -100.0f64..100.0,
                bx in -100.0f64..100.0, by in -100.0f64..100.0, bz in -100.0f64..100.0,
            ) {
                // Pure Euclidean vectors (no e₀ component) - components for e₁, e₂, e₃, e₀
                let pga_a: Multivector<f64, Projective3> =
                    Multivector::vector(&[ax, ay, az, 0.0]);
                let pga_b: Multivector<f64, Projective3> =
                    Multivector::vector(&[bx, by, bz, 0.0]);

                let na_a = na::Vector3::new(ax, ay, az);
                let na_b = na::Vector3::new(bx, by, bz);

                let pga_dot = pga_a.inner(&pga_b).scalar_part();
                let na_dot = na_a.dot(&na_b);

                prop_assert!(relative_eq!(pga_dot, na_dot, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
            }

            /// Euclidean subspace geometric product gives correct scalar + bivector
            #[test]
            fn pga3_euclidean_vectors_product(
                ax in -10.0f64..10.0, ay in -10.0f64..10.0, az in -10.0f64..10.0,
                bx in -10.0f64..10.0, by in -10.0f64..10.0, bz in -10.0f64..10.0,
            ) {
                let pga_a: Multivector<f64, Projective3> =
                    Multivector::vector(&[ax, ay, az, 0.0]);
                let pga_b: Multivector<f64, Projective3> =
                    Multivector::vector(&[bx, by, bz, 0.0]);

                let na_a = na::Vector3::new(ax, ay, az);
                let na_b = na::Vector3::new(bx, by, bz);

                // Geometric product of vectors: ab = a·b + a∧b
                let product = &pga_a * &pga_b;

                // Scalar part should be dot product
                let expected_scalar = na_a.dot(&na_b);
                prop_assert!(relative_eq!(
                    product.scalar_part(),
                    expected_scalar,
                    max_relative = RELATIVE_EQ_EPS
                ));

                // Should have no grade-1 or grade-3 parts (only scalar and bivector)
                prop_assert!(product.grade_select(1).is_zero(RELATIVE_EQ_EPS));
                prop_assert!(product.grade_select(3).is_zero(RELATIVE_EQ_EPS));
            }

            /// Cross product can be extracted from wedge product in 3D
            #[test]
            fn pga3_wedge_matches_cross_product(
                ax in -10.0f64..10.0, ay in -10.0f64..10.0, az in -10.0f64..10.0,
                bx in -10.0f64..10.0, by in -10.0f64..10.0, bz in -10.0f64..10.0,
            ) {
                let pga_a: Multivector<f64, Projective3> =
                    Multivector::vector(&[ax, ay, az, 0.0]);
                let pga_b: Multivector<f64, Projective3> =
                    Multivector::vector(&[bx, by, bz, 0.0]);

                let na_a = na::Vector3::new(ax, ay, az);
                let na_b = na::Vector3::new(bx, by, bz);
                let na_cross = na_a.cross(&na_b);

                // The wedge product a∧b gives a bivector
                let wedge = pga_a.exterior(&pga_b);

                // In 3D Euclidean subspace, the bivector components are related to cross product:
                // e₁₂ component = ax*by - ay*bx = (a×b)_z
                // e₁₃ component = ax*bz - az*bx = -(a×b)_y
                // e₂₃ component = ay*bz - az*by = (a×b)_x
                //
                // Blade indices: e₁₂ = 0b0011 = 3, e₁₃ = 0b0101 = 5, e₂₃ = 0b0110 = 6
                let biv_12 = wedge.get(Blade::from_index(0b0011)); // e₁₂
                let biv_13 = wedge.get(Blade::from_index(0b0101)); // e₁₃
                let biv_23 = wedge.get(Blade::from_index(0b0110)); // e₂₃

                prop_assert!(relative_eq!(biv_23, na_cross.x, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
                prop_assert!(relative_eq!(-biv_13, na_cross.y, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
                prop_assert!(relative_eq!(biv_12, na_cross.z, epsilon = RELATIVE_EQ_EPS, max_relative = RELATIVE_EQ_EPS));
            }
        }
    }
}