Rig
A rig with identity is a semiring in which there is an identity element for multiplication (called identity): \(\exists 1 \in S \quad \forall a \in S \quad a * 1 = 1 * a = a\).
All laws of a semiring must hold for a rig with identity:
- Additive closure (closure): for \(\forall x, y \in S\), \(x + y \in S\).
- Additive associativity (associativity): for \(\forall x, y, z \in S\), \((x + y) + z = x + (y + z)\).
- Existence of zero element: there exists \(\exists 0 \in S\) such that for \(\forall x \in S\), \(0 + x = x + 0 = x\)
- Additive commutativity (commutative): for \(\forall x, y \in S\), \(x + y = y + x\).
- Multiplicative closure (closure): for \(\forall x, y \in S\), \(x * y \in S\).
- Multiplicative associativity (associativity): for \(\forall x, y, z \in S\), \((x * y) * z = x * (y * z)\).
- Distributivity (distributivus, distributive law): for \(\forall x, y, z \in S\), \(x * (y + z) = x * y + x * z\) and \((x + y) * z = x * z + y * z\).
- Multiplicative zero property: \(a * 0 = 0 * a = 0\)
as well as the law of multiplicative identity:
- Existence of unit element: \(\exists 1 \in S \quad \forall x \in S \quad x * 1 = 1 * x = x\)
Code
trait Rig[A] extends Semiring[A]:
def one: A
Laws
- Laws of a semiring
- Check for the existence of an identity element.
The identity element (
su.one) is such an element that when multiplied by any other elementxit does not change its value. This can be expressed in two ways:
- If you multiply
xby the identity element (su.one), the result will be equal tox:su.times(x, su.one) == x
- If you multiply the identity element (
su.one) byx, the result will also be equal tox:su.times(su.one, x) == x
Diagram
classDiagram
class Semigroup~A~{
+combine(x: A, y: A) A
}
class Monoid~A~{
+empty() A
}
Semigroup <|-- Monoid
class CommutativeSemigroup~A~
Semigroup <|-- CommutativeSemigroup
class CommutativeMonoid~A~
Monoid <|-- CommutativeMonoid
CommutativeSemigroup <|-- CommutativeMonoid
class Semiring~A~
CommutativeMonoid <|-- Semiring
class MultiplicativeSemigroup~A~{
+times(x: A, y: A) A
}
MultiplicativeSemigroup <|-- Semiring
Semigroup .. MultiplicativeSemigroup
class Rig~A~{
+one() A
}
Semiring <|-- Rig
Implementation in Libraries
Implementation in Spire
Rig[A] provides +, zero, *, and one.
Rig[A] extends Semiring[A] and MultiplicativeMonoid[A].
zero: additive identityone: multiplicative identity (an element such that for anyx,x * one = x)
import spire.algebra.Rig
import spire.math.Rational
Rig.plus(Rational(1, 2), Rational(1, 3))
// val res0: spire.math.Rational = 5/6
Rig.times(Rational(1, 2), Rational(1, 3))
// val res1: spire.math.Rational = 1/6
Rig.pow(Rational(1, 2), 3)
// val res2: spire.math.Rational = 1/8
Rig.zero[Rational]
// val res3: spire.math.Rational = 0
Rig.one[Rational]
// val res4: spire.math.Rational = 1
References: