Class MutableRational

java.lang.Object
de.uni_freiburg.informatik.ultimate.logic.MutableRational
All Implemented Interfaces:
java.lang.Comparable<MutableRational>

public class MutableRational
extends java.lang.Object
implements java.lang.Comparable<MutableRational>
Mutable version of the Rational class. All arithmetic operations change the value of this object. This class is intended to save some unneeded temporary objects in bigger calculations. This should reduce the number of garbage collections such that the program should run faster.
Author:
Juergen Christ
  • Constructor Details

    • MutableRational

      public MutableRational​(int num, int denom)
      Create a new rational representing num/denom.
      Parameters:
      num - the numerator.
      denom - the denominator.
    • MutableRational

      public MutableRational​(java.math.BigInteger num, java.math.BigInteger denom)
      Create a new rational representing num/denom.
      Parameters:
      num - the numerator.
      denom - the denominator.
    • MutableRational

      public MutableRational​(Rational r)
      Create a new mutable rational from a rational.
      Parameters:
      r - the rational.
    • MutableRational

      public MutableRational​(MutableRational r)
      Create a new copy of a mutable rational.
      Parameters:
      r - the original rational.
  • Method Details

    • setValue

      public void setValue​(Rational value)
      Set the value of this rational to value.
      Parameters:
      value - the value to set to.
    • setValue

      public void setValue​(long newnum, long newdenom)
      Set the value of this rational to newnum/newdenom.
      Parameters:
      newnum - the new numerator.
      newdenom - the new denominator.
    • add

      public MutableRational add​(Rational other)
      Add the rational other to this rational.
      Parameters:
      other - the rational to add.
      Returns:
      this mutable rational.
    • negate

      public MutableRational negate()
      Negate this rational, i.e., this = -this.
      Returns:
      this mutable rational.
    • sub

      public MutableRational sub​(Rational other)
      Subtract the other rational from this.
      Parameters:
      other - the rational to subtract.
      Returns:
      this mutable rational.
    • mul

      public MutableRational mul​(Rational other)
      Multiply this rational with other and store the result in this.
      Parameters:
      other - the rational to multiply with.
      Returns:
      this mutable rational.
    • div

      public MutableRational div​(Rational other)
      Divide this rational by the other and store the result in this.
      Parameters:
      other - the divisor.
      Returns:
      this mutable rational.
    • inverse

      public MutableRational inverse()
      Compute the multiplicative inverse of this rational and store it in this.
      Returns:
      this mutable rational.
    • isNegative

      public boolean isNegative()
      Check if this rational is negative.
      Returns:
      true iff this < 0.
    • addmul

      public MutableRational addmul​(Rational fac1, Rational fac2)
      Computes this += (fac1*fac2).
      Parameters:
      fac1 - one of the factors.
      fac2 - the other factor.
      Returns:
      this mutable rational.
    • addmul

      public MutableRational addmul​(Rational fac1, java.math.BigInteger fac2)
      Computes this += (fac1*fac2).
      Parameters:
      fac1 - one of the factors.
      fac2 - the other factor.
      Returns:
      this mutable rational.
    • subdiv

      public MutableRational subdiv​(Rational s, Rational d)
      Computes this = (this - s) / d.
      Parameters:
      s - the rational to subtract.
      d - the divisor.
      Returns:
      this mutable rational.
    • compareTo

      public int compareTo​(MutableRational o)
      Specified by:
      compareTo in interface java.lang.Comparable<MutableRational>
    • compareTo

      public int compareTo​(Rational o)
      Compares this mutable rational with the other.
      Parameters:
      o - the other rational.
      Returns:
      -1, if this < o; 1, if this > o; 0 if they are equal.
    • equals

      public boolean equals​(java.lang.Object o)
      Compares this mutable rational with the other. This works with MutableRational and Rational.
      Overrides:
      equals in class java.lang.Object
      Parameters:
      o - the other rational.
      Returns:
      true if this equals o, false otherwise.
    • numerator

      public java.math.BigInteger numerator()
      Get the numerator of this rational.
      Returns:
      the numerator.
    • denominator

      public java.math.BigInteger denominator()
      Get the denominator of this rational.
      Returns:
      the denominator.
    • hashCode

      public int hashCode()
      Computes a hashcode. The hashcode is computed as 257 * numerator + denominator if both fit into an integer and 257 * numerator().hashCode() + denominator().hashCode() if big integers are necessary.
      Overrides:
      hashCode in class java.lang.Object
      Returns:
      the hashcode.
    • toString

      public java.lang.String toString()
      Get a string representation of this number. This is numerator()+ "/" + denominator() except for infinity ("inf"), nan ("nan"), or minus infinity ("-inf").
      Overrides:
      toString in class java.lang.Object
      Returns:
      the string representation.
    • isIntegral

      public boolean isIntegral()
      Check whether this rational represents an integral value. Both infinity values are treated as integral.
      Returns:
      true iff value is integral.
    • toRational

      public Rational toRational()
      Convert this mutable rational into a immutable rational.
      Returns:
      the rational.
    • signum

      public int signum()
      Compute sign of this rational. This is equivalent to compare(Rational.ZERO).
      Returns:
      the sign of the rational, +1 for positive, 0 for 0 and -1 for negative.