Package gmp

Class Mpq

All Implemented Interfaces:
Serializable, Cloneable, Comparable<Mpq>

public class Mpq extends Object implements Serializable, Comparable<Mpq>, Cloneable
GMP arbitrary precision rationals.

This class wraps mpq_t GMP objects.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Mpq()
    Constructs a rational initialized to 0/1.
    Mpq(double d)
    Constructs a rational equal to the double d.
    Mpq(int op)
    Constructs a rational equal to the integer op.
    Mpq(int num, int den)
    Constructs a rational equal to num/den.
    Mpq(Mpq op)
    Constructs a copy of op.
    Mpq(Mpz op)
    Constructs a rational equal to the integer op.
    Mpq(Mpz num, Mpz den)
    Constructs a rational equal to num/den.
    Constructs a rational from the String s.
    Mpq(String s, int b)
    Constructs a rational from the String s expressed in base b.
    Constructs a rational equal to the integer op.
    Constructs a rational equal to num/den.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    abs()
    Replaces this with |this|.
    void
    abs(Mpq arg)
    Stores |arg| into this.
    void
    add(Mpq arg)
    Adds arg to this.
    void
    add(Mpq arg1, Mpq arg2)
    Stores arg1+arg2 into this.
    void
    Puts the fraction into normal form.
    Creates a new Mpq initialized with the value of this.
    int
    cmp(int num, int den)
    Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than num/den.
    int
    cmp(Mpq op)
    Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.
    int
    Compare the value of this and x.
    Returns a reference to the denominator of this.
    void
    div(Mpq arg)
    Divides this by arg.
    void
    div(Mpq arg1, Mpq arg2)
    Stores arg1/arg2 into this.
    void
    div2exp(int arg)
    Divides this by (2^arg).
    void
    div2exp(Mpq arg1, int arg2)
    Stores arg1/(2^arg2) into this.
    void
    divRev(Mpq arg)
    Stores arg/this into this.
    double
    Returns this as a double.
    boolean
    Whether x is a Mpq and has the same value as this.
    Returns a copy of the denominator of this.
    Returns a copy of the numerator of this.
    int
    Returns a hash of the value of this.
    void
    inv()
    Replaces this with 1/this.
    void
    inv(Mpq arg)
    Stores 1/arg into this.
    boolean
    Whether this equals op.
    boolean
    Whether this and x have the same address in memory.
    void
    mul(Mpq arg)
    Multiply this by arg.
    void
    mul(Mpq arg1, Mpq arg2)
    Stores arg1*arg2 into this.
    void
    mul2exp(int arg)
    Multiply this by 2^arg.
    void
    mul2exp(Mpq arg1, int arg2)
    Stores arg1*(2^arg2) into this.
    void
    neg()
    Replaces this with -this.
    void
    neg(Mpq arg)
    Stores -arg into this.
    Returns a reference to the numerator of this.
    void
    set(double d)
    Sets this to the double number.
    void
    set(int op)
    Sets this to the integer op.
    void
    set(int num, int den)
    Sets this to num/den.
    void
    set(Mpq op)
    Copies the argument into this.
    void
    set(Mpz op)
    Sets this to the integer op.
    void
    set(Mpz num, Mpz den)
    Sets this to num/den.
    void
    Sets this to the String s.
    void
    set(String s, int b)
    Sets this to the String s expressed in base b.
    void
    Sets this to the integer op.
    void
    Sets this to num/den.
    void
    setDen(int op)
    Changes the denominator of this.
    void
    setDen(Mpz op)
    Changes the denominator of this.
    void
    Changes the denominator of this.
    void
    setNum(int op)
    Changes the numerator of this.
    void
    setNum(Mpz op)
    Changes the numerator of this.
    void
    Changes the numerator of this.
    int
    sgn()
    Returns +1, 0, or -1 when this is respectively strictly positive, null, or strictly negative.
    int
    Returns the size (in GMP limbs) of the internal representation of this.
    void
    sub(Mpq arg)
    Subtracts arg from this.
    void
    sub(Mpq arg1, Mpq arg2)
    Stores arg1-arg2 into this.
    void
    subRev(Mpq arg)
    Replaces this with arg-this.
    Returns a String representation of this in base 10.
    toString(int b)
    Returns a String representation of this in base b.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Mpq

      public Mpq()
      Constructs a rational initialized to 0/1.
    • Mpq

      public Mpq(Mpq op)
      Constructs a copy of op.
    • Mpq

      public Mpq(Mpz op)
      Constructs a rational equal to the integer op.
    • Mpq

      public Mpq(BigInteger op)
      Constructs a rational equal to the integer op.
    • Mpq

      public Mpq(int op)
      Constructs a rational equal to the integer op.
    • Mpq

      public Mpq(double d)
      Constructs a rational equal to the double d.

      There is no rounding, the conversion is exact.

    • Mpq

      public Mpq(String s, int b)
      Constructs a rational from the String s expressed in base b.

      The String can have a fraction / symbol.

      The result is canonicalized.

      See Also:
    • Mpq

      public Mpq(String s)
      Constructs a rational from the String s.

      The String can have a fraction / symbol.

      The result is canonicalized.

      See Also:
    • Mpq

      public Mpq(Mpz num, Mpz den)
      Constructs a rational equal to num/den.

      The result is canonicalized.

    • Mpq

      public Mpq(BigInteger num, BigInteger den)
      Constructs a rational equal to num/den.

      The result is canonicalized.

    • Mpq

      public Mpq(int num, int den)
      Constructs a rational equal to num/den.

      The result is canonicalized.

  • Method Details

    • canonicalize

      public void canonicalize()
      Puts the fraction into normal form.

      Removes common factors in the numerator and denominator, and makes the denominator positive.

      All arithmetic functions assume and return canonical fractions. However, pure assignment functions (including separate assignments of denominators and numerators, directly or through a MpzRef reference) do not canonicalize the assigned object, and so, the function must be called by the user before arithmetic operations can be performed.

    • clone

      public Mpq clone() throws CloneNotSupportedException
      Creates a new Mpq initialized with the value of this.
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • set

      public void set(Mpq op)
      Copies the argument into this.
    • set

      public void set(Mpz op)
      Sets this to the integer op.
    • set

      public void set(BigInteger op)
      Sets this to the integer op.
    • set

      public void set(int op)
      Sets this to the integer op.
    • set

      public void set(Mpz num, Mpz den)
      Sets this to num/den.

      canonicalize() must be called manually if the fraction is not canonical.

    • set

      public void set(BigInteger num, BigInteger den)
      Sets this to num/den.

      canonicalize() must be called manually if the fraction is not canonical.

    • set

      public void set(int num, int den)
      Sets this to num/den.

      canonicalize() must be called manually if the fraction is not canonical.

    • set

      public void set(double d)
      Sets this to the double number.

      There is no rounding: the conversion is exact.

    • set

      public void set(String s, int b)
      Sets this to the String s expressed in base b.

      The String can have a fraction / symbol.

      canonicalize() must be called manually if the fraction is not canonical.

      See Also:
    • set

      public void set(String s)
      Sets this to the String s.

      The String can have a fraction / symbol.

      canonicalize() must be called manually if the fraction is not canonical.

      See Also:
    • setNum

      public void setNum(Mpz op)
      Changes the numerator of this.

      canonicalize() must be called manually if the fraction is not canonical.

    • setNum

      public void setNum(int op)
      Changes the numerator of this.

      canonicalize() must be called manually if the fraction is not canonical.

    • setNum

      public void setNum(BigInteger op)
      Changes the numerator of this.

      canonicalize() must be called manually if the fraction is not canonical.

    • setDen

      public void setDen(Mpz op)
      Changes the denominator of this.

      canonicalize() must be called manually if the fraction is not canonical.

    • setDen

      public void setDen(int op)
      Changes the denominator of this.

      canonicalize() must be called manually if the fraction is not canonical.

    • setDen

      public void setDen(BigInteger op)
      Changes the denominator of this.

      canonicalize() must be called manually if the fraction is not canonical.

    • doubleValue

      public double doubleValue()
      Returns this as a double.

      If this cannot be represented as a double, the result is truncated.

      Overflows cause infinities to be returned.

    • toString

      public String toString(int b)
      Returns a String representation of this in base b.
      Parameters:
      b - must be between 2 and 62.
    • toString

      public String toString()
      Returns a String representation of this in base 10.
      Overrides:
      toString in class Object
    • getNum

      public Mpz getNum()
      Returns a copy of the numerator of this.
    • getDen

      public Mpz getDen()
      Returns a copy of the denominator of this.
    • numRef

      public MpzRef numRef()
      Returns a reference to the numerator of this.
    • denRef

      public MpzRef denRef()
      Returns a reference to the denominator of this.
    • add

      public void add(Mpq arg1, Mpq arg2)
      Stores arg1+arg2 into this.
    • add

      public void add(Mpq arg)
      Adds arg to this.
    • sub

      public void sub(Mpq arg1, Mpq arg2)
      Stores arg1-arg2 into this.
    • sub

      public void sub(Mpq arg)
      Subtracts arg from this.
    • subRev

      public void subRev(Mpq arg)
      Replaces this with arg-this.
    • mul

      public void mul(Mpq arg1, Mpq arg2)
      Stores arg1*arg2 into this.
    • mul

      public void mul(Mpq arg)
      Multiply this by arg.
    • mul2exp

      public void mul2exp(Mpq arg1, int arg2)
      Stores arg1*(2^arg2) into this.
    • mul2exp

      public void mul2exp(int arg)
      Multiply this by 2^arg.
    • div

      public void div(Mpq arg1, Mpq arg2)
      Stores arg1/arg2 into this.
    • div

      public void div(Mpq arg)
      Divides this by arg.
    • divRev

      public void divRev(Mpq arg)
      Stores arg/this into this.
    • div2exp

      public void div2exp(Mpq arg1, int arg2)
      Stores arg1/(2^arg2) into this.
    • div2exp

      public void div2exp(int arg)
      Divides this by (2^arg).
    • neg

      public void neg(Mpq arg)
      Stores -arg into this.
    • neg

      public void neg()
      Replaces this with -this.
    • abs

      public void abs(Mpq arg)
      Stores |arg| into this.
    • abs

      public void abs()
      Replaces this with |this|.
    • inv

      public void inv(Mpq arg)
      Stores 1/arg into this.
    • inv

      public void inv()
      Replaces this with 1/this.
    • size

      public int size()
      Returns the size (in GMP limbs) of the internal representation of this.
    • cmp

      public int cmp(Mpq op)
      Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.
    • cmp

      public int cmp(int num, int den)
      Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than num/den.

      den must be positive.

    • sgn

      public int sgn()
      Returns +1, 0, or -1 when this is respectively strictly positive, null, or strictly negative.
    • isEqual

      public boolean isEqual(Mpq op)
      Whether this equals op.

      This method is faster than calling cmp, as fractions are assumed to be canonical.

    • equals

      public boolean equals(Object x)
      Whether x is a Mpq and has the same value as this.
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(Mpq x)
      Compare the value of this and x.
      Specified by:
      compareTo in interface Comparable<Mpq>
    • hashCode

      public int hashCode()
      Returns a hash of the value of this.
      Overrides:
      hashCode in class Object
    • isPhysicalEqual

      public boolean isPhysicalEqual(Mpq x)
      Whether this and x have the same address in memory.