Package cc.redberry.rings
Class Rationals<E>
- java.lang.Object
-
- cc.redberry.rings.Rationals<E>
-
- All Implemented Interfaces:
IParser<Rational<E>>,Stringifiable<Rational<E>>,Ring<Rational<E>>,Serializable,Iterable<Rational<E>>,Comparator<Rational<E>>
public final class Rationals<E> extends Object implements Ring<Rational<E>>
The ring of rationals (Q).- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Rational<E>add(Rational<E> a, Rational<E> b)Add two elementsBigIntegercardinality()Returns the number of elements in this ring (cardinality) or null if ring is infiniteBigIntegercharacteristic()Returns characteristic of this ringintcompare(Rational<E> o1, Rational<E> o2)Rational<E>copy(Rational<E> element)Makes a deep copy of the specified element (for immutable instances the same reference returned).Rational<E>[]createArray(int length)Creates generic array of ring elements of specified lengthRational<E>[][]createArray2d(int length)Creates 2d array of ring elements of specified lengthRational<E>[][]createArray2d(int m, int n)Creates 2d array of ring elements of specified shapeRational<E>[]divideAndRemainder(Rational<E> dividend, Rational<E> divider)Returns quotient and remainder ofdividend / dividerbooleanequals(Object o)FactorDecomposition<Rational<E>>factor(Rational<E> element)Factor specified elementFactorDecomposition<Rational<E>>factorSquareFree(Rational<E> element)Square-free factorization of specified elementRational<E>gcd(Rational<E> a, Rational<E> b)Returns the greatest common divisor of two elementsRational<E>getNegativeOne()Returns negative unit element of this ring (minus one)Rational<E>getOne()Returns unit element of this ring (one)Rational<E>getZero()Returns zero element of this ringinthashCode()booleanisEuclideanRing()Returns whether this ring is a Euclidean ringbooleanisField()Returns whether this ring is a fieldbooleanisOne(Rational<E> element)Tests whether specified element is one (exactly)booleanisPerfectPower()Returns whether the cardinality is a perfect power (p^k with k > 1)booleanisUnit(Rational<E> element)Tests whether specified element is a ring unitbooleanisZero(Rational<E> element)Tests whether specified element is zeroIterator<Rational<E>>iterator()Returns iterator over ring elements (for finite rings, otherwise throws exception)Rational<E>mk(long num, long den)Gives rational with a given numerator and denominatorRational<E>mk(E num, E den)Gives rational with a given numerator and denominatorRational<E>mkDenominator(long den)Gives rational with a given denominator and unit numeratorRational<E>mkDenominator(E den)Gives rational with a given denominator and unit numeratorRational<E>mkNumerator(long num)Gives rational with a given numerator and unit denominatorRational<E>mkNumerator(E num)Gives rational with a given numerator and unit denominatorRational<E>multiply(Rational<E> a, Rational<E> b)Multiplies two elementsRational<E>negate(Rational<E> element)Negates the given elementBigIntegerperfectPowerBase()Returnsbaseso thatcardinality == base^exponentor null if cardinality is not finiteBigIntegerperfectPowerExponent()Returnsexponentso thatcardinality == base^exponentor null if cardinality is not finiteRational<E>randomElement(org.apache.commons.math3.random.RandomGenerator rnd)Returns a random element from this ringRational<E>randomElementTree(org.apache.commons.math3.random.RandomGenerator rnd)If this ring has a complicated nested structure, this method guaranties that the resulting random element will reflect ring complicated structure, i.e.Rational<E>reciprocal(Rational<E> element)Gives the inverse elementelement ^ (-1)intsignum(Rational<E> element)Returns -1 ifelement < 0, 0 ifelement == 0and 1 ifelement > 0, where comparison is specified byComparator.compare(Object, Object)Rational<E>subtract(Rational<E> a, Rational<E> b)SubtractsbfromaStringtoString()StringtoString(IStringifier<Rational<E>> stringifier)convert this to string with the use of stringifierRational<E>valueOf(long val)Returns ring element associated with specifiedlongRational<E>valueOf(Rational<E> val)Converts a value from other ring to this ring.Rational<E>valueOfBigInteger(BigInteger val)Returns ring element associated with specified integer-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Comparator
reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface cc.redberry.rings.Ring
abs, add, addMutable, createArray, createArray, createArray, createZeroesArray, createZeroesArray2d, decrement, divideExact, divideExactMutable, divideOrNull, extendedGCD, factorial, fillZeros, firstBezoutCoefficient, gcd, gcd, increment, isFinite, isFiniteField, isMinusOne, isUnitOrZero, lcm, lcm, lcm, max, min, multiply, multiply, multiply, multiplyMutable, negateMutable, parse, pow, pow, pow, quotient, randomElement, randomElementTree, randomNonZeroElement, remainder, setToValueOf, subtractMutable, valueOf
-
-
-
-
Method Detail
-
mkNumerator
public Rational<E> mkNumerator(E num)
Gives rational with a given numerator and unit denominator
-
mkNumerator
public Rational<E> mkNumerator(long num)
Gives rational with a given numerator and unit denominator
-
mkDenominator
public Rational<E> mkDenominator(E den)
Gives rational with a given denominator and unit numerator
-
mkDenominator
public Rational<E> mkDenominator(long den)
Gives rational with a given denominator and unit numerator
-
isField
public boolean isField()
Description copied from interface:RingReturns whether this ring is a field
-
isEuclideanRing
public boolean isEuclideanRing()
Description copied from interface:RingReturns whether this ring is a Euclidean ring- Specified by:
isEuclideanRingin interfaceRing<E>- Returns:
- whether this ring is a Euclidean ring
-
cardinality
public BigInteger cardinality()
Description copied from interface:RingReturns the number of elements in this ring (cardinality) or null if ring is infinite- Specified by:
cardinalityin interfaceRing<E>- Returns:
- the number of elements in this ring (cardinality) or null if ring is infinite
-
characteristic
public BigInteger characteristic()
Description copied from interface:RingReturns characteristic of this ring- Specified by:
characteristicin interfaceRing<E>- Returns:
- characteristic of this ring
-
isPerfectPower
public boolean isPerfectPower()
Description copied from interface:RingReturns whether the cardinality is a perfect power (p^k with k > 1)- Specified by:
isPerfectPowerin interfaceRing<E>- Returns:
- whether the cardinality is a perfect power (p^k with k > 1)
-
perfectPowerBase
public BigInteger perfectPowerBase()
Description copied from interface:RingReturnsbaseso thatcardinality == base^exponentor null if cardinality is not finite- Specified by:
perfectPowerBasein interfaceRing<E>- Returns:
baseso thatcardinality == base^exponentor null if cardinality is not finite
-
perfectPowerExponent
public BigInteger perfectPowerExponent()
Description copied from interface:RingReturnsexponentso thatcardinality == base^exponentor null if cardinality is not finite- Specified by:
perfectPowerExponentin interfaceRing<E>- Returns:
exponentso thatcardinality == base^exponentor null if cardinality is not finite
-
add
public Rational<E> add(Rational<E> a, Rational<E> b)
Description copied from interface:RingAdd two elements
-
subtract
public Rational<E> subtract(Rational<E> a, Rational<E> b)
Description copied from interface:RingSubtractsbfroma
-
multiply
public Rational<E> multiply(Rational<E> a, Rational<E> b)
Description copied from interface:RingMultiplies two elements
-
negate
public Rational<E> negate(Rational<E> element)
Description copied from interface:RingNegates the given element
-
signum
public int signum(Rational<E> element)
Description copied from interface:RingReturns -1 ifelement < 0, 0 ifelement == 0and 1 ifelement > 0, where comparison is specified byComparator.compare(Object, Object)
-
divideAndRemainder
public Rational<E>[] divideAndRemainder(Rational<E> dividend, Rational<E> divider)
Description copied from interface:RingReturns quotient and remainder ofdividend / divider- Specified by:
divideAndRemainderin interfaceRing<E>- Parameters:
dividend- the dividenddivider- the divider- Returns:
{quotient, remainder}
-
reciprocal
public Rational<E> reciprocal(Rational<E> element)
Description copied from interface:RingGives the inverse elementelement ^ (-1)- Specified by:
reciprocalin interfaceRing<E>- Parameters:
element- the element- Returns:
element ^ (-1)
-
gcd
public Rational<E> gcd(Rational<E> a, Rational<E> b)
Description copied from interface:RingReturns the greatest common divisor of two elements
-
factorSquareFree
public FactorDecomposition<Rational<E>> factorSquareFree(Rational<E> element)
Description copied from interface:RingSquare-free factorization of specified element- Specified by:
factorSquareFreein interfaceRing<E>
-
factor
public FactorDecomposition<Rational<E>> factor(Rational<E> element)
Description copied from interface:RingFactor specified element
-
getZero
public Rational<E> getZero()
Description copied from interface:RingReturns zero element of this ring
-
getOne
public Rational<E> getOne()
Description copied from interface:RingReturns unit element of this ring (one)
-
isZero
public boolean isZero(Rational<E> element)
Description copied from interface:RingTests whether specified element is zero
-
isOne
public boolean isOne(Rational<E> element)
Description copied from interface:RingTests whether specified element is one (exactly)- Specified by:
isOnein interfaceRing<E>- Parameters:
element- the ring element- Returns:
- whether specified element is exactly one
- See Also:
Ring.isUnit(Object)
-
isUnit
public boolean isUnit(Rational<E> element)
Description copied from interface:RingTests whether specified element is a ring unit- Specified by:
isUnitin interfaceRing<E>- Parameters:
element- the ring element- Returns:
- whether specified element is a ring unit
- See Also:
Ring.isOne(Object)
-
valueOf
public Rational<E> valueOf(long val)
Description copied from interface:RingReturns ring element associated with specifiedlong
-
valueOfBigInteger
public Rational<E> valueOfBigInteger(BigInteger val)
Description copied from interface:RingReturns ring element associated with specified integer- Specified by:
valueOfBigIntegerin interfaceRing<E>- Parameters:
val- integer- Returns:
- ring element associated with specified integer
-
copy
public Rational<E> copy(Rational<E> element)
Description copied from interface:RingMakes a deep copy of the specified element (for immutable instances the same reference returned).
-
valueOf
public Rational<E> valueOf(Rational<E> val)
Description copied from interface:RingConverts a value from other ring to this ring. The result is not guarantied to be a new instance (i.e.val == valueOf(val)is possible).
-
createArray2d
public Rational<E>[][] createArray2d(int length)
Description copied from interface:RingCreates 2d array of ring elements of specified length- Specified by:
createArray2din interfaceRing<E>- Parameters:
length- array length- Returns:
- 2d array of ring elements of specified
length
-
createArray2d
public Rational<E>[][] createArray2d(int m, int n)
Description copied from interface:RingCreates 2d array of ring elements of specified shape- Specified by:
createArray2din interfaceRing<E>- Parameters:
m- result lengthn- length of each array in the result- Returns:
- 2d array E[m][n]
-
compare
public int compare(Rational<E> o1, Rational<E> o2)
- Specified by:
comparein interfaceComparator<E>
-
getNegativeOne
public Rational<E> getNegativeOne()
Description copied from interface:RingReturns negative unit element of this ring (minus one)- Specified by:
getNegativeOnein interfaceRing<E>- Returns:
- -1
-
createArray
public Rational<E>[] createArray(int length)
Description copied from interface:RingCreates generic array of ring elements of specified length- Specified by:
createArrayin interfaceRing<E>- Parameters:
length- array length- Returns:
- array of ring elements of specified
length
-
randomElement
public Rational<E> randomElement(org.apache.commons.math3.random.RandomGenerator rnd)
Description copied from interface:RingReturns a random element from this ring- Specified by:
randomElementin interfaceRing<E>- Parameters:
rnd- the source of randomness- Returns:
- random element from this ring
-
randomElementTree
public Rational<E> randomElementTree(org.apache.commons.math3.random.RandomGenerator rnd)
Description copied from interface:RingIf this ring has a complicated nested structure, this method guaranties that the resulting random element will reflect ring complicated structure, i.e. the result will be roughly as complicated as the ring is- Specified by:
randomElementTreein interfaceRing<E>- Returns:
- random element from this ring
-
iterator
public Iterator<Rational<E>> iterator()
Description copied from interface:RingReturns iterator over ring elements (for finite rings, otherwise throws exception)
-
equals
public boolean equals(Object o)
- Specified by:
equalsin interfaceComparator<E>- Overrides:
equalsin classObject
-
toString
public String toString(IStringifier<Rational<E>> stringifier)
Description copied from interface:Stringifiableconvert this to string with the use of stringifier- Specified by:
toStringin interfaceStringifiable<E>
-
-