noether 0.3.0

Abstract algebraic structures for Rust
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
//! Field theory structures.
//!
//! This module defines traits for algebraic structures related to fields,
//! including Euclidean domains, fields, and field extensions.

use crate::groups::MultiplicativeAbelianGroup;
use crate::rings::PrincipalIdealDomain;
use crate::spaces::VectorSpace;
use num_traits::{Euclid, One, Zero};

/// Represents a Euclidean Domain, an integral domain with a Euclidean function.
///
/// # Mathematical Definition
/// A Euclidean Domain (R, +, ·, φ) is a principal ideal domain equipped with a
/// Euclidean function φ: R\{0} → ℕ₀ that satisfies certain properties.
///
/// # Formal Definition
/// Let (R, +, ·) be an integral domain and φ: R\{0} → ℕ₀ a function. R is a Euclidean domain if:
/// 1. ∀a, b ∈ R, b ≠ 0, ∃!q, r ∈ R : a = bq + r ∧ (r = 0 ∨ φ(r) < φ(b)) (Division with Remainder)
/// 2. ∀a, b ∈ R\{0} : φ(a) ≤ φ(ab) (Multiplicative Property)
pub trait EuclideanDomain: PrincipalIdealDomain + Euclid {}

/// Represents a Field, a commutative ring where every non-zero element has a multiplicative inverse.
///
/// # Mathematical Definition
/// A field (F, +, ·) is a commutative ring where:
/// - Every non-zero element has a multiplicative inverse
///
/// # Formal Definition
/// Let (F, +, ·) be a field. Then:
/// 1. (F, +, ·) is a commutative ring
/// 2. ∀ a ∈ F, a ≠ 0, ∃ a⁻¹ ∈ F, a · a⁻¹ = a⁻¹ · a = 1 (multiplicative inverse)
pub trait Field: EuclideanDomain + MultiplicativeAbelianGroup {}

/// Represents a Finite Field, a field with a finite number of elements.
///
/// # Mathematical Definition
/// A finite field is a field with a finite number of elements.
///
/// # Properties
/// - The number of elements is always a prime power p^n
pub trait FiniteField: Field {
    ///Allows for an arbitrary size representation without specificing type
    type ScalarType: Clone + PartialOrd + Zero + One;

    /// Returns the characteristic of the field.
    fn characteristic() -> u64;

    /// Returns the number of elements in the field.
    fn order() -> u64;
}

/// Represents an Ordered Field, a field with a total order compatible with its operations.
///
/// # Mathematical Definition
/// An ordered field is a field equipped with a total order ≤ where:
/// - If a ≤ b then a + c ≤ b + c for all c
/// - If 0 ≤ a and 0 ≤ b then 0 ≤ a · b
pub trait OrderedField: Field + PartialOrd {}

/// Represents a Real Field, a complete ordered field.
///
/// # Mathematical Definition
/// A real field is an ordered field that is Dedekind-complete:
/// - Every non-empty subset with an upper bound has a least upper bound
pub trait RealField: OrderedField {}

/// Represents a Field Extension.
///
/// # Mathematical Definition
/// A field extension L/K is a field L that contains K as a subfield.
///
/// # Properties
/// - L is a field
/// - K is a subfield of L
/// - L is a vector space over K
pub trait FieldExtension: Field + VectorSpace<Self::BaseField> {
    /// The base field of this extension.
    type BaseField: Field;

    /// Returns the degree of the field extension.
    fn degree() -> usize;

    /// Computes the trace of an element.
    fn trace(&self) -> Self::BaseField;

    /// Computes the norm of an element.
    fn norm(&self) -> Self::BaseField;
}

/// Represents a Tower of Field Extensions.
///
/// # Mathematical Definition
/// A tower of field extensions is a sequence of fields K = F₀ ⊂ F₁ ⊂ ... ⊂ Fₙ = L
/// where each Fᵢ₊₁/Fᵢ is a field extension.
///
/// # Properties
/// - Each level is a field extension of the previous level
/// - The composition of the extensions forms the overall extension L/K
/// - The degree of L/K is the product of the degrees of each extension in the tower
pub trait FieldExtensionTower: FieldExtension {
    /// Returns the number of extensions in the tower.
    fn tower_height() -> usize;

    /// Returns the degree of the i-th extension in the tower.
    fn extension_degree(i: usize) -> usize;
}

// Blanket implementations
impl<T: PrincipalIdealDomain + Euclid> EuclideanDomain for T {}
impl<T: EuclideanDomain + MultiplicativeAbelianGroup> Field for T {}
impl<T: Field + PartialOrd> OrderedField for T {}

// RealField
// Note: This cannot be implemented as a blanket impl because it requires knowledge about completeness

// FieldExtension
// Note: This cannot be implemented as a blanket impl because it requires specific knowledge about the base field and extension structure

// FieldExtensionTower
// Note: This cannot be implemented as a blanket impl because it requires specific knowledge about the tower structure

#[cfg(test)]
mod tests {
    use super::*;
    use crate::operations::{
        AssociativeAddition, AssociativeMultiplication, CommutativeAddition,
        CommutativeMultiplication, Distributive,
    };
    use num_traits::{Inv, One, Zero};
    use std::cmp::Ordering;
    use std::ops::{
        Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
    };

    // Implement a simple finite field (GF(5) - integers modulo 5)
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    struct GF5(u8);

    impl GF5 {
        // Create a new element, ensuring it is normalized into the range [0, 4]
        fn new(value: u8) -> Self {
            Self(value % 5)
        }
    }

    // Additive operations for GF5
    impl Add for GF5 {
        type Output = Self;

        fn add(self, rhs: Self) -> Self::Output {
            Self::new(self.0 + rhs.0)
        }
    }

    impl AddAssign for GF5 {
        fn add_assign(&mut self, rhs: Self) {
            *self = *self + rhs;
        }
    }

    impl Sub for GF5 {
        type Output = Self;

        fn sub(self, rhs: Self) -> Self::Output {
            // Add 5 to handle the case where self.value < rhs.value
            Self::new(self.0 + 5 - rhs.0)
        }
    }

    impl SubAssign for GF5 {
        fn sub_assign(&mut self, rhs: Self) {
            *self = *self - rhs;
        }
    }

    impl Neg for GF5 {
        type Output = Self;

        fn neg(self) -> Self::Output {
            if self.0 == 0 {
                Self(0) // -0 = 0
            } else {
                Self(5 - self.0)
            }
        }
    }

    impl Zero for GF5 {
        fn zero() -> Self {
            Self(0)
        }

        fn is_zero(&self) -> bool {
            self.0 == 0
        }
    }

    // Multiplicative operations for GF5
    impl Mul for GF5 {
        type Output = Self;

        fn mul(self, rhs: Self) -> Self::Output {
            Self::new(self.0 * rhs.0)
        }
    }

    impl MulAssign for GF5 {
        fn mul_assign(&mut self, rhs: Self) {
            *self = *self * rhs;
        }
    }

    impl One for GF5 {
        fn one() -> Self {
            Self(1)
        }
    }

    // Multiplicative inverses in GF(5)
    impl Inv for GF5 {
        type Output = Self;

        fn inv(self) -> Self::Output {
            match self.0 {
                0 => panic!("Division by zero in GF(5)"),
                1 => Self(1), // 1*1 ≡ 1 (mod 5)
                2 => Self(3), // 2*3 ≡ 6 ≡ 1 (mod 5)
                3 => Self(2), // 3*2 ≡ 6 ≡ 1 (mod 5)
                4 => Self(4), // 4*4 ≡ 16 ≡ 1 (mod 5)
                _ => unreachable!("All values should be normalized to 0-4"),
            }
        }
    }

    impl Div for GF5 {
        type Output = Self;

        fn div(self, rhs: Self) -> Self::Output {
            if rhs.0 == 0 {
                panic!("Division by zero in GF(5)");
            }
            self * rhs.inv()
        }
    }

    impl DivAssign for GF5 {
        fn div_assign(&mut self, rhs: Self) {
            *self = *self / rhs;
        }
    }

    impl Rem for GF5 {
        type Output = Self;

        fn rem(self, rhs: Self) -> Self::Output {
            if rhs.0 == 0 {
                panic!("Remainder by zero in GF(5)");
            }
            Self(self.0 % rhs.0)
        }
    }

    impl RemAssign for GF5 {
        fn rem_assign(&mut self, rhs: Self) {
            if rhs.0 == 0 {
                panic!("Remainder by zero in GF(5)");
            }
            *self = Self(self.0 % rhs.0);
        }
    }

    // Implement Euclid trait for GF5
    impl Euclid for GF5 {
        fn div_euclid(&self, v: &Self) -> Self {
            if v.0 == 0 {
                panic!("Division by zero in GF(5)");
            }
            *self / *v
        }

        fn rem_euclid(&self, v: &Self) -> Self {
            if v.0 == 0 {
                panic!("Remainder by zero in GF(5)");
            }
            Self(self.0 % v.0)
        }
    }

    // Marker traits for GF5
    impl CommutativeAddition for GF5 {}
    impl AssociativeAddition for GF5 {}
    impl CommutativeMultiplication for GF5 {}
    impl AssociativeMultiplication for GF5 {}
    impl Distributive for GF5 {}

    impl PartialOrd for GF5 {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            self.0.partial_cmp(&other.0)
        }
    }

    // Implement FiniteField for GF5
    impl FiniteField for GF5 {
        type ScalarType = u8;

        fn characteristic() -> u64 {
            5
        }

        fn order() -> u64 {
            5
        }
    }

    // Implement a simple ordered field based on rational numbers
    #[derive(Debug, Clone, Copy, PartialEq)]
    struct Rational {
        num: i32,
        den: i32,
    }

    impl Rational {
        // Create a new rational number in simplified form
        fn new(num: i32, den: i32) -> Self {
            if den == 0 {
                panic!("Denominator cannot be zero");
            }

            // Ensure the denominator is positive
            let (num, den) = if den < 0 { (-num, -den) } else { (num, den) };

            // Simplify the fraction using GCD
            let gcd = Self::gcd(num.abs(), den);
            Self {
                num: num / gcd,
                den: den / gcd,
            }
        }

        // Compute the greatest common divisor using Euclidean algorithm
        fn gcd(a: i32, b: i32) -> i32 {
            if b == 0 {
                a
            } else {
                Self::gcd(b, a % b)
            }
        }
    }

    // Additive operations for Rational
    impl Add for Rational {
        type Output = Self;

        fn add(self, rhs: Self) -> Self::Output {
            Self::new(self.num * rhs.den + rhs.num * self.den, self.den * rhs.den)
        }
    }

    impl AddAssign for Rational {
        fn add_assign(&mut self, rhs: Self) {
            *self = *self + rhs;
        }
    }

    impl Sub for Rational {
        type Output = Self;

        fn sub(self, rhs: Self) -> Self::Output {
            Self::new(self.num * rhs.den - rhs.num * self.den, self.den * rhs.den)
        }
    }

    impl SubAssign for Rational {
        fn sub_assign(&mut self, rhs: Self) {
            *self = *self - rhs;
        }
    }

    impl Neg for Rational {
        type Output = Self;

        fn neg(self) -> Self::Output {
            Self::new(-self.num, self.den)
        }
    }

    impl Zero for Rational {
        fn zero() -> Self {
            Self::new(0, 1)
        }

        fn is_zero(&self) -> bool {
            self.num == 0
        }
    }

    // Multiplicative operations for Rational
    impl Mul for Rational {
        type Output = Self;

        fn mul(self, rhs: Self) -> Self::Output {
            Self::new(self.num * rhs.num, self.den * rhs.den)
        }
    }

    impl MulAssign for Rational {
        fn mul_assign(&mut self, rhs: Self) {
            *self = *self * rhs;
        }
    }

    impl One for Rational {
        fn one() -> Self {
            Self::new(1, 1)
        }
    }

    impl Inv for Rational {
        type Output = Self;

        fn inv(self) -> Self::Output {
            if self.num == 0 {
                panic!("Division by zero");
            }
            Self::new(self.den, self.num)
        }
    }

    impl Div for Rational {
        type Output = Self;

        fn div(self, rhs: Self) -> Self::Output {
            if rhs.num == 0 {
                panic!("Division by zero");
            }
            Self::new(self.num * rhs.den, self.den * rhs.num)
        }
    }

    impl DivAssign for Rational {
        fn div_assign(&mut self, rhs: Self) {
            *self = *self / rhs;
        }
    }

    impl Rem for Rational {
        type Output = Self;

        fn rem(self, rhs: Self) -> Self::Output {
            if rhs.num == 0 {
                panic!("Remainder by zero");
            }
            // a % b = a - (a / b).floor() * b
            let quotient = (self.num as f64 * rhs.den as f64) / (self.den as f64 * rhs.num as f64);
            let whole_part = Self::new(quotient.floor() as i32, 1);
            self - whole_part * rhs
        }
    }

    impl RemAssign for Rational {
        fn rem_assign(&mut self, rhs: Self) {
            *self = *self % rhs;
        }
    }

    // Euclidean functions for Rational
    impl Euclid for Rational {
        fn div_euclid(&self, v: &Self) -> Self {
            if v.num == 0 {
                panic!("Division by zero");
            }
            // For rationals, we define Euclidean division as rounding down
            let quotient = (self.num as f64 * v.den as f64) / (self.den as f64 * v.num as f64);
            Self::new(quotient.floor() as i32, 1)
        }

        fn rem_euclid(&self, v: &Self) -> Self {
            if v.num == 0 {
                panic!("Remainder by zero");
            }
            let quotient = self.div_euclid(v);
            *self - quotient * *v
        }
    }

    // Partial ordering for Rational
    impl PartialOrd for Rational {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            // Compare a/b and c/d by computing a*d vs b*c
            let left = self.num as i64 * other.den as i64;
            let right = self.den as i64 * other.num as i64;
            left.partial_cmp(&right)
        }
    }

    // Marker traits for Rational
    impl CommutativeAddition for Rational {}
    impl AssociativeAddition for Rational {}
    impl CommutativeMultiplication for Rational {}
    impl AssociativeMultiplication for Rational {}
    impl Distributive for Rational {}

    #[test]
    fn test_euclidean_domain() {
        fn assert_is_euclidean_domain<T: EuclideanDomain>(_: &T) {}

        // Test GF5 implements EuclideanDomain
        assert_is_euclidean_domain(&GF5::new(2));

        // Test Rational implements EuclideanDomain
        assert_is_euclidean_domain(&Rational::new(3, 4));

        // Test primitive float implements EuclideanDomain
        assert_is_euclidean_domain(&std::f64::consts::PI);

        // Test euclidean division in GF5
        let a = GF5::new(7); // 7 mod 5 = 2
        let b = GF5::new(3); // 3

        // In GF5: 2 / 3 = 2 * inverse(3) = 2 * 2 = 4
        assert_eq!(a.div_euclid(&b), GF5::new(4));

        // In GF5: 2 % 3 = 2 (since 3 * 4 + 2 = 14 ≡ 4 mod 5)
        // But this is more complex - for simplicity just ensuring it's defined
        let _ = a.rem_euclid(&b);

        // Test euclidean division in Rational
        let p = Rational::new(7, 2); // 7/2 = 3.5
        let q = Rational::new(2, 1); // 2

        // 3.5 divided by 2 gives quotient 1 (floor of 1.75) and remainder 1.5
        assert_eq!(p.div_euclid(&q), Rational::new(1, 1));
        assert_eq!(p.rem_euclid(&q), Rational::new(3, 2));
    }

    #[test]
    fn test_field() {
        fn assert_is_field<T: Field>(_: &T) {}

        // Test GF5 implements Field
        assert_is_field(&GF5::new(2));

        // Test Rational implements Field
        assert_is_field(&Rational::new(3, 4));

        // Test primitive float implements Field
        assert_is_field(&std::f64::consts::PI);

        // Test field properties in GF5
        let a = GF5::new(2);
        let b = GF5::new(3);

        // Addition is commutative
        assert_eq!(a + b, b + a);

        // Multiplication is commutative
        assert_eq!(a * b, b * a);

        // Distributivity
        let c = GF5::new(4);
        assert_eq!(a * (b + c), a * b + a * c);

        // Multiplicative inverse
        assert_eq!(a * a.inv(), GF5::one());
        assert_eq!(b * b.inv(), GF5::one());

        // Division
        assert_eq!(a / b, a * b.inv());

        // Test field properties in Rational
        let p = Rational::new(3, 4); // 3/4
        let q = Rational::new(2, 5); // 2/5

        // Addition is commutative
        assert_eq!(p + q, q + p);

        // Multiplication is commutative
        assert_eq!(p * q, q * p);

        // Distributivity
        let r = Rational::new(1, 2); // 1/2
        assert_eq!(p * (q + r), p * q + p * r);

        // Multiplicative inverse
        assert_eq!(p * p.inv(), Rational::one());
        assert_eq!(q * q.inv(), Rational::one());

        // Division
        assert_eq!(p / q, p * q.inv());
    }

    #[test]
    fn test_finite_field() {
        fn assert_is_finite_field<T: FiniteField>(_: &T) {}

        // Test GF5 implements FiniteField
        assert_is_finite_field(&GF5::new(2));

        // Test field properties
        assert_eq!(GF5::characteristic(), 5);
        assert_eq!(GF5::order(), 5);

        // Test all field elements
        let elements: Vec<GF5> = (0..5).map(GF5::new).collect();

        // Verify closure under operations
        for &a in &elements {
            for &b in &elements {
                if !b.is_zero() {
                    let sum = a + b;
                    let product = a * b;
                    let quotient = a / b;

                    // Results should be in the field
                    assert!(elements.contains(&sum));
                    assert!(elements.contains(&product));
                    assert!(elements.contains(&quotient));
                }
            }
        }

        // Verify that every non-zero element has a unique multiplicative inverse
        let mut seen_inverses = Vec::new();
        for &a in &elements {
            if !a.is_zero() {
                let a_inv = a.inv();
                assert!(!seen_inverses.contains(&a_inv));
                seen_inverses.push(a_inv);

                // Verify inverse property
                assert_eq!(a * a_inv, GF5::one());
            }
        }
    }

    #[test]
    fn test_ordered_field() {
        fn assert_is_ordered_field<T: OrderedField>(_: &T) {}

        // Test Rational implements OrderedField
        assert_is_ordered_field(&Rational::new(3, 4));

        // Test primitive float implements OrderedField
        assert_is_ordered_field(&std::f64::consts::PI);

        // Test ordering properties of Rational
        let a = Rational::new(3, 4); // 3/4
        let b = Rational::new(2, 3); // 2/3
        let c = Rational::new(1, 2); // 1/2

        // Basic comparison
        assert!(a > b && b > c);
        assert!(a > c);

        // Order compatibility with addition
        let d = Rational::new(1, 10); // 1/10
        assert!(a > b);
        assert!(a + d > b + d);

        // Order compatibility with positive multiplication
        let pos = Rational::new(2, 1); // 2
        assert!(a > c);
        assert!(a * pos > c * pos);

        // Negative numbers and order
        let neg = Rational::new(-2, 1); // -2
        assert!(a * neg < c * neg); // -3/2 < -1
    }

    #[test]
    fn test_field_axioms() {
        // Test the field axioms using GF5
        let elements: Vec<GF5> = (0..5).map(GF5::new).collect();
        let non_zero: Vec<GF5> = elements.iter().filter(|e| !e.is_zero()).cloned().collect();

        let zero = GF5::zero();
        let one = GF5::one();

        // 1. Additive identity: a + 0 = a
        for &a in &elements {
            assert_eq!(a + zero, a);
        }

        // 2. Additive inverse: a + (-a) = 0
        for &a in &elements {
            assert_eq!(a + (-a), zero);
        }

        // 3. Multiplicative identity: a * 1 = a
        for &a in &elements {
            assert_eq!(a * one, a);
        }

        // 4. Multiplicative inverse: a * a^(-1) = 1 for all a ≠ 0
        for &a in &non_zero {
            assert_eq!(a * a.inv(), one);
        }

        // 5. Addition is associative: (a + b) + c = a + (b + c)
        for &a in &elements {
            for &b in &elements {
                for &c in &elements {
                    assert_eq!((a + b) + c, a + (b + c));
                }
            }
        }

        // 6. Addition is commutative: a + b = b + a
        for &a in &elements {
            for &b in &elements {
                assert_eq!(a + b, b + a);
            }
        }

        // 7. Multiplication is associative: (a * b) * c = a * (b * c)
        for &a in &elements {
            for &b in &elements {
                for &c in &elements {
                    assert_eq!((a * b) * c, a * (b * c));
                }
            }
        }

        // 8. Multiplication is commutative: a * b = b * a
        for &a in &elements {
            for &b in &elements {
                assert_eq!(a * b, b * a);
            }
        }

        // 9. Distributivity: a * (b + c) = a * b + a * c
        for &a in &elements {
            for &b in &elements {
                for &c in &elements {
                    assert_eq!(a * (b + c), a * b + a * c);
                }
            }
        }
    }
}