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
Modifier and Type Method Description Rational<E>
add(Rational<E> a, Rational<E> b)
Add two elementsBigInteger
cardinality()
Returns the number of elements in this ring (cardinality) or null if ring is infiniteBigInteger
characteristic()
Returns characteristic of this ringint
compare(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 / divider
boolean
equals(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 ringint
hashCode()
boolean
isEuclideanRing()
Returns whether this ring is a Euclidean ringboolean
isField()
Returns whether this ring is a fieldboolean
isOne(Rational<E> element)
Tests whether specified element is one (exactly)boolean
isPerfectPower()
Returns whether the cardinality is a perfect power (p^k with k > 1)boolean
isUnit(Rational<E> element)
Tests whether specified element is a ring unitboolean
isZero(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 elementBigInteger
perfectPowerBase()
Returnsbase
so thatcardinality == base^exponent
or null if cardinality is not finiteBigInteger
perfectPowerExponent()
Returnsexponent
so thatcardinality == base^exponent
or 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)
int
signum(Rational<E> element)
Returns -1 ifelement < 0
, 0 ifelement == 0
and 1 ifelement > 0
, where comparison is specified byComparator.compare(Object, Object)
Rational<E>
subtract(Rational<E> a, Rational<E> b)
Subtractsb
froma
String
toString()
String
toString(IStringifier<Rational<E>> stringifier)
convert this to string with the use of stringifierRational<E>
valueOf(long val)
Returns ring element associated with specifiedlong
Rational<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:Ring
Returns whether this ring is a field
-
isEuclideanRing
public boolean isEuclideanRing()
Description copied from interface:Ring
Returns whether this ring is a Euclidean ring- Specified by:
isEuclideanRing
in interfaceRing<E>
- Returns:
- whether this ring is a Euclidean ring
-
cardinality
public BigInteger cardinality()
Description copied from interface:Ring
Returns the number of elements in this ring (cardinality) or null if ring is infinite- Specified by:
cardinality
in 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:Ring
Returns characteristic of this ring- Specified by:
characteristic
in interfaceRing<E>
- Returns:
- characteristic of this ring
-
isPerfectPower
public boolean isPerfectPower()
Description copied from interface:Ring
Returns whether the cardinality is a perfect power (p^k with k > 1)- Specified by:
isPerfectPower
in interfaceRing<E>
- Returns:
- whether the cardinality is a perfect power (p^k with k > 1)
-
perfectPowerBase
public BigInteger perfectPowerBase()
Description copied from interface:Ring
Returnsbase
so thatcardinality == base^exponent
or null if cardinality is not finite- Specified by:
perfectPowerBase
in interfaceRing<E>
- Returns:
base
so thatcardinality == base^exponent
or null if cardinality is not finite
-
perfectPowerExponent
public BigInteger perfectPowerExponent()
Description copied from interface:Ring
Returnsexponent
so thatcardinality == base^exponent
or null if cardinality is not finite- Specified by:
perfectPowerExponent
in interfaceRing<E>
- Returns:
exponent
so thatcardinality == base^exponent
or null if cardinality is not finite
-
add
public Rational<E> add(Rational<E> a, Rational<E> b)
Description copied from interface:Ring
Add two elements
-
subtract
public Rational<E> subtract(Rational<E> a, Rational<E> b)
Description copied from interface:Ring
Subtractsb
froma
-
multiply
public Rational<E> multiply(Rational<E> a, Rational<E> b)
Description copied from interface:Ring
Multiplies two elements
-
negate
public Rational<E> negate(Rational<E> element)
Description copied from interface:Ring
Negates the given element
-
signum
public int signum(Rational<E> element)
Description copied from interface:Ring
Returns -1 ifelement < 0
, 0 ifelement == 0
and 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:Ring
Returns quotient and remainder ofdividend / divider
- Specified by:
divideAndRemainder
in interfaceRing<E>
- Parameters:
dividend
- the dividenddivider
- the divider- Returns:
{quotient, remainder}
-
reciprocal
public Rational<E> reciprocal(Rational<E> element)
Description copied from interface:Ring
Gives the inverse elementelement ^ (-1)
- Specified by:
reciprocal
in interfaceRing<E>
- Parameters:
element
- the element- Returns:
element ^ (-1)
-
gcd
public Rational<E> gcd(Rational<E> a, Rational<E> b)
Description copied from interface:Ring
Returns the greatest common divisor of two elements
-
factorSquareFree
public FactorDecomposition<Rational<E>> factorSquareFree(Rational<E> element)
Description copied from interface:Ring
Square-free factorization of specified element- Specified by:
factorSquareFree
in interfaceRing<E>
-
factor
public FactorDecomposition<Rational<E>> factor(Rational<E> element)
Description copied from interface:Ring
Factor specified element
-
getZero
public Rational<E> getZero()
Description copied from interface:Ring
Returns zero element of this ring
-
getOne
public Rational<E> getOne()
Description copied from interface:Ring
Returns unit element of this ring (one)
-
isZero
public boolean isZero(Rational<E> element)
Description copied from interface:Ring
Tests whether specified element is zero
-
isOne
public boolean isOne(Rational<E> element)
Description copied from interface:Ring
Tests whether specified element is one (exactly)- Specified by:
isOne
in 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:Ring
Tests whether specified element is a ring unit- Specified by:
isUnit
in 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:Ring
Returns ring element associated with specifiedlong
-
valueOfBigInteger
public Rational<E> valueOfBigInteger(BigInteger val)
Description copied from interface:Ring
Returns ring element associated with specified integer- Specified by:
valueOfBigInteger
in interfaceRing<E>
- Parameters:
val
- integer- Returns:
- ring element associated with specified integer
-
copy
public Rational<E> copy(Rational<E> element)
Description copied from interface:Ring
Makes 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:Ring
Converts 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:Ring
Creates 2d array of ring elements of specified length- Specified by:
createArray2d
in 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:Ring
Creates 2d array of ring elements of specified shape- Specified by:
createArray2d
in 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:
compare
in interfaceComparator<E>
-
getNegativeOne
public Rational<E> getNegativeOne()
Description copied from interface:Ring
Returns negative unit element of this ring (minus one)- Specified by:
getNegativeOne
in interfaceRing<E>
- Returns:
- -1
-
createArray
public Rational<E>[] createArray(int length)
Description copied from interface:Ring
Creates generic array of ring elements of specified length- Specified by:
createArray
in 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:Ring
Returns a random element from this ring- Specified by:
randomElement
in 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:Ring
If 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:
randomElementTree
in interfaceRing<E>
- Returns:
- random element from this ring
-
iterator
public Iterator<Rational<E>> iterator()
Description copied from interface:Ring
Returns iterator over ring elements (for finite rings, otherwise throws exception)
-
equals
public boolean equals(Object o)
- Specified by:
equals
in interfaceComparator<E>
- Overrides:
equals
in classObject
-
toString
public String toString(IStringifier<Rational<E>> stringifier)
Description copied from interface:Stringifiable
convert this to string with the use of stringifier- Specified by:
toString
in interfaceStringifiable<E>
-
-