Class Rational

java.lang.Object
java.lang.Number
org.episteme.core.mathematics.numbers.rationals.Rational
All Implemented Interfaces:
Serializable, Comparable<Rational>, AbelianGroup<Rational>, AbelianMonoid<Rational>, Group<Rational>, Magma<Rational>, Monoid<Rational>, Field<Rational>, FieldElement<Rational>, Ring<Rational>, RingElement<Rational>, Semiring<Rational>, Set<Rational>

public final class Rational extends Number implements Comparable<Rational>, Field<Rational>, FieldElement<Rational>
Represents a rational number (ℚ), defined as the quotient of two integers.

Rational numbers form a Field under addition and multiplication. They are always stored in reduced form (gcd(numerator, denominator) = 1) with a positive denominator.

* @author Silvere Martin-Michiellot
Since:
1.0
Author:
Gemini AI (Google DeepMind)
See Also:
  • Field Details

    • ZERO

      public static final Rational ZERO
    • ONE

      public static final Rational ONE
  • Method Details

    • of

      public static Rational of(Integer numerator, Integer denominator)
    • of

      public static Rational of(long numerator, long denominator)
    • of

      public static Rational of(int numerator, int denominator)
    • of

      public static Rational of(long value)
    • of

      public static Rational of(int value)
    • getNumerator

      public Integer getNumerator()
    • getDenominator

      public Integer getDenominator()
    • operate

      public Rational operate(Rational a, Rational b)
      Description copied from interface: Magma
      Performs the binary operation on two elements.

      This is the fundamental operation of a magma. The result must be an element of this magma (closure property).

      Properties: None required (not necessarily associative or commutative)

      Specified by:
      operate in interface Magma<Rational>
      Parameters:
      a - the first operand
      b - the second operand
      Returns:
      the result of a ∗ b
      See Also:
    • add

      public Rational add(Rational a, Rational b)
      Description copied from interface: AbelianMonoid
      Returns the sum of two elements (additive notation).

      Delegates to Magma.operate(Object, Object).

      Specified by:
      add in interface AbelianMonoid<Rational>
      Parameters:
      a - the first addend
      b - the second addend
      Returns:
      a + b
    • add

      public Rational add(Rational other)
      Description copied from interface: RingElement
      Returns the sum of this element and another.
      Specified by:
      add in interface RingElement<Rational>
      Parameters:
      other - the element to add
      Returns:
      this + other
    • zero

      public Rational zero()
      Description copied from interface: AbelianMonoid
      Returns the additive identity (zero element).

      Delegates to AbelianMonoid.identity().

      Specified by:
      zero in interface AbelianMonoid<Rational>
      Specified by:
      zero in interface RingElement<Rational>
      Returns:
      the zero element
    • negate

      public Rational negate(Rational a)
      Description copied from interface: AbelianGroup
      Returns the additive inverse (negation) of an element.

      Satisfies: a + (-a) = (-a) + a = 0

      Specified by:
      negate in interface AbelianGroup<Rational>
      Parameters:
      a - the element to negate
      Returns:
      -element
      See Also:
    • negate

      public Rational negate()
      Description copied from interface: RingElement
      Returns the additive inverse of this element.
      Specified by:
      negate in interface RingElement<Rational>
      Returns:
      -this
    • subtract

      public Rational subtract(Rational a, Rational b)
      Description copied from interface: AbelianGroup
      Returns the difference of two elements.

      Defined as: a - b = a + (-b)

      Specified by:
      subtract in interface AbelianGroup<Rational>
      Parameters:
      a - the minuend
      b - the subtrahend
      Returns:
      a - b
      See Also:
    • subtract

      public Rational subtract(Rational other)
      Description copied from interface: RingElement
      Returns the difference of this element and another.

      Default implementation: this.add(other.negate())

      Specified by:
      subtract in interface RingElement<Rational>
      Parameters:
      other - the element to subtract
      Returns:
      this - other
    • multiply

      public Rational multiply(Rational a, Rational b)
      Description copied from interface: Semiring
      Returns the product of two elements.

      Multiplication must be associative and distribute over addition.

      Specified by:
      multiply in interface Semiring<Rational>
      Parameters:
      a - the first factor
      b - the second factor
      Returns:
      a × b
    • multiply

      public Rational multiply(Rational other)
      Description copied from interface: RingElement
      Returns the product of this element and another.
      Specified by:
      multiply in interface RingElement<Rational>
      Parameters:
      other - the element to multiply
      Returns:
      this * other
    • one

      public Rational one()
      Description copied from interface: Semiring
      Returns the multiplicative identity (one element).

      Satisfies: 1 × a = a × 1 = a for all elements a.

      Specified by:
      one in interface RingElement<Rational>
      Specified by:
      one in interface Semiring<Rational>
      Returns:
      the multiplicative identity
    • isMultiplicationCommutative

      public boolean isMultiplicationCommutative()
      Description copied from interface: Field
      Fields always have commutative multiplication.
      Specified by:
      isMultiplicationCommutative in interface Field<Rational>
      Specified by:
      isMultiplicationCommutative in interface Semiring<Rational>
      Returns:
      always true
    • inverse

      public Rational inverse(Rational a)
      Description copied from interface: Field
      Returns the multiplicative inverse of a non-zero element.

      For element a ≠ 0, returns a⁻¹ such that: a × a⁻¹ = a⁻¹ × a = 1

      Examples:

      • In ℚ: inverse(2/3) = 3/2
      • In ℝ: inverse(5.0) = 0.2
      • In â„‚: inverse(3+4i) = (3-4i)/25

      Specified by:
      inverse in interface Field<Rational>
      Specified by:
      inverse in interface Group<Rational>
      Parameters:
      a - the element to invert (must be non-zero)
      Returns:
      the multiplicative inverse
      See Also:
    • inverse

      public Rational inverse()
      Description copied from interface: FieldElement
      Returns the multiplicative inverse of this element.

      For any non-zero element e: e.multiply(e.inverse()) = e.one()

      Specified by:
      inverse in interface FieldElement<Rational>
      Returns:
      1/this
    • divide

      public Rational divide(Rational a, Rational b)
      Description copied from interface: Field
      Returns the quotient of two elements (division).

      Defined as: a ÷ b = a × b⁻¹

      Specified by:
      divide in interface Field<Rational>
      Parameters:
      a - the dividend (numerator)
      b - the divisor (denominator, must be non-zero)
      Returns:
      dividend ÷ divisor
      See Also:
    • divide

      public Rational divide(Rational other)
      Description copied from interface: FieldElement
      Returns this element divided by another.

      Default implementation: this.multiply(other.inverse())

      Specified by:
      divide in interface FieldElement<Rational>
      Parameters:
      other - the divisor
      Returns:
      this / other
    • compareTo

      public int compareTo(Rational other)
      Specified by:
      compareTo in interface Comparable<Rational>
    • intValue

      public int intValue()
      Specified by:
      intValue in class Number
    • longValue

      public long longValue()
      Specified by:
      longValue in class Number
    • floatValue

      public float floatValue()
      Specified by:
      floatValue in class Number
    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in class Number
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • characteristic

      public int characteristic()
      Description copied from interface: Field
      Returns the characteristic of this field.

      The characteristic is the smallest positive integer n such that:
      1 + 1 + ... + 1 (n times) = 0
      If no such n exists, the characteristic is 0.

      Examples:

      • char(ℚ) = char(ℝ) = char(â„‚) = 0
      • char(𝔽ₚ) = p (for prime p)

      Specified by:
      characteristic in interface Field<Rational>
      Returns:
      the characteristic (0 for infinite fields, p for finite fields)
    • contains

      public boolean contains(Rational element)
      Description copied from interface: Set
      Tests whether this set contains the specified element.

      This is the fundamental operation of a set - membership testing.

      Specified by:
      contains in interface Set<Rational>
      Parameters:
      element - the element to test for membership
      Returns:
      true if this set contains the element, false otherwise
      See Also:
    • description

      public String description()
      Description copied from interface: Set
      Returns a human-readable description of this set.

      Examples:

      • "ℝ (Real Numbers)"
      • "ℤ/12ℤ (Integers modulo 12)"
      • "{1, 2, 3, 4, 5}"

      Specified by:
      description in interface Set<Rational>
      Returns:
      a description of this set
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Set
      Returns true if this set contains no elements.

      The empty set (∅) is a fundamental concept in set theory. It is the unique set containing no elements.

      Specified by:
      isEmpty in interface Set<Rational>
      Returns:
      true if this set is empty
    • abs

      public Rational abs()
    • floor

      public Integer floor()
    • ceil

      public Integer ceil()
    • fractionalPart

      public Rational fractionalPart()
    • mod

      public Rational mod(Rational m)