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
//! Euclidean signature types for standard geometric algebras.
//!
//! This module provides signature types for Euclidean spaces where all basis
//! vectors square to `+1`. These are the most commonly used signatures in
//! practical geometric algebra applications.
//!
//! # Available Signatures
//!
//! - [`Euclidean2`]: 2D plane, `Cl(2,0,0)` with 4 basis blades
//! - [`Euclidean3`]: 3D space, `Cl(3,0,0)` with 8 basis blades
//! - [`Euclidean4`]: 4D space, `Cl(4,0,0)` with 16 basis blades
//!
//! # Basis Blade Structure
//!
//! For Euclidean 3D (`Cl(3,0,0)`), the 8 basis blades are:
//!
//! | Index | Binary | Blade | Grade | Name |
//! |-------|--------|-------|-------|------|
//! | 0 | `000` | `1` | 0 | Scalar |
//! | 1 | `001` | `e₁` | 1 | Vector |
//! | 2 | `010` | `e₂` | 1 | Vector |
//! | 3 | `011` | `e₁₂` | 2 | Bivector |
//! | 4 | `100` | `e₃` | 1 | Vector |
//! | 5 | `101` | `e₁₃` | 2 | Bivector |
//! | 6 | `110` | `e₂₃` | 2 | Bivector |
//! | 7 | `111` | `e₁₂₃` | 3 | Pseudoscalar |

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

/// 2D Euclidean signature: `Cl(2,0,0)`.
///
/// Represents the standard 2D plane with basis vectors `e₁` and `e₂`.
///
/// # Basis Blades (4 total)
///
/// | Index | Blade | Grade | Description |
/// |-------|-------|-------|-------------|
/// | 0 | `1` | 0 | Scalar |
/// | 1 | `e₁` | 1 | x-direction vector |
/// | 2 | `e₂` | 1 | y-direction vector |
/// | 3 | `e₁₂` | 2 | Pseudoscalar (oriented area) |
///
/// # Properties
///
/// - `e₁² = e₂² = +1`
/// - `e₁₂² = e₁e₂e₁e₂ = -e₁e₁e₂e₂ = -1`
/// - The pseudoscalar `e₁₂` behaves like the imaginary unit `i`
///
/// # Example
///
/// ```
/// use clifford::prelude::*;
///
/// assert_eq!(Euclidean2::DIM, 2);
/// assert_eq!(Euclidean2::num_blades(), 4);
/// assert_eq!(Euclidean2::metric(0), 1);
/// assert_eq!(Euclidean2::metric(1), 1);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Euclidean2;

impl Signature for Euclidean2 {
    type NumBlades = U4; // 2^2 = 4

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Euclidean2");
        1
    }
}

/// 3D Euclidean signature: `Cl(3,0,0)`.
///
/// Represents standard 3D space with basis vectors `e₁`, `e₂`, and `e₃`.
/// This is the most commonly used geometric algebra for 3D graphics,
/// physics simulations, and robotics.
///
/// # Basis Blades (8 total)
///
/// | Index | Blade | Grade | Description |
/// |-------|-------|-------|-------------|
/// | 0 | `1` | 0 | Scalar |
/// | 1 | `e₁` | 1 | x-direction vector |
/// | 2 | `e₂` | 1 | y-direction vector |
/// | 3 | `e₁₂` | 2 | xy-plane bivector |
/// | 4 | `e₃` | 1 | z-direction vector |
/// | 5 | `e₁₃` | 2 | xz-plane bivector |
/// | 6 | `e₂₃` | 2 | yz-plane bivector |
/// | 7 | `e₁₂₃` | 3 | Pseudoscalar (oriented volume) |
///
/// # Bivectors and Rotation
///
/// The three bivectors (`e₁₂`, `e₁₃`, `e₂₃`) represent oriented planes.
/// They are dual to the three axes and are used to construct rotors
/// (rotation operators). Each bivector squares to `-1`:
///
/// - `e₁₂² = -1` (rotation in xy-plane, around z-axis)
/// - `e₁₃² = -1` (rotation in xz-plane, around y-axis)
/// - `e₂₃² = -1` (rotation in yz-plane, around x-axis)
///
/// # Example
///
/// ```
/// use clifford::prelude::*;
///
/// assert_eq!(Euclidean3::DIM, 3);
/// assert_eq!(Euclidean3::num_blades(), 8);
///
/// // All basis vectors square to +1
/// for i in 0..3 {
///     assert_eq!(Euclidean3::metric(i), 1);
/// }
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Euclidean3;

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

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Euclidean3");
        1
    }
}

/// 4D Euclidean signature: `Cl(4,0,0)`.
///
/// Represents 4D Euclidean space with basis vectors `e₁`, `e₂`, `e₃`, and `e₄`.
///
/// # Basis Blades (16 total)
///
/// | Grade | Count | Description |
/// |-------|-------|-------------|
/// | 0 | 1 | Scalar |
/// | 1 | 4 | Vectors |
/// | 2 | 6 | Bivectors |
/// | 3 | 4 | Trivectors |
/// | 4 | 1 | Pseudoscalar (4-volume) |
///
/// # Applications
///
/// While less common than 3D, 4D Euclidean GA is useful for:
/// - Conformal model preprocessing
/// - Quaternion-like operations with additional structure
/// - Higher-dimensional geometry research
///
/// # Example
///
/// ```
/// use clifford::prelude::*;
///
/// assert_eq!(Euclidean4::DIM, 4);
/// assert_eq!(Euclidean4::num_blades(), 16);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Euclidean4;

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

    const P: usize = 4;
    const Q: usize = 0;
    const R: usize = 0;

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Euclidean4");
        1
    }
}

/// 1D Hyperbolic signature: `Cl(1,0,0)`.
///
/// This is used for hyperbolic numbers (split-complex numbers).
/// With one positive basis vector `e₁` where `e₁² = +1`, this
/// algebra produces hyperbolic numbers `a + bj` where `j² = +1`.
///
/// # Basis Blades (2 total)
///
/// | Index | Blade | Grade | Description |
/// |-------|-------|-------|-------------|
/// | 0 | `1` | 0 | Scalar (real part) |
/// | 1 | `e₁` | 1 | Hyperbolic unit (j) |
///
/// # Properties
///
/// - `e₁² = +1` (unlike complex numbers where `i² = -1`)
/// - Hyperbolic numbers have zero divisors: `(1+j)(1-j) = 0`
/// - The norm uses grade involution: `z * involute(z) = a² - b²`
///
/// # Example
///
/// ```
/// use clifford::signature::{Cl1_0_0, Signature};
///
/// assert_eq!(Cl1_0_0::DIM, 1);
/// assert_eq!(Cl1_0_0::num_blades(), 2);
/// assert_eq!(Cl1_0_0::metric(0), 1);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Cl1_0_0;

impl Signature for Cl1_0_0 {
    type NumBlades = typenum::U2; // 2^1 = 2

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Cl1_0_0");
        1
    }
}

/// Type alias for the hyperbolic numbers signature.
pub type Hyperbolic1 = Cl1_0_0;

/// 1D Anti-Euclidean signature: `Cl(0,1,0)`.
///
/// This is used for complex numbers.
/// With one negative basis vector `e₁` where `e₁² = -1`, this
/// algebra produces complex numbers `a + bi` where `i² = -1`.
///
/// # Basis Blades (2 total)
///
/// | Index | Blade | Grade | Description |
/// |-------|-------|-------|-------------|
/// | 0 | `1` | 0 | Scalar (real part) |
/// | 1 | `e₁` | 1 | Imaginary unit (i) |
///
/// # Properties
///
/// - `e₁² = -1` (like complex numbers)
/// - Complex numbers have no zero divisors
/// - The norm uses Clifford conjugate: `z * conjugate(z) = a² + b²`
///
/// # Example
///
/// ```
/// use clifford::signature::{Cl0_1_0, Signature};
///
/// assert_eq!(Cl0_1_0::DIM, 1);
/// assert_eq!(Cl0_1_0::num_blades(), 2);
/// assert_eq!(Cl0_1_0::metric(0), -1);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Cl0_1_0;

impl Signature for Cl0_1_0 {
    type NumBlades = typenum::U2; // 2^1 = 2

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Cl0_1_0");
        -1
    }
}

/// Type alias for the complex numbers signature.
pub type Complex1 = Cl0_1_0;

/// 2D Negative-definite signature: `Cl(0,2,0)`.
///
/// This is the quaternion algebra. With two basis vectors `e₁, e₂`
/// where `e₁² = e₂² = -1`, this gives the standard quaternion algebra
/// with basis mapping: `i → e₁`, `j → e₂`, `k → e₁e₂`.
///
/// # Basis Blades (4 total)
///
/// | Index | Blade | Grade | Description |
/// |-------|-------|-------|-------------|
/// | 0 | `1` | 0 | Scalar (real part, w) |
/// | 1 | `e₁` | 1 | Imaginary i |
/// | 2 | `e₂` | 1 | Imaginary j |
/// | 3 | `e₁₂` | 2 | Imaginary k = ij |
///
/// # Properties
///
/// - `e₁² = e₂² = -1`
/// - `e₁e₂ = -e₂e₁` (anti-commutative)
/// - Quaternions have no zero divisors (division algebra)
/// - The norm uses Clifford conjugate: `q * conjugate(q) = w² + x² + y² + z²`
///
/// # Example
///
/// ```
/// use clifford::signature::{Cl0_2_0, Signature};
///
/// assert_eq!(Cl0_2_0::DIM, 2);
/// assert_eq!(Cl0_2_0::num_blades(), 4);
/// assert_eq!(Cl0_2_0::metric(0), -1);
/// assert_eq!(Cl0_2_0::metric(1), -1);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Cl0_2_0;

impl Signature for Cl0_2_0 {
    type NumBlades = typenum::U4; // 2^2 = 4

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Cl0_2_0");
        -1
    }
}

/// Type alias for the quaternion signature.
pub type Quaternion2 = Cl0_2_0;

/// 2D Indefinite signature: `Cl(1,1,0)`.
///
/// This is the Minkowski plane (2D spacetime algebra). With one
/// spacelike basis vector `e₁` (e₁² = +1) and one timelike basis
/// vector `e₂` (e₂² = -1), this gives a 2D relativistic algebra.
///
/// # Basis Blades (4 total)
///
/// | Index | Blade | Grade | Description |
/// |-------|-------|-------|-------------|
/// | 0 | `1` | 0 | Scalar |
/// | 1 | `e₁` | 1 | Spacelike vector |
/// | 2 | `e₂` | 1 | Timelike vector |
/// | 3 | `e₁₂` | 2 | Bivector (pseudoscalar) |
///
/// # Properties
///
/// - `e₁² = +1` (spacelike)
/// - `e₂² = -1` (timelike)
/// - Indefinite norm: v² can be positive, negative, or zero
/// - Null vectors exist: e.g., `e₁ + e₂` has v² = 0
/// - Even subalgebra is isomorphic to hyperbolic numbers
///
/// # Example
///
/// ```
/// use clifford::signature::{Cl1_1_0, Signature};
///
/// assert_eq!(Cl1_1_0::DIM, 2);
/// assert_eq!(Cl1_1_0::num_blades(), 4);
/// assert_eq!(Cl1_1_0::metric(0), 1);   // e1^2 = +1
/// assert_eq!(Cl1_1_0::metric(1), -1);  // e2^2 = -1
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Cl1_1_0;

impl Signature for Cl1_1_0 {
    type NumBlades = typenum::U4; // 2^2 = 4

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Cl1_1_0");
        match i {
            0 => 1,  // e1² = +1 (spacelike)
            1 => -1, // e2² = -1 (timelike)
            _ => unreachable!(),
        }
    }
}

/// Type alias for the Minkowski plane signature.
pub type Minkowski2 = Cl1_1_0;

/// 3D Indefinite signature: `Cl(2,1,0)`.
///
/// This is the hyperbolic plane (Lobachevsky plane) algebra. With two
/// spacelike basis vectors `e₁, e₂` (e₁² = e₂² = +1) and one timelike
/// basis vector `e₃` (e₃² = -1), this gives the algebra for 2D hyperbolic
/// geometry.
///
/// # Basis Blades (8 total)
///
/// | Index | Blade | Grade | Square | Description |
/// |-------|-------|-------|--------|-------------|
/// | 0 | `1` | 0 | +1 | Scalar |
/// | 1 | `e₁` | 1 | +1 | Spacelike vector |
/// | 2 | `e₂` | 1 | +1 | Spacelike vector |
/// | 3 | `e₁₂` | 2 | -1 | Rotation bivector |
/// | 4 | `e₃` | 1 | -1 | Timelike vector |
/// | 5 | `e₁₃` | 2 | +1 | Boost bivector |
/// | 6 | `e₂₃` | 2 | +1 | Boost bivector |
/// | 7 | `e₁₂₃` | 3 | +1 | Pseudoscalar |
///
/// # Properties
///
/// - `e₁² = e₂² = +1` (spacelike)
/// - `e₃² = -1` (timelike)
/// - Indefinite norm: elements can be spacelike, timelike, or null
/// - Used for 2D hyperbolic geometry (negative curvature)
///
/// # Causal Structure
///
/// Points (grade-1) are classified by their norm:
/// - Ordinary (timelike): x² + y² - t² < 0
/// - Ideal (null): x² + y² - t² = 0
/// - Ultra-ideal (spacelike): x² + y² - t² > 0
///
/// # Example
///
/// ```
/// use clifford::signature::{Cl2_1_0, Signature};
///
/// assert_eq!(Cl2_1_0::DIM, 3);
/// assert_eq!(Cl2_1_0::num_blades(), 8);
/// assert_eq!(Cl2_1_0::metric(0), 1);   // e1^2 = +1
/// assert_eq!(Cl2_1_0::metric(1), 1);   // e2^2 = +1
/// assert_eq!(Cl2_1_0::metric(2), -1);  // e3^2 = -1
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Cl2_1_0;

impl Signature for Cl2_1_0 {
    type NumBlades = typenum::U8; // 2^3 = 8

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Cl2_1_0");
        match i {
            0 => 1,  // e1² = +1 (spacelike)
            1 => 1,  // e2² = +1 (spacelike)
            2 => -1, // e3² = -1 (timelike)
            _ => unreachable!(),
        }
    }
}

/// Type alias for the hyperbolic plane signature.
pub type Hyperbolic2 = Cl2_1_0;

/// 4D Indefinite signature: `Cl(3,1,0)`.
///
/// This is the Minkowski spacetime algebra (spacetime algebra, STA). With three
/// spacelike basis vectors `e₁, e₂, e₃` (e₁² = e₂² = e₃² = +1) and one timelike
/// basis vector `e₄` (e₄² = -1), this gives the algebra for special relativity.
///
/// # Basis Blades (16 total)
///
/// | Grade | Count | Description |
/// |-------|-------|-------------|
/// | 0 | 1 | Scalar |
/// | 1 | 4 | Spacetime vectors |
/// | 2 | 6 | Bivectors (rotations + boosts) |
/// | 3 | 4 | Trivectors |
/// | 4 | 1 | Pseudoscalar (4-volume) |
///
/// # Properties
///
/// - `e₁² = e₂² = e₃² = +1` (spacelike)
/// - `e₄² = -1` (timelike)
/// - Indefinite norm: elements can be spacelike, timelike, or null
/// - Used for special relativity and electromagnetic theory
///
/// # Causal Structure
///
/// Vectors (grade-1) are classified by their norm:
/// - Timelike: v² < 0 (massive particles, observers)
/// - Null: v² = 0 (light rays, photons)
/// - Spacelike: v² > 0 (spatial separations)
///
/// # Example
///
/// ```
/// use clifford::signature::{Cl3_1_0, Signature};
///
/// assert_eq!(Cl3_1_0::DIM, 4);
/// assert_eq!(Cl3_1_0::num_blades(), 16);
/// assert_eq!(Cl3_1_0::metric(0), 1);   // e1^2 = +1
/// assert_eq!(Cl3_1_0::metric(1), 1);   // e2^2 = +1
/// assert_eq!(Cl3_1_0::metric(2), 1);   // e3^2 = +1
/// assert_eq!(Cl3_1_0::metric(3), -1);  // e4^2 = -1
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Cl3_1_0;

impl Signature for Cl3_1_0 {
    type NumBlades = typenum::U16; // 2^4 = 16

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Cl3_1_0");
        match i {
            0 => 1,  // e1² = +1 (spacelike)
            1 => 1,  // e2² = +1 (spacelike)
            2 => 1,  // e3² = +1 (spacelike)
            3 => -1, // e4² = -1 (timelike)
            _ => unreachable!(),
        }
    }
}

/// Type alias for the Minkowski spacetime signature.
pub type Minkowski3 = Cl3_1_0;

/// 1D Degenerate signature: `Cl(0,0,1)`.
///
/// This is used for dual numbers (automatic differentiation).
/// With one null basis vector `e₁` where `e₁² = 0`, this
/// algebra produces dual numbers `a + bε` where `ε² = 0`.
///
/// # Basis Blades (2 total)
///
/// | Index | Blade | Grade | Description |
/// |-------|-------|-------|-------------|
/// | 0 | `1` | 0 | Scalar (real part) |
/// | 1 | `e₁` | 1 | Dual unit (ε, nilpotent) |
///
/// # Properties
///
/// - `e₁² = 0` (nilpotent)
/// - Dual numbers have zero divisors: all `bε` have zero norm
/// - The norm is degenerate: `z * involute(z) = a²`
/// - Foundation for forward-mode automatic differentiation
///
/// # Example
///
/// ```
/// use clifford::signature::{Cl0_0_1, Signature};
///
/// assert_eq!(Cl0_0_1::DIM, 1);
/// assert_eq!(Cl0_0_1::num_blades(), 2);
/// assert_eq!(Cl0_0_1::metric(0), 0);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Cl0_0_1;

impl Signature for Cl0_0_1 {
    type NumBlades = typenum::U2; // 2^1 = 2

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Cl0_0_1");
        0
    }
}

/// Type alias for the dual numbers signature.
pub type Dual1 = Cl0_0_1;

/// 3D Mixed degenerate signature: `Cl(0,2,1)`.
///
/// This is the dual quaternion algebra. With two basis vectors `e₁, e₂`
/// squaring to -1 (quaternion part) and one null basis vector `e₃`
/// (dual part), this gives dual quaternions for rigid transformations.
///
/// # Basis Blades (8 total)
///
/// | Index | Blade | Grade | Description |
/// |-------|-------|-------|-------------|
/// | 0 | `1` | 0 | Scalar |
/// | 1 | `e₁` | 1 | Quaternion i |
/// | 2 | `e₂` | 1 | Quaternion j |
/// | 3 | `e₃` | 1 | Dual unit ε |
/// | 4 | `e₁₂` | 2 | Quaternion k |
/// | 5 | `e₁₃` | 2 | Dual of i |
/// | 6 | `e₂₃` | 2 | Dual of j |
/// | 7 | `e₁₂₃` | 3 | Dual of k (pseudoscalar) |
///
/// # Properties
///
/// - `e₁² = e₂² = -1` (quaternion basis)
/// - `e₃² = 0` (dual/nilpotent)
/// - Dual quaternions represent rigid transformations (rotation + translation)
/// - Unit dual quaternions: `q * conjugate(q) = 1`
///
/// # Example
///
/// ```
/// use clifford::signature::{Cl0_2_1, Signature};
///
/// assert_eq!(Cl0_2_1::DIM, 3);
/// assert_eq!(Cl0_2_1::num_blades(), 8);
/// assert_eq!(Cl0_2_1::metric(0), -1);  // e1^2 = -1
/// assert_eq!(Cl0_2_1::metric(1), -1);  // e2^2 = -1
/// assert_eq!(Cl0_2_1::metric(2), 0);   // e3^2 = 0
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Cl0_2_1;

impl Signature for Cl0_2_1 {
    type NumBlades = typenum::U8; // 2^3 = 8

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

    #[inline]
    fn metric(i: usize) -> i8 {
        debug_assert!(i < Self::DIM, "basis index {i} out of range for Cl0_2_1");
        match i {
            0 => -1, // e1² = -1 (quaternion i)
            1 => -1, // e2² = -1 (quaternion j)
            2 => 0,  // e3² = 0 (dual unit)
            _ => unreachable!(),
        }
    }
}

/// Type alias for the dual quaternion signature.
pub type DualQuaternion3 = Cl0_2_1;

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

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

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

        #[test]
        fn euclidean4_metric_always_positive(i in 0usize..4) {
            prop_assert_eq!(Euclidean4::metric(i), 1);
        }
    }

    #[test]
    fn euclidean_dimensions() {
        assert_eq!(Euclidean2::DIM, 2);
        assert_eq!(Euclidean2::num_blades(), 4);

        assert_eq!(Euclidean3::DIM, 3);
        assert_eq!(Euclidean3::num_blades(), 8);

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

    #[test]
    fn euclidean_pqr_consistency() {
        assert_eq!(
            Euclidean2::P + Euclidean2::Q + Euclidean2::R,
            Euclidean2::DIM
        );
        assert_eq!(
            Euclidean3::P + Euclidean3::Q + Euclidean3::R,
            Euclidean3::DIM
        );
        assert_eq!(
            Euclidean4::P + Euclidean4::Q + Euclidean4::R,
            Euclidean4::DIM
        );
    }
}