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
//! Ring theory abstractions and implementations.
//!
//! This module provides traits and implementations for ring theory concepts,
//! including both general rings and fields (which are special types of rings).
use *;
use crateAbelianGroup;
/// A trait representing a mathematical ring.
///
/// A ring is a set equipped with two binary operations (addition and multiplication)
/// satisfying properties analogous to those of addition and multiplication of integers.
/// This trait combines the requirements for an Abelian group with multiplicative properties.
/// A trait representing a mathematical field.
///
/// A field is a set on which addition, subtraction, multiplication, and division
/// are defined and behave as the corresponding operations on rational and real numbers.
/// Every non-zero element has a multiplicative inverse.
impl_ring!;
impl_ring!;
impl_ring!;
impl_ring!;
impl_ring!;
impl_ring!;
impl_ring!;
impl_ring!;
impl_field!;
impl_field!;
/// A trait representing a mathematical semiring.
///
/// A semiring is a set equipped with two binary operations (addition and multiplication)
/// satisfying properties of distributivity and associativity analogous to those of addition and
/// multiplication of integers. This trait combines the requirements for an Abelian monoid with
/// multiplicative properties.
///
/// # Requirements
///
/// A semiring (R, +, ·) must satisfy:
/// 1. (R, +) is a commutative monoid with identity element 0
/// 2. (R, ·) is a monoid with identity element 1
/// 3. Multiplication distributes over addition:
/// - Left distributivity: a·(b + c) = a·b + a·c
/// - Right distributivity: (a + b)·c = a·c + b·c
/// 4. Multiplication by 0 annihilates R: 0·a = a·0 = 0
///
/// # Implementation Notes
///
/// The distributive properties are enforced by the combination of the `Additive` and
/// `Multiplicative` traits. Implementors must ensure that their implementations satisfy these
/// properties. Semirings are not groups because they do not have additive inverses.
///
/// If you want a structure with an additive inverse, use the Ring trait instead, since it
/// has the abelian group trait bound. If you only need addition to be associative and commutative
/// (but without an additive identity), use the semiring trait.
///
/// # Examples
///
/// Common examples of semirings include:
/// - Natural numbers (ℕ, +, ×)
/// - Tropical semiring (ℝ ∪ {∞}, min, +)
/// - Probability semiring (ℝ₊, +, ×)