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>, Serializable, Comparable<BigRational>, Iterable<BigRational>
public final class BigRational
extends Object
implements GcdRingElem<BigRational>, RingFactory<BigRational>, Rational, 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.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal BigIntegerDenominator part of the data structure.private booleanstatic final BigRationalThe Constant 1/2.private booleanfinal BigIntegerNumerator part of the data structure.static final BigRationalThe Constant 1.private static final Randomstatic final BigRationalThe Constant 0. -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructor 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.Constructor for a BigRational from jas.arith.BigIntegers.BigRational(BigInteger n, BigInteger d) Constructor for a BigRational from jas.arith.BigIntegers.Constructor for a BigRational from String.Constructor for a BigRational from math.BigIntegers.protectedBigRational(BigInteger n, BigInteger d) Constructor for a BigRational from math.BigIntegers. -
Method Summary
Modifier and TypeMethodDescriptionabs()Rational number absolute value.longReturns the number of bits in the representation of this BigRational, including a sign bit.ceil()BigRational ceiling.Characteristic of this ring.intRational number comparison.copy()Clone this.copy(BigRational c) Copy BigRational element c.Get the denominator.Rational number quotient.doubleGet this as a double.egcd(BigRational S) BigRational extended greatest common divisor.booleanComparison with any other object.factory()Get the corresponding element factory.floor()BigRational floor.fromInteger(long a) Get a BigRational element from a long.Get a BigRational element from a arith.BigInteger.Get a BigRational element from a math.BigInteger.gcd(BigRational S) Rational number greatest common divisor.Get a list of the generating elements.Get the decimal representation.getONE()Get the one element.Return a BigRational approximation of this Element.getZERO()Get the zero element.inthashCode()Hash code for this BigRational.inverse()Rational number inverse.booleanQuery if this ring is associative.booleanQuery 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.iterator()Get a BigRational iterator.Rational number product.negate()Rational number negative.Get the numerator.Parse rational number from Reader.Parse rational number from String.Quotient and remainder by division of this by S.random(int n) Rational number, random.Rational number, random.static BigRationalreduction(BigInteger n, BigInteger d) Rational number reduction to lowest terms.Rational number remainder.static BigRationalRNABS(BigRational R) Rational number absolute value.static intRNCOMP(BigRational R, BigRational S) Rational number comparison.static 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(BigInteger A) Rational number from integer.static BigRationalRNINV(BigRational R) Rational number inverse.static BigRationalRNNEG(BigRational R) Rational number negative.static 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(BigInteger n, BigInteger d) Rational number reduction to lowest terms.static intRational number sign.static BigRationalRNSUM(BigRational R, BigRational S) Rational number sum.voidSet the iteration algorithm to all elements.voidSet the iteration algorithm to allow duplicate elements.voidSet the iteration algorithm to no duplicate elements.voidSet the iteration algorithm to non-negative elements.intsignum()Rational number sign.Rational number difference.sum(BigRational S) Rational number sum.toScript()Get a scripting compatible string representation.Get a scripting compatible string representation of the factory.toString()Get the string representation.toString(int n) Get the decimal string representation with given precision.Get a BigRational iterator with no duplicates.static BigRationalvalueOf(long a) Get a BigRational element from a long.static BigRationalGet a BigRational element from a math.BigInteger.Methods inherited from interface ElemFactory
valueOfMethods inherited from interface Iterable
forEach, spliteratorMethods inherited from interface MonoidElem
leftDivide, leftRemainder, power, rightDivide, rightRemainder, twosidedDivide, twosidedRemainder
-
Field Details
-
num
Numerator part of the data structure. -
den
Denominator part of the data structure. -
ZERO
The Constant 0. -
ONE
The Constant 1. -
HALF
The Constant 1/2. -
random
-
nonNegative
private boolean nonNegative -
duplicates
private boolean duplicates
-
-
Constructor Details
-
BigRational
Constructor for a BigRational from math.BigIntegers.- Parameters:
n- math.BigInteger.d- math.BigInteger.
-
BigRational
Constructor for a BigRational from math.BigIntegers.- Parameters:
n- math.BigInteger.
-
BigRational
Constructor for a BigRational from jas.arith.BigIntegers.- Parameters:
n- edu.jas.arith.BigInteger.
-
BigRational
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
Constructor for a BigRational from String.- Parameters:
s- String.- Throws:
NumberFormatException
-
-
Method Details
-
factory
Get the corresponding element factory.- Specified by:
factoryin interfaceElement<BigRational>- Returns:
- factory for this Element.
- See Also:
-
generators
Get a list of the generating elements.- Specified by:
generatorsin interfaceElemFactory<BigRational>- Returns:
- list of generators for the algebraic structure.
- See Also:
-
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:
-
copy
Clone this.- Specified by:
copyin interfaceElement<BigRational>- Returns:
- Creates and returns a copy of this Element.
- See Also:
-
copy
Copy BigRational element c.- Specified by:
copyin interfaceElemFactory<BigRational>- Parameters:
c- BigRational.- Returns:
- a copy of c.
-
getRational
Return a BigRational approximation of this Element.- Specified by:
getRationalin interfaceRational- Returns:
- a BigRational approximation of this.
- See Also:
-
numerator
-
denominator
-
toString
-
toString
Get the decimal string representation with given precision.- Parameters:
n- precision.- Returns:
- decimal approximation.
-
getDecimal
-
doubleValue
public double doubleValue()Get this as a double.- Returns:
- this as a double
- See Also:
-
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:
-
toScriptFactory
Get a scripting compatible string representation of the factory.- Specified by:
toScriptFactoryin interfaceElement<BigRational>- Returns:
- script compatible representation for this ElemFactory.
- See Also:
-
getZERO
Get the zero element.- Specified by:
getZEROin interfaceAbelianGroupFactory<BigRational>- Returns:
- 0 as 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
Characteristic of this ring.- Specified by:
characteristicin interfaceRingFactory<BigRational>- Returns:
- characteristic of this ring.
-
fromInteger
Get a BigRational element from a math.BigInteger.- Specified by:
fromIntegerin interfaceElemFactory<BigRational>- Parameters:
a- math.BigInteger.- Returns:
- BigRational from a.
-
fromInteger
Get a BigRational element from a arith.BigInteger.- Parameters:
a- arith.BigInteger.- Returns:
- BigRational from a.
-
valueOf
Get a BigRational element from a math.BigInteger.- Parameters:
a- math.BigInteger.- Returns:
- BigRational from a.
-
fromInteger
Get a BigRational element from a long.- Specified by:
fromIntegerin interfaceElemFactory<BigRational>- Parameters:
a- long.- Returns:
- BigRational from a.
-
valueOf
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:
-
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:
-
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:
-
isEntier
public boolean isEntier()Is BigRational entier.- Returns:
- If this is an integer then true is returned, else false.
-
equals
-
hashCode
public int hashCode()Hash code for this BigRational.- Specified by:
hashCodein interfaceElement<BigRational>- Overrides:
hashCodein classObject- Returns:
- the hashCode.
- See Also:
-
RNRED
Rational number reduction to lowest terms.- Parameters:
n- BigInteger.d- BigInteger.- Returns:
- a/b ~ n/d, gcd(a,b) = 1, b > 0.
-
reduction
Rational number reduction to lowest terms.- Parameters:
n- BigInteger.d- BigInteger.- Returns:
- a/b ~ n/d, gcd(a,b) = 1, b > 0.
-
abs
Rational number absolute value.- Specified by:
absin interfaceAbelianGroupElem<BigRational>- Returns:
- the absolute value of this.
- See Also:
-
RNABS
Rational number absolute value.- Parameters:
R- is a rational number.- Returns:
- the absolute value of R.
-
compareTo
Rational number comparison.- Specified by:
compareToin interfaceComparable<BigRational>- Specified by:
compareToin interfaceElement<BigRational>- Parameters:
S- BigRational.- Returns:
- SIGN(this-S).
-
RNCOMP
Rational number comparison.- Parameters:
R- BigRational.S- BigRational.- Returns:
- SIGN(R-S).
-
RNDEN
Rational number denominator.- Parameters:
R- BigRational.- Returns:
- R.denominator().
-
subtract
Rational number difference.- Specified by:
subtractin interfaceAbelianGroupElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this-S.
-
RNDIF
Rational number difference.- Parameters:
R- BigRational.S- BigRational.- Returns:
- R-S.
-
RNDWR
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
Rational number from integer.- Parameters:
A- BigInteger.- Returns:
- A/1.
-
inverse
Rational number inverse.- Specified by:
inversein interfaceMonoidElem<BigRational>- Returns:
- 1/this.
- See Also:
-
RNINV
Rational number inverse.- Parameters:
R- BigRational.- Returns:
- 1/R.
-
negate
Rational number negative.- Specified by:
negatein interfaceAbelianGroupElem<BigRational>- Returns:
- -this.
- See Also:
-
RNNEG
Rational number negative.- Parameters:
R- BigRational.- Returns:
- -R.
-
RNNUM
Rational number numerator.- Parameters:
R- BigRational.- Returns:
- R.numerator().
-
multiply
Rational number product.- Specified by:
multiplyin interfaceMonoidElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this*S.
-
RNPROD
Rational number product.- Parameters:
R- BigRational.S- BigRational.- Returns:
- R*S.
-
divide
Rational number quotient.- Specified by:
dividein interfaceMonoidElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this/S.
-
RNQ
Rational number quotient.- Parameters:
R- BigRational.S- BigRational.- Returns:
- R/S.
-
remainder
Rational number remainder.- Specified by:
remainderin interfaceMonoidElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this-(this/S)*S
-
quotientRemainder
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
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
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
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:
-
RNSIGN
Rational number sign.- Parameters:
R- BigRational.- Returns:
- R.signum().
-
sum
Rational number sum.- Specified by:
sumin interfaceAbelianGroupElem<BigRational>- Parameters:
S- BigRational.- Returns:
- this+S.
-
RNSUM
Rational number sum.- Parameters:
R- BigRational.S- BigRational.- Returns:
- R+S.
-
parse
Parse rational number from String.- Specified by:
parsein interfaceElemFactory<BigRational>- Parameters:
s- String.- Returns:
- BigRational from s.
-
parse
Parse rational number from Reader.- Specified by:
parsein interfaceElemFactory<BigRational>- Parameters:
r- Reader.- Returns:
- next BigRational from r.
-
gcd
Rational number greatest common divisor.- Specified by:
gcdin interfaceRingElem<BigRational>- Parameters:
S- BigRational.- Returns:
- gcd(this,S).
-
egcd
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
-
floor
-
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
Get a BigRational iterator.- Specified by:
iteratorin interfaceIterable<BigRational>- Returns:
- a iterator over all rationals.
-
uniqueIterator
Get a BigRational iterator with no duplicates.- Returns:
- a iterator over all rationals without duplicates.
-