Package edu.jas.arith
Class BigRational
- java.lang.Object
-
- edu.jas.arith.BigRational
-
- All Implemented Interfaces:
Rational,AbelianGroupElem<BigRational>,AbelianGroupFactory<BigRational>,Element<BigRational>,ElemFactory<BigRational>,GcdRingElem<BigRational>,MonoidElem<BigRational>,MonoidFactory<BigRational>,RingElem<BigRational>,RingFactory<BigRational>,java.io.Serializable,java.lang.Comparable<BigRational>,java.lang.Iterable<BigRational>
public final class BigRational extends java.lang.Object implements GcdRingElem<BigRational>, RingFactory<BigRational>, Rational, java.lang.Iterable<BigRational>
Immutable arbitrary-precision rational numbers. BigRational class based on BigInteger and implementing the RingElem interface. BigInteger is from java.math in the implementation. The SAC2 static methods are also provided.- Author:
- Heinz Kredel
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description java.math.BigIntegerdenDenominator part of the data structure.static BigRationalHALFThe Constant 1/2.java.math.BigIntegernumNumerator part of the data structure.static BigRationalONEThe Constant 1.static BigRationalZEROThe Constant 0.
-
Constructor Summary
Constructors Modifier Constructor Description BigRational()Constructor for a BigRational with no arguments.BigRational(long n)Constructor for a BigRational from longs.BigRational(long n, long d)Constructor for a BigRational from longs.BigRational(BigInteger n)Constructor for a BigRational from jas.arith.BigIntegers.BigRational(BigInteger n, BigInteger d)Constructor for a BigRational from jas.arith.BigIntegers.BigRational(java.lang.String s)Constructor for a BigRational from String.BigRational(java.math.BigInteger n)Constructor for a BigRational from math.BigIntegers.protectedBigRational(java.math.BigInteger n, java.math.BigInteger d)Constructor for a BigRational from math.BigIntegers.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigRationalabs()Rational number absolute value.longbitLength()Returns the number of bits in the representation of this BigRational, including a sign bit.java.math.BigIntegerceil()BigRational ceiling.java.math.BigIntegercharacteristic()Characteristic of this ring.intcompareTo(BigRational S)Rational number comparison.BigRationalcopy()Clone this.BigRationalcopy(BigRational c)Copy BigRational element c.java.math.BigIntegerdenominator()Get the denominator.BigRationaldivide(BigRational S)Rational number quotient.doubledoubleValue()Get this as a double.BigRational[]egcd(BigRational S)BigRational extended greatest common divisor.booleanequals(java.lang.Object b)Comparison with any other object.BigRationalfactory()Get the corresponding element factory.java.math.BigIntegerfloor()BigRational floor.BigRationalfromInteger(long a)Get a BigRational element from a long.BigRationalfromInteger(BigInteger a)Get a BigRational element from a arith.BigInteger.BigRationalfromInteger(java.math.BigInteger a)Get a BigRational element from a math.BigInteger.BigRationalgcd(BigRational S)Rational number greatest common divisor.java.util.List<BigRational>generators()Get a list of the generating elements.BigDecimalgetDecimal()Get the decimal representation.BigRationalgetONE()Get the one element.BigRationalgetRational()Return a BigRational approximation of this Element.BigRationalgetZERO()Get the zero element.inthashCode()Hash code for this BigRational.BigRationalinverse()Rational number inverse.booleanisAssociative()Query if this ring is associative.booleanisCommutative()Query if this ring is commutative.booleanisEntier()Is BigRational entier.booleanisField()Query if this ring is a field.booleanisFinite()Is this structure finite or infinite.booleanisONE()Is BigRational one.booleanisUnit()Is BigRational unit.booleanisZERO()Is BigRational zero.java.util.Iterator<BigRational>iterator()Get a BigRational iterator.BigRationalmultiply(BigRational S)Rational number product.BigRationalnegate()Rational number negative.java.math.BigIntegernumerator()Get the numerator.BigRationalparse(java.io.Reader r)Parse rational number from Reader.BigRationalparse(java.lang.String s)Parse rational number from String.BigRational[]quotientRemainder(BigRational S)Quotient and remainder by division of this by S.BigRationalrandom(int n)Rational number, random.BigRationalrandom(int n, java.util.Random rnd)Rational number, random.static BigRationalreduction(java.math.BigInteger n, java.math.BigInteger d)Rational number reduction to lowest terms.BigRationalremainder(BigRational S)Rational number remainder.static BigRationalRNABS(BigRational R)Rational number absolute value.static intRNCOMP(BigRational R, BigRational S)Rational number comparison.static java.math.BigIntegerRNDEN(BigRational R)Rational number denominator.static BigRationalRNDIF(BigRational R, BigRational S)Rational number difference.static voidRNDWR(BigRational R, int NL)Rational number decimal write.static BigRationalRNINT(java.math.BigInteger A)Rational number from integer.static BigRationalRNINV(BigRational R)Rational number inverse.static BigRationalRNNEG(BigRational R)Rational number negative.static java.math.BigIntegerRNNUM(BigRational R)Rational number numerator.static BigRationalRNPROD(BigRational R, BigRational S)Rational number product.static BigRationalRNQ(BigRational R, BigRational S)Rational number quotient.static BigRationalRNRAND(int NL)Rational number, random.static BigRationalRNRED(java.math.BigInteger n, java.math.BigInteger d)Rational number reduction to lowest terms.static intRNSIGN(BigRational R)Rational number sign.static BigRationalRNSUM(BigRational R, BigRational S)Rational number sum.voidsetAllIterator()Set the iteration algorithm to all elements.voidsetDuplicatesIterator()Set the iteration algorithm to allow duplicate elements.voidsetNoDuplicatesIterator()Set the iteration algorithm to no duplicate elements.voidsetNonNegativeIterator()Set the iteration algorithm to non-negative elements.intsignum()Rational number sign.BigRationalsubtract(BigRational S)Rational number difference.BigRationalsum(BigRational S)Rational number sum.java.lang.StringtoScript()Get a scripting compatible string representation.java.lang.StringtoScriptFactory()Get a scripting compatible string representation of the factory.java.lang.StringtoString()Get the string representation.java.lang.StringtoString(int n)Get the decimal string representation with given precision.java.util.Iterator<BigRational>uniqueIterator()Get a BigRational iterator with no duplicates.static BigRationalvalueOf(long a)Get a BigRational element from a long.static BigRationalvalueOf(java.math.BigInteger a)Get a BigRational element from a math.BigInteger.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface edu.jas.structure.ElemFactory
valueOf
-
Methods inherited from interface edu.jas.structure.MonoidElem
leftDivide, leftRemainder, power, rightDivide, rightRemainder, twosidedDivide, twosidedRemainder
-
-
-
-
Field Detail
-
num
public final java.math.BigInteger num
Numerator part of the data structure.
-
den
public final java.math.BigInteger den
Denominator part of the data structure.
-
ZERO
public static final BigRational ZERO
The Constant 0.
-
ONE
public static final BigRational ONE
The Constant 1.
-
HALF
public static final BigRational HALF
The Constant 1/2.
-
-
Constructor Detail
-
BigRational
protected BigRational(java.math.BigInteger n, java.math.BigInteger d)
Constructor for a BigRational from math.BigIntegers.- Parameters:
n- math.BigInteger.d- math.BigInteger.
-
BigRational
public BigRational(java.math.BigInteger n)
Constructor for a BigRational from math.BigIntegers.- Parameters:
n- math.BigInteger.
-
BigRational
public BigRational(BigInteger n)
Constructor for a BigRational from jas.arith.BigIntegers.- Parameters:
n- edu.jas.arith.BigInteger.
-
BigRational
public BigRational(BigInteger n, BigInteger d)
Constructor for a BigRational from jas.arith.BigIntegers.- Parameters:
n- edu.jas.arith.BigInteger.d- edu.jas.arith.BigInteger.
-
BigRational
public BigRational(long n, long d)
Constructor for a BigRational from longs.- Parameters:
n- long.d- long.
-
BigRational
public BigRational(long n)
Constructor for a BigRational from longs.- Parameters:
n- long.
-
BigRational
public BigRational()
Constructor for a BigRational with no arguments.
-
BigRational
public BigRational(java.lang.String s) throws java.lang.NumberFormatException
Constructor for a BigRational from String.- Parameters:
s- String.- Throws:
java.lang.NumberFormatException
-
-
Method Detail
-
factory
public BigRational factory()
Get the corresponding element factory.- Specified by:
factoryin interfaceElement<BigRational>- Returns:
- factory for this Element.
- See Also:
Element.factory()
-
generators
public java.util.List<BigRational> generators()
Get a list of the generating elements.- Specified by:
generatorsin interfaceElemFactory<BigRational>- Returns:
- list of generators for the algebraic structure.
- See Also:
ElemFactory.generators()
-
isFinite
public boolean isFinite()
Is this structure finite or infinite.- Specified by:
isFinitein interfaceElemFactory<BigRational>- Returns:
- true if this structure is finite, else false.
- See Also:
ElemFactory.isFinite()
-
copy
public BigRational copy()
Clone this.- Specified by:
copyin interfaceElement<BigRational>- Returns:
- Creates and returns a copy of this Element.
- See Also:
Object.clone()
-
copy
public BigRational copy(BigRational c)
Copy BigRational element c.- Specified by:
copyin interfaceElemFactory<BigRational>- Parameters:
c- BigRational.- Returns:
- a copy of c.
-
getRational
public BigRational getRational()
Return a BigRational approximation of this Element.- Specified by:
getRationalin interfaceRational- Returns:
- a BigRational approximation of this.
- See Also:
Rational.getRational()
-
numerator
public java.math.BigInteger numerator()
Get the numerator.- Returns:
- num.
-
denominator
public java.math.BigInteger denominator()
Get the denominator.- Returns:
- den.
-
toString
public java.lang.String toString()
Get the string representation.- Overrides:
toStringin classjava.lang.Object- See Also:
Object.toString()
-
toString
public java.lang.String toString(int n)
Get the decimal string representation with given precision.- Parameters:
n- precision.- Returns:
- decimal approximation.
-
getDecimal
public BigDecimal getDecimal()
Get the decimal representation.- Returns:
- decimal.
-
doubleValue
public double doubleValue()
Get this as a double.- Returns:
- this as a double
- See Also:
Number.doubleValue()
-
toScript
public java.lang.String toScript()
Get a scripting compatible string representation.- Specified by:
toScriptin interfaceElement<BigRational>- Specified by:
toScriptin interfaceElemFactory<BigRational>- Returns:
- script compatible representation for this Element.
- See Also:
Element.toScript()
-
toScriptFactory
public java.lang.String toScriptFactory()
Get a scripting compatible string representation of the factory.- Specified by:
toScriptFactoryin interfaceElement<BigRational>- Returns:
- script compatible representation for this ElemFactory.
- See Also:
Element.toScriptFactory()
-
getZERO
public BigRational getZERO()
Get the zero element.- Specified by:
getZEROin interfaceAbelianGroupFactory<BigRational>- Returns:
- 0 as BigRational.
-
getONE
public BigRational getONE()
Get the one element.- Specified by:
getONEin interfaceMonoidFactory<BigRational>- Returns:
- 1 as BigRational.
-
isCommutative
public boolean isCommutative()
Query if this ring is commutative.- Specified by:
isCommutativein interfaceMonoidFactory<BigRational>- Returns:
- true.
-
isAssociative
public boolean isAssociative()
Query if this ring is associative.- Specified by:
isAssociativein interfaceMonoidFactory<BigRational>- Returns:
- true.
-
isField
public boolean isField()
Query if this ring is a field.- Specified by:
isFieldin interfaceRingFactory<BigRational>- Returns:
- true.
-
characteristic
public java.math.BigInteger characteristic()
Characteristic of this ring.- Specified by:
characteristicin interfaceRingFactory<BigRational>- Returns:
- characteristic of this ring.
-
fromInteger
public BigRational fromInteger(java.math.BigInteger a)
Get a BigRational element from a math.BigInteger.- Specified by:
fromIntegerin interfaceElemFactory<BigRational>- Parameters:
a- math.BigInteger.- Returns:
- BigRational from a.
-
fromInteger
public BigRational fromInteger(BigInteger a)
Get a BigRational element from a arith.BigInteger.- Parameters:
a- arith.BigInteger.- Returns:
- BigRational from a.
-
valueOf
public static BigRational valueOf(java.math.BigInteger a)
Get a BigRational element from a math.BigInteger.- Parameters:
a- math.BigInteger.- Returns:
- BigRational from a.
-
fromInteger
public BigRational fromInteger(long a)
Get a BigRational element from a long.- Specified by:
fromIntegerin interfaceElemFactory<BigRational>- Parameters:
a- long.- Returns:
- BigRational from a.
-
valueOf
public static BigRational valueOf(long a)
Get a BigRational element from a long.- Parameters:
a- long.- Returns:
- BigRational from a.
-
isZERO
public boolean isZERO()
Is BigRational zero.- Specified by:
isZEROin interfaceAbelianGroupElem<BigRational>- Returns:
- If this is 0 then true is returned, else false.
- See Also:
AbelianGroupElem.isZERO()
-
isONE
public boolean isONE()
Is BigRational one.- Specified by:
isONEin interfaceMonoidElem<BigRational>- Returns:
- If this is 1 then true is returned, else false.
- See Also:
MonoidElem.isONE()
-
isUnit
public boolean isUnit()
Is BigRational unit.- Specified by:
isUnitin interfaceMonoidElem<BigRational>- Returns:
- If this is a unit then true is returned, else false.
- See Also:
MonoidElem.isUnit()
-
isEntier
public boolean isEntier()
Is BigRational entier.- Returns:
- If this is an integer then true is returned, else false.
-
equals
public boolean equals(java.lang.Object b)
Comparison with any other object.- Specified by:
equalsin interfaceElement<BigRational>- Overrides:
equalsin classjava.lang.Object- Returns:
- true if this is equal to b, else false.
- See Also:
Object.equals(java.lang.Object)
-
hashCode
public int hashCode()
Hash code for this BigRational.- Specified by:
hashCodein interfaceElement<BigRational>- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hashCode.
- See Also:
Object.hashCode()
-
RNRED
public static BigRational RNRED(java.math.BigInteger n, java.math.BigInteger d)
Rational number reduction to lowest terms.- Parameters:
n- BigInteger.d- BigInteger.- Returns:
- a/b ~ n/d, gcd(a,b) = 1, b > 0.
-
reduction
public static BigRational reduction(java.math.BigInteger n, java.math.BigInteger d)
Rational number reduction to lowest terms.- Parameters:
n- BigInteger.d- BigInteger.- Returns:
- a/b ~ n/d, gcd(a,b) = 1, b > 0.
-
abs
public BigRational abs()
Rational number absolute value.- Specified by:
absin interfaceAbelianGroupElem<BigRational>- Returns:
- the absolute value of this.
- See Also:
AbelianGroupElem.abs()
-
RNABS
public static BigRational RNABS(BigRational R)
Rational number absolute value.- Parameters:
R- is a rational number.- Returns:
- the absolute value of R.
-
compareTo
public int compareTo(BigRational S)
Rational number comparison.- Specified by:
compareToin interfacejava.lang.Comparable<BigRational>- Specified by:
compareToin interfaceElement<BigRational>- Parameters:
S- BigRational.- Returns:
- SIGN(this-S).
-
RNCOMP
public static int RNCOMP(BigRational R, BigRational S)
Rational number comparison.- Parameters:
R- BigRational.S- BigRational.- Returns:
- SIGN(R-S).
-
RNDEN
public static java.math.BigInteger RNDEN(BigRational R)
Rational number denominator.- Parameters:
R- BigRational.- Returns:
- R.denominator().
-
subtract
public BigRational subtract(BigRational S)
Rational number difference.- Specified by:
subtractin interfaceAbelianGroupElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this-S.
-
RNDIF
public static BigRational RNDIF(BigRational R, BigRational S)
Rational number difference.- Parameters:
R- BigRational.S- BigRational.- Returns:
- R-S.
-
RNDWR
public static void RNDWR(BigRational R, int NL)
Rational number decimal write. R is a rational number. n is a non-negative integer. R is approximated by a decimal fraction D with n decimal digits following the decimal point and D is written in the output stream. The inaccuracy of the approximation is at most (1/2)*10**-n.- Parameters:
R-NL-
-
RNINT
public static BigRational RNINT(java.math.BigInteger A)
Rational number from integer.- Parameters:
A- BigInteger.- Returns:
- A/1.
-
inverse
public BigRational inverse()
Rational number inverse.- Specified by:
inversein interfaceMonoidElem<BigRational>- Returns:
- 1/this.
- See Also:
MonoidElem.inverse()
-
RNINV
public static BigRational RNINV(BigRational R)
Rational number inverse.- Parameters:
R- BigRational.- Returns:
- 1/R.
-
negate
public BigRational negate()
Rational number negative.- Specified by:
negatein interfaceAbelianGroupElem<BigRational>- Returns:
- -this.
- See Also:
AbelianGroupElem.negate()
-
RNNEG
public static BigRational RNNEG(BigRational R)
Rational number negative.- Parameters:
R- BigRational.- Returns:
- -R.
-
RNNUM
public static java.math.BigInteger RNNUM(BigRational R)
Rational number numerator.- Parameters:
R- BigRational.- Returns:
- R.numerator().
-
multiply
public BigRational multiply(BigRational S)
Rational number product.- Specified by:
multiplyin interfaceMonoidElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this*S.
-
RNPROD
public static BigRational RNPROD(BigRational R, BigRational S)
Rational number product.- Parameters:
R- BigRational.S- BigRational.- Returns:
- R*S.
-
divide
public BigRational divide(BigRational S)
Rational number quotient.- Specified by:
dividein interfaceMonoidElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this/S.
-
RNQ
public static BigRational RNQ(BigRational R, BigRational S)
Rational number quotient.- Parameters:
R- BigRational.S- BigRational.- Returns:
- R/S.
-
remainder
public BigRational remainder(BigRational S)
Rational number remainder.- Specified by:
remainderin interfaceMonoidElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this-(this/S)*S
-
quotientRemainder
public BigRational[] quotientRemainder(BigRational S)
Quotient and remainder by division of this by S.- Specified by:
quotientRemainderin interfaceMonoidElem<BigRational>- Parameters:
S- a rational number- Returns:
- [this/S, this - (this/S)*S].
-
random
public BigRational random(int n)
Rational number, random. Random integers A, B and a random sign s are generated using BigInteger(n,random) and random.nextBoolen(). Then R = s*A/(B+1), reduced to lowest terms.- Specified by:
randomin interfaceElemFactory<BigRational>- Parameters:
n- such that 0 ≤ A, B ≤ (2n-1).- Returns:
- a random BigRational.
-
random
public BigRational random(int n, java.util.Random rnd)
Rational number, random. Random integers A, B and a random sign s are generated using BigInteger(n,random) and random.nextBoolen(). Then R = s*A/(B+1), reduced to lowest terms.- Specified by:
randomin interfaceElemFactory<BigRational>- Parameters:
n- such that 0 ≤ A, B ≤ (2n-1).rnd- is a source for random bits.- Returns:
- a random BigRational.
-
RNRAND
public static BigRational RNRAND(int NL)
Rational number, random. Random integers A, B and a random sign s are generated using BigInteger(n,random) and random.nextBoolen(). Then R = s*A/(B+1), reduced to lowest terms.- Parameters:
NL- such that 0 ≤ A, B ≤ (2n-1).- Returns:
- a random BigRational.
-
signum
public int signum()
Rational number sign.- Specified by:
signumin interfaceAbelianGroupElem<BigRational>- Returns:
- the sign of this.
- See Also:
AbelianGroupElem.signum()
-
RNSIGN
public static int RNSIGN(BigRational R)
Rational number sign.- Parameters:
R- BigRational.- Returns:
- R.signum().
-
sum
public BigRational sum(BigRational S)
Rational number sum.- Specified by:
sumin interfaceAbelianGroupElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this+S.
-
RNSUM
public static BigRational RNSUM(BigRational R, BigRational S)
Rational number sum.- Parameters:
R- BigRational.S- BigRational.- Returns:
- R+S.
-
parse
public BigRational parse(java.lang.String s)
Parse rational number from String.- Specified by:
parsein interfaceElemFactory<BigRational>- Parameters:
s- String.- Returns:
- BigRational from s.
-
parse
public BigRational parse(java.io.Reader r)
Parse rational number from Reader.- Specified by:
parsein interfaceElemFactory<BigRational>- Parameters:
r- Reader.- Returns:
- next BigRational from r.
-
gcd
public BigRational gcd(BigRational S)
Rational number greatest common divisor.- Specified by:
gcdin interfaceRingElem<BigRational>- Parameters:
S- BigRational.- Returns:
- gcd(this,S).
-
egcd
public BigRational[] egcd(BigRational S)
BigRational extended greatest common divisor.- Specified by:
egcdin interfaceRingElem<BigRational>- Parameters:
S- BigRational.- Returns:
- [ gcd(this,S), a, b ] with a*this + b*S = gcd(this,S).
-
ceil
public java.math.BigInteger ceil()
BigRational ceiling.- Returns:
- ceiling of this.
-
floor
public java.math.BigInteger floor()
BigRational floor.- Returns:
- floor of this.
-
bitLength
public long bitLength()
Returns the number of bits in the representation of this BigRational, including a sign bit. For positive BigRational, this is equivalent tonum.bitLength()+den.bitLength().)- Returns:
- number of bits in the representation of this BigRational, including a sign bit.
-
setAllIterator
public void setAllIterator()
Set the iteration algorithm to all elements.
-
setNonNegativeIterator
public void setNonNegativeIterator()
Set the iteration algorithm to non-negative elements.
-
setNoDuplicatesIterator
public void setNoDuplicatesIterator()
Set the iteration algorithm to no duplicate elements.
-
setDuplicatesIterator
public void setDuplicatesIterator()
Set the iteration algorithm to allow duplicate elements.
-
iterator
public java.util.Iterator<BigRational> iterator()
Get a BigRational iterator.- Specified by:
iteratorin interfacejava.lang.Iterable<BigRational>- Returns:
- a iterator over all rationals.
-
uniqueIterator
public java.util.Iterator<BigRational> uniqueIterator()
Get a BigRational iterator with no duplicates.- Returns:
- a iterator over all rationals without duplicates.
-
-