Class 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
Serialized Form
• Field Summary

Fields
Modifier and Type Field Description
`Ring<E>` `ring`
Ring that numerator and denominator belongs to
• Constructor Summary

Constructors
Constructor Description
`Rationals​(Ring<E> ring)`
• Method Summary

Modifier and Type Method Description
`Rational<E>` ```add​(Rational<E> a, Rational<E> b)```
`BigInteger` `cardinality()`
Returns the number of elements in this ring (cardinality) or null if ring is infinite
`BigInteger` `characteristic()`
Returns characteristic of this ring
`int` ```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 length
`Rational<E>[][]` `createArray2d​(int length)`
Creates 2d array of ring elements of specified length
`Rational<E>[][]` ```createArray2d​(int m, int n)```
Creates 2d array of ring elements of specified shape
`Rational<E>[]` ```divideAndRemainder​(Rational<E> dividend, Rational<E> divider)```
Returns quotient and remainder of `dividend / divider`
`boolean` `equals​(Object o)`
`FactorDecomposition<Rational<E>>` `factor​(Rational<E> element)`
Factor specified element
`FactorDecomposition<Rational<E>>` `factorSquareFree​(Rational<E> element)`
Square-free factorization of specified element
`Rational<E>` ```gcd​(Rational<E> a, Rational<E> b)```
Returns the greatest common divisor of two elements
`Rational<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 ring
`int` `hashCode()`
`boolean` `isEuclideanRing()`
Returns whether this ring is a Euclidean ring
`boolean` `isField()`
Returns whether this ring is a field
`boolean` `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 unit
`boolean` `isZero​(Rational<E> element)`
Tests whether specified element is zero
`Iterator<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 denominator
`Rational<E>` ```mk​(E num, E den)```
Gives rational with a given numerator and denominator
`Rational<E>` `mkDenominator​(long den)`
Gives rational with a given denominator and unit numerator
`Rational<E>` `mkDenominator​(E den)`
Gives rational with a given denominator and unit numerator
`Rational<E>` `mkNumerator​(long num)`
Gives rational with a given numerator and unit denominator
`Rational<E>` `mkNumerator​(E num)`
Gives rational with a given numerator and unit denominator
`Rational<E>` ```multiply​(Rational<E> a, Rational<E> b)```
Multiplies two elements
`Rational<E>` `negate​(Rational<E> element)`
Negates the given element
`BigInteger` `perfectPowerBase()`
Returns `base` so that `cardinality == base^exponent` or null if cardinality is not finite
`BigInteger` `perfectPowerExponent()`
Returns `exponent` so that `cardinality == base^exponent` or null if cardinality is not finite
`Rational<E>` `randomElement​(org.apache.commons.math3.random.RandomGenerator rnd)`
Returns a random element from this ring
`Rational<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 element `element ^ (-1) `
`int` `signum​(Rational<E> element)`
Returns -1 if `element < 0`, 0 if `element == 0` and 1 if `element > 0`, where comparison is specified by `Comparator.compare(Object, Object)`
`Rational<E>` ```subtract​(Rational<E> a, Rational<E> b)```
Subtracts `b` from `a`
`String` `toString()`
`String` `toString​(IStringifier<Rational<E>> stringifier)`
convert this to string with the use of stringifier
`Rational<E>` `valueOf​(long val)`
Returns ring element associated with specified `long`
`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`
• Field Detail

• ring

`public final Ring<E> ring`
Ring that numerator and denominator belongs to
• Constructor Detail

• Rationals

`public Rationals​(Ring<E> ring)`
• 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
• mk

```public Rational<E> mk​(E num,
E den)```
Gives rational with a given numerator and denominator
• mk

```public Rational<E> mk​(long num,
long den)```
Gives rational with a given numerator and denominator
• isField

`public boolean isField()`
Description copied from interface: `Ring`
Returns whether this ring is a field
Specified by:
`isField` in interface `Ring<E>`
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 interface `Ring<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 interface `Ring<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 interface `Ring<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 interface `Ring<E>`
Returns:
whether the cardinality is a perfect power (p^k with k > 1)
• perfectPowerBase

`public BigInteger perfectPowerBase()`
Description copied from interface: `Ring`
Returns `base` so that `cardinality == base^exponent` or null if cardinality is not finite
Specified by:
`perfectPowerBase` in interface `Ring<E>`
Returns:
`base` so that `cardinality == base^exponent` or null if cardinality is not finite
• perfectPowerExponent

`public BigInteger perfectPowerExponent()`
Description copied from interface: `Ring`
Returns `exponent` so that `cardinality == base^exponent` or null if cardinality is not finite
Specified by:
`perfectPowerExponent` in interface `Ring<E>`
Returns:
`exponent` so that `cardinality == base^exponent` or null if cardinality is not finite

```public Rational<E> add​(Rational<E> a,
Rational<E> b)```
Description copied from interface: `Ring`
Specified by:
`add` in interface `Ring<E>`
Parameters:
`a` - the first element
`b` - the second element
Returns:
a + b
• subtract

```public Rational<E> subtract​(Rational<E> a,
Rational<E> b)```
Description copied from interface: `Ring`
Subtracts `b` from `a`
Specified by:
`subtract` in interface `Ring<E>`
Parameters:
`a` - the first element
`b` - the second element
Returns:
a - b
• multiply

```public Rational<E> multiply​(Rational<E> a,
Rational<E> b)```
Description copied from interface: `Ring`
Multiplies two elements
Specified by:
`multiply` in interface `Ring<E>`
Parameters:
`a` - the first element
`b` - the second element
Returns:
a * b
• negate

`public Rational<E> negate​(Rational<E> element)`
Description copied from interface: `Ring`
Negates the given element
Specified by:
`negate` in interface `Ring<E>`
Parameters:
`element` - the ring element
Returns:
-val
• signum

`public int signum​(Rational<E> element)`
Description copied from interface: `Ring`
Returns -1 if `element < 0`, 0 if `element == 0` and 1 if `element > 0`, where comparison is specified by `Comparator.compare(Object, Object)`
Specified by:
`signum` in interface `Ring<E>`
Parameters:
`element` - the element
Returns:
-1 if `element < 0`, 0 if `element == 0` and 1 otherwise
• divideAndRemainder

```public Rational<E>[] divideAndRemainder​(Rational<E> dividend,
Rational<E> divider)```
Description copied from interface: `Ring`
Returns quotient and remainder of `dividend / divider`
Specified by:
`divideAndRemainder` in interface `Ring<E>`
Parameters:
`dividend` - the dividend
`divider` - the divider
Returns:
`{quotient, remainder}`
• reciprocal

`public Rational<E> reciprocal​(Rational<E> element)`
Description copied from interface: `Ring`
Gives the inverse element `element ^ (-1) `
Specified by:
`reciprocal` in interface `Ring<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
Specified by:
`gcd` in interface `Ring<E>`
Parameters:
`a` - the first element
`b` - the second element
Returns:
gcd
• factorSquareFree

`public FactorDecomposition<Rational<E>> factorSquareFree​(Rational<E> element)`
Description copied from interface: `Ring`
Square-free factorization of specified element
Specified by:
`factorSquareFree` in interface `Ring<E>`
• factor

`public FactorDecomposition<Rational<E>> factor​(Rational<E> element)`
Description copied from interface: `Ring`
Factor specified element
Specified by:
`factor` in interface `Ring<E>`
• getZero

`public Rational<E> getZero()`
Description copied from interface: `Ring`
Returns zero element of this ring
Specified by:
`getZero` in interface `Ring<E>`
Returns:
0
• getOne

`public Rational<E> getOne()`
Description copied from interface: `Ring`
Returns unit element of this ring (one)
Specified by:
`getOne` in interface `Ring<E>`
Returns:
1
• isZero

`public boolean isZero​(Rational<E> element)`
Description copied from interface: `Ring`
Tests whether specified element is zero
Specified by:
`isZero` in interface `Ring<E>`
Parameters:
`element` - the ring element
Returns:
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 interface `Ring<E>`
Parameters:
`element` - the ring element
Returns:
whether specified element is exactly one
`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 interface `Ring<E>`
Parameters:
`element` - the ring element
Returns:
whether specified element is a ring unit
`Ring.isOne(Object)`
• valueOf

`public Rational<E> valueOf​(long val)`
Description copied from interface: `Ring`
Returns ring element associated with specified `long`
Specified by:
`valueOf` in interface `Ring<E>`
Parameters:
`val` - machine integer
Returns:
ring element associated with specified `long`
• valueOfBigInteger

`public Rational<E> valueOfBigInteger​(BigInteger val)`
Description copied from interface: `Ring`
Returns ring element associated with specified integer
Specified by:
`valueOfBigInteger` in interface `Ring<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).
Specified by:
`copy` in interface `Ring<E>`
Parameters:
`element` - the element
Returns:
deep copy of specified element
• 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).
Specified by:
`valueOf` in interface `Ring<E>`
Parameters:
`val` - some element from any ring
Returns:
this ring element associated with specified `val`
• 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 interface `Ring<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 interface `Ring<E>`
Parameters:
`m` - result length
`n` - 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 interface `Comparator<E>`
• getNegativeOne

`public Rational<E> getNegativeOne()`
Description copied from interface: `Ring`
Returns negative unit element of this ring (minus one)
Specified by:
`getNegativeOne` in interface `Ring<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 interface `Ring<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 interface `Ring<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 interface `Ring<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)
Specified by:
`iterator` in interface `Iterable<E>`
Specified by:
`iterator` in interface `Ring<E>`
• equals

`public boolean equals​(Object o)`
Specified by:
`equals` in interface `Comparator<E>`
Overrides:
`equals` in class `Object`
• hashCode

`public int hashCode()`
Overrides:
`hashCode` in class `Object`
• 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 interface `Stringifiable<E>`
• toString

`public String toString()`
Overrides:
`toString` in class `Object`