Interface Semiring<T extends Semiring<T>>
- Type Parameters:
T
- the type of the semiring element.
- All Superinterfaces:
Comparable<T>
,Serializable
- All Known Implementing Classes:
BoolSemiring
,Complex128
,Complex64
,RealFloat32
,RealFloat64
,RealInt16
,RealInt32
A semiring is an algebraic structure consisting of a set R equipped with two binary operations:
addition (+) and multiplication (*). The semiring generalizes the concept of a ring
but does not require the
existence of additive inverses (i.e., negative elements). As a result, subtraction is not generally defined within a semiring.
Formal Definition:
For all elements a
, b
, and c
in R, the following properties must hold:
- Addition is associative:
a + (b + c) = (a + b) + c
- Addition is commutative:
a + b = b + a
- Additive identity exists: There exists an element
0
in R such thata + 0 = a
- Multiplication is associative:
a * (b * c) = (a * b) * c
- Multiplicative identity exists: There exists an element
1
in R such thata * 1 = a
- Multiplication distributes over addition:
- Left distributivity:
a * (b + c) = (a * b) + (a * c)
- Right distributivity:
(a + b) * c = (a * c) + (b * c)
- Left distributivity:
- Zero is absorbing for multiplication:
a * 0 = 0 * a = 0
Implementations:
Implementations of the Semiring
interface should ensure that instances are immutable. This means
that all operations should return new instances rather than modifying existing ones. Immutability guarantees
thread safety and consistent behavior across different contexts.
The compareTo(Semiring)
method should implement some ordering (total or partial) on the semiring.
Further, implementations should ensure that the equality and hash code methods are consistent with the semiring's equality definition.
Examples of Semirings:
- Natural Numbers (ℕ): The set of natural numbers with usual addition and multiplication forms a semiring.
- Boolean Semiring: The set {
false
,true
} with logical OR as addition and logical AND as multiplication. - Tropical Semiring: The set of real numbers extended with infinity, where addition is defined as taking the minimum (or maximum) and multiplication as the standard real number addition.
Interface Methods:
The Semiring
interface specifies the following methods that semiring elements must implement:
add(Semiring)
: Performs the addition operation, returning a new semiring element.mult(Semiring)
: Performs the multiplication operation, returning a new semiring element.isZero()
: Checks if the element is the additive identity (zero element).isOne()
: Checks if the element is the multiplicative identity (one element).getZero()
: Returns the additive identity element of the semiring.getOne()
: Returns the multiplicative identity element of the semiring.
Usage Example:
// Assume T is a concrete implementation of Semiring<T>
T a = ...; // Initialize element a
T b = ...; // Initialize element b
T sum = a.add(b); // Perform addition in the semiring
T product = a.mult(b); // Perform multiplication in the semiring
boolean isZero = a.isZero(); // Check if a is the additive identity
boolean isOne = b.isOne(); // Check if b is the multiplicative identity
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionSums two elements of this semiring (associative and commutative).int
Compares this element of the semiring withb
.double
Converts this semiring value to an equivalent double value.getOne()
Gets the multiplicative identity for this semiring.getZero()
Gets the additive identity for this semiring.boolean
isOne()
Checks if this value is a multiplicative identity for this semiring.boolean
isZero()
Checks if this value is an additive identity for this semiring.Multiplies two elements of this semiring (associative).
-
Method Details
-
add
-
mult
-
isZero
boolean isZero()Checks if this value is an additive identity for this semiring.
An element 0 is an additive identity if a + 0 = a for any a in the semiring.
- Returns:
- True if this value is an additive identity for this semiring. Otherwise, false.
-
isOne
boolean isOne()Checks if this value is a multiplicative identity for this semiring.
An element 1 is a multiplicative identity if a * 1 = a for any a in the semiring.
- Returns:
- True if this value is a multiplicative identity for this semiring. Otherwise, false.
-
getZero
T getZero()Gets the additive identity for this semiring.
An element 0 is an additive identity if a + 0 = a for any a in the semiring.
- Returns:
- The additive identity for this semiring.
-
getOne
T getOne()Gets the multiplicative identity for this semiring.
An element 1 is a multiplicative identity if a * 1 = a for any a in the semiring.
- Returns:
- The multiplicative identity for this semiring.
-
compareTo
Compares this element of the semiring withb
.- Specified by:
compareTo
in interfaceComparable<T extends Semiring<T>>
- Parameters:
b
- Second element of the semiring.- Returns:
- An int value:
- 0 if this semiring element is equal to
b
. - invalid input: '<' 0 if this semiring element is less than
b
. - > 0 if this semiring element is greater than
b
.
Hence, this method returns zero if and only if the two semiring elements are equal, a negative value if and only the semiring
element it was called on is less than
b
and positive if and only if the semiring element it was called on is greater thanb
. - 0 if this semiring element is equal to
-
doubleValue
double doubleValue()Converts this semiring value to an equivalent double value.- Returns:
- A double value equivalent to this semiring element.
-