Class BoolSemiring

java.lang.Object
org.flag4j.algebraic_structures.BoolSemiring
All Implemented Interfaces:
Serializable, Comparable<BoolSemiring>, Semiring<BoolSemiring>

public class BoolSemiring extends Object implements Semiring<BoolSemiring>
Represents an immutable boolean value within a semiring structure.

This class wraps a primitive boolean value and provides operations consistent with a boolean semiring. In this semiring:

  • Addition is defined as logical OR (a + b = a || b).
  • Multiplication is defined as logical AND (a * b = a && b).
  • Additive Identity is false (denoted by ZERO or FALSE).
  • Multiplicative Identity is true (denoted by ONE or TRUE).

The class implements the Semiring interface and adheres to its contract. Instances of BoolSemiring are immutable and thread-safe.

Usage Example:


 BoolSemiring a = BoolSemiring.TRUE;  // Equivalent to new BoolSemiring(true)
 BoolSemiring b = BoolSemiring.FALSE; // Equivalent to new BoolSemiring(false)

 BoolSemiring sum = a.add(b);       // Logical OR: true || false => true
 BoolSemiring product = a.mult(b);  // Logical AND: true && false => false
 BoolSemiring notA = a.not();       // Logical NOT: !true => false
 

Constants:

The class provides constants for common boolean values:

  • TRUE or ONE - Represents the boolean value true.
  • FALSE or ZERO - Represents the boolean value false.
See Also:
  • Field Details

    • ONE

      public static final BoolSemiring ONE
      The boolean value true.
    • TRUE

      public static final BoolSemiring TRUE
      The boolean value true.
    • ZERO

      public static final BoolSemiring ZERO
      The boolean value false.
    • FALSE

      public static final BoolSemiring FALSE
      The boolean value false.
  • Constructor Details

    • BoolSemiring

      public BoolSemiring(boolean value)
      Constructs a BoolSemiring semiring element with the specified boolean value.
      Parameters:
      value - the boolean value to wrap.
    • BoolSemiring

      public BoolSemiring(int value)
      Constructs a BoolSemiring semiring element from an integer.
      Parameters:
      value - the integer value (must be 0 or 1).
      Throws:
      IllegalArgumentException - if the value is not 0 or 1.
  • Method Details

    • add

      public BoolSemiring add(BoolSemiring b)

      Performs the addition operation of this semiring, defined as logical OR.

      This operation is associative and commutative.

      Specified by:
      add in interface Semiring<BoolSemiring>
      Parameters:
      b - the second semiring element to add.
      Returns:
      A new BoolSemiring representing the logical OR of this value and b.
    • or

      public BoolSemiring or(BoolSemiring b)

      Computes the logical OR of this BoolSemiring with another.

      This method is equivalent to add(BoolSemiring).

      Parameters:
      b - the BoolSemiring to perform the logical OR with.
      Returns:
      A new BoolSemiring representing the logical OR of this value and b.
    • xor

      public BoolSemiring xor(BoolSemiring b)
      Computes the exclusive OR (XOR) of this BoolSemiring with another.
      Parameters:
      b - the BoolSemiring to perform the XOR with.
      Returns:
      A new BoolSemiring representing the XOR of this value and b.
    • mult

      public BoolSemiring mult(BoolSemiring b)

      Performs the multiplication operation of this semiring, defined as logical AND.

      This operation is associative.

      Specified by:
      mult in interface Semiring<BoolSemiring>
      Parameters:
      b - the second semiring element to multiply.
      Returns:
      A new BoolSemiring representing the logical AND of this value and b.
    • and

      public BoolSemiring and(BoolSemiring b)

      Computes the logical AND of this BoolSemiring with another.

      This method is equivalent to mult(BoolSemiring).

      Parameters:
      b - the BoolSemiring to perform the logical AND with.
      Returns:
      A new BoolSemiring representing the logical AND of this value and b.
    • not

      public BoolSemiring not()
      Computes the logical NOT (negation) of this BoolSemiring.
      Returns:
      a new BoolSemiring representing the logical NOT of this value.
    • isZero

      public 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.

      Specified by:
      isZero in interface Semiring<BoolSemiring>
      Returns:
      true if this value is an additive identity for this semiring; false otherwise.
    • isOne

      public 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.

      Specified by:
      isOne in interface Semiring<BoolSemiring>
      Returns:
      true if this value is a multiplicative identity for this semiring; false otherwise.
    • getValue

      public boolean getValue()
      Gets the value of this semiring element.
      Returns:
      The value of this semiring element.
    • getZero

      public BoolSemiring 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.

      Specified by:
      getZero in interface Semiring<BoolSemiring>
      Returns:
      The additive identity for this semiring.
    • getOne

      public BoolSemiring 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.

      Specified by:
      getOne in interface Semiring<BoolSemiring>
      Returns:
      The multiplicative identity for this semiring.
    • compareTo

      public int compareTo(BoolSemiring b)
      Compares this element of the semiring with b.
      Specified by:
      compareTo in interface Comparable<BoolSemiring>
      Specified by:
      compareTo in interface Semiring<BoolSemiring>
      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 than b.
    • doubleValue

      public double doubleValue()
      Converts this semiring value to an equivalent double value.
      Specified by:
      doubleValue in interface Semiring<BoolSemiring>
      Returns:
      A double value equivalent to this semiring element.
    • equals

      public boolean equals(Object b)
      Checks if an object is equal to this semiring element.
      Overrides:
      equals in class Object
      Parameters:
      b - Object to compare to this semiring element.
      Returns:
      True if the objects are the same or are both BoolSemiring's and have equal values.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object