Package org.nevec.rjm
Class Rational
- java.lang.Object
-
- org.nevec.rjm.Rational
-
- All Implemented Interfaces:
java.lang.Cloneable,java.lang.Comparable<Rational>
public class Rational extends java.lang.Object implements java.lang.Cloneable, java.lang.Comparable<Rational>
Fractions (rational numbers). They are divisions of two BigInteger numbers, reduced to coprime numerator and denominator.- Since:
- 2006-06-25
- Author:
- Richard J. Mathar
-
-
Constructor Summary
Constructors Constructor Description Rational()Default ctor, which represents the zero.Rational(int n)ctor from an integer.Rational(int a, int b)ctor from a numerator and denominator.Rational(java.lang.String str)ctor from a string representation.Rational(java.lang.String str, int radix)ctor from a string representation in a specified base.Rational(java.math.BigInteger a)ctor from a numerator.Rational(java.math.BigInteger a, java.math.BigInteger b)ctor from a numerator and denominator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Rationalabs()Absolute value.Rationaladd(int val)Add another integer.Rationaladd(java.math.BigInteger val)Add another integer.Rationaladd(Rational val)Add another fraction.java.math.BigDecimalBigDecimalValue(java.math.MathContext mc)Return a representation as BigDecimal.static Rationalbinomial(Rational n, int m)binomial (n choose m).static Rationalbinomial(Rational n, java.math.BigInteger m)binomial (n choose m).java.math.BigIntegerceil()ceil(): the nearest integer not smaller than this.Rationalclone()Create a copy.intcompareTo(java.math.BigInteger val)Compares the value of this with another constant.intcompareTo(Rational val)Compares the value of this with another constant.java.math.BigIntegerdenom()Get the denominator.Rationaldivide(int val)Divide by an integer.Rationaldivide(java.math.BigInteger val)Divide by an integer.Rationaldivide(Rational val)Divide by another fraction.doubledoubleValue()Return a double value representation.floatfloatValue()Return a float value representation.java.math.BigIntegerfloor()floor(): the nearest integer not greater than this.static RationalhankelSymb(Rational n, int k)Hankel's symbol (n,k)booleanisBigInteger()True if the value is integer.booleanisInteger()True if the value is integer and in the range of the standard integer.booleanisIntegerFrac()True if the value is a fraction of two integers in the range of the standard integer.static java.math.BigIntegerlcmDenom(Rational[] vals)Common lcm of the denominators of a set of rational values.Rationalmax(Rational val)Compares the value of this with another constant.Rationalmin(Rational val)Compares the value of this with another constant.Rationalmultiply(int val)Multiply by an integer.Rationalmultiply(java.math.BigInteger val)Multiply by a BigInteger.Rationalmultiply(Rational val)Multiply by another fraction.Rationalnegate()Compute the negative.protected voidnormalize()Normalize to coprime numerator and denominator.java.math.BigIntegernumer()Get the numerator.RationalPochhammer(int n)Compute pochhammer's symbol (this)_n.RationalPochhammer(java.math.BigInteger n)Compute Pochhammer's symbol (this)_n.Rationalpow(int exponent)Power to an integer.Rationalpow(java.math.BigInteger exponent)Power to an integer.Rationalpow(Rational exponent)Raise to a rational power.Rationalroot(java.math.BigInteger r)r-th root.intsignum()The sign: 1 if the number is >0, 0 if ==0, -1 if <0Rationalsubtract(int val)Subtract an integer.Rationalsubtract(java.math.BigInteger val)Subtract an integer.Rationalsubtract(Rational val)Subtract another fraction.java.lang.StringtoFString(int digits)Return a string in floating point format.java.lang.StringtoString()Return a string in the format number/denom.java.math.BigIntegertrunc()Remove the fractional part.
-
-
-
Field Detail
-
MAX_INT
public static java.math.BigInteger MAX_INT
The maximum and minimum value of a standard Java integer, 2^31.- Since:
- 2009-05-18
-
MIN_INT
public static java.math.BigInteger MIN_INT
-
ZERO
public static Rational ZERO
The constant 0.
-
HALF
public static Rational HALF
The constant 1/2- Since:
- 2010-05-25
-
-
Constructor Detail
-
Rational
public Rational()
Default ctor, which represents the zero.- Since:
- 2007-11-17
-
Rational
public Rational(java.math.BigInteger a, java.math.BigInteger b)ctor from a numerator and denominator.- Parameters:
a- the numerator.b- the denominator.
-
Rational
public Rational(java.math.BigInteger a)
ctor from a numerator.- Parameters:
a- the BigInteger.
-
Rational
public Rational(int a, int b)ctor from a numerator and denominator.- Parameters:
a- the numerator.b- the denominator.
-
Rational
public Rational(int n)
ctor from an integer.- Parameters:
n- the integer to be represented by the new instance.- Since:
- 2010-07-18
-
Rational
public Rational(java.lang.String str) throws java.lang.NumberFormatExceptionctor from a string representation.- Parameters:
str- the string. This either has a slash in it, separating two integers, or, if there is no slash, is representing the numerator with implicit denominator equal to 1. Warning: this does not yet test for a denominator equal to zero- Throws:
java.lang.NumberFormatException
-
Rational
public Rational(java.lang.String str, int radix) throws java.lang.NumberFormatExceptionctor from a string representation in a specified base.- Parameters:
str- the string. This either has a slash in it, separating two integers, or, if there is no slash, is just representing the numerator.radix- the number base for numerator and denominator Warning: this does not yet test for a denominator equal to zero- Throws:
java.lang.NumberFormatException
-
-
Method Detail
-
clone
public Rational clone()
Create a copy.- Overrides:
clonein classjava.lang.Object- Since:
- 2008-11-07
-
multiply
public Rational multiply(Rational val)
Multiply by another fraction.- Parameters:
val- a second rational number.- Returns:
- the product of this with the val.
-
multiply
public Rational multiply(java.math.BigInteger val)
Multiply by a BigInteger.- Parameters:
val- a second number.- Returns:
- the product of this with the value.
-
multiply
public Rational multiply(int val)
Multiply by an integer.- Parameters:
val- a second number.- Returns:
- the product of this with the value.
-
pow
public Rational pow(int exponent)
Power to an integer.- Parameters:
exponent- the exponent.- Returns:
- this value raised to the power given by the exponent. If the exponent is 0, the value 1 is returned.
-
pow
public Rational pow(java.math.BigInteger exponent) throws java.lang.NumberFormatException
Power to an integer.- Parameters:
exponent- the exponent.- Returns:
- this value raised to the power given by the exponent. If the exponent is 0, the value 1 is returned.
- Throws:
java.lang.NumberFormatException- Since:
- 2009-05-18
-
root
public Rational root(java.math.BigInteger r) throws java.lang.NumberFormatException
r-th root.- Parameters:
r- the inverse of the exponent. 2 for the square root, 3 for the third root etc- Returns:
- this value raised to the inverse power given by the root argument, this^(1/r).
- Throws:
java.lang.NumberFormatException- Since:
- 2009-05-18
-
pow
public Rational pow(Rational exponent) throws java.lang.NumberFormatException
Raise to a rational power.- Parameters:
exponent- The exponent.- Returns:
- This value raised to the power given by the exponent. If the exponent is 0, the value 1 is returned.
- Throws:
java.lang.NumberFormatException- Since:
- 2009-05-18
-
divide
public Rational divide(Rational val)
Divide by another fraction.- Parameters:
val- A second rational number.- Returns:
- The value of this/val
-
divide
public Rational divide(java.math.BigInteger val)
Divide by an integer.- Parameters:
val- a second number.- Returns:
- the value of this/val
-
divide
public Rational divide(int val)
Divide by an integer.- Parameters:
val- A second number.- Returns:
- The value of this/val
-
add
public Rational add(Rational val)
Add another fraction.- Parameters:
val- The number to be added- Returns:
- this+val.
-
add
public Rational add(java.math.BigInteger val)
Add another integer.- Parameters:
val- The number to be added- Returns:
- this+val.
-
add
public Rational add(int val)
Add another integer.- Parameters:
val- The number to be added- Returns:
- this+val.
- Since:
- May 26 2010
-
negate
public Rational negate()
Compute the negative.- Returns:
- -this.
-
subtract
public Rational subtract(Rational val)
Subtract another fraction.- Parameters:
val- the number to be subtracted from this- Returns:
- this - val.
-
subtract
public Rational subtract(java.math.BigInteger val)
Subtract an integer.- Parameters:
val- the number to be subtracted from this- Returns:
- this - val.
-
subtract
public Rational subtract(int val)
Subtract an integer.- Parameters:
val- the number to be subtracted from this- Returns:
- this - val.
-
binomial
public static Rational binomial(Rational n, java.math.BigInteger m)
binomial (n choose m).- Parameters:
n- the numerator. Equals the size of the set to choose from.m- the denominator. Equals the number of elements to select.- Returns:
- the binomial coefficient.
- Since:
- 2006-06-27
-
binomial
public static Rational binomial(Rational n, int m)
binomial (n choose m).- Parameters:
n- the numerator. Equals the size of the set to choose from.m- the denominator. Equals the number of elements to select.- Returns:
- the binomial coefficient.
- Since:
- 2009-05-19
-
hankelSymb
public static Rational hankelSymb(Rational n, int k)
Hankel's symbol (n,k)- Parameters:
n- the first parameter.k- the second parameter, greater or equal to 0.- Returns:
- Gamma(n+k+1/2)/k!/GAMMA(n-k+1/2)
- Since:
- 2010-07-18
-
numer
public java.math.BigInteger numer()
Get the numerator.- Returns:
- The numerator of the reduced fraction.
-
denom
public java.math.BigInteger denom()
Get the denominator.- Returns:
- The denominator of the reduced fraction.
-
abs
public Rational abs()
Absolute value.- Returns:
- The absolute (non-negative) value of this.
-
floor
public java.math.BigInteger floor()
floor(): the nearest integer not greater than this.- Returns:
- The integer rounded towards negative infinity.
-
ceil
public java.math.BigInteger ceil()
ceil(): the nearest integer not smaller than this.- Returns:
- The integer rounded towards positive infinity.
- Since:
- 2010-05-26
-
trunc
public java.math.BigInteger trunc()
Remove the fractional part.- Returns:
- The integer rounded towards zero.
-
compareTo
public int compareTo(Rational val)
Compares the value of this with another constant.- Specified by:
compareToin interfacejava.lang.Comparable<Rational>- Parameters:
val- the other constant to compare with- Returns:
- -1, 0 or 1 if this number is numerically less than, equal to, or greater than val.
-
compareTo
public int compareTo(java.math.BigInteger val)
Compares the value of this with another constant.- Parameters:
val- the other constant to compare with- Returns:
- -1, 0 or 1 if this number is numerically less than, equal to, or greater than val.
-
toString
public java.lang.String toString()
Return a string in the format number/denom. If the denominator equals 1, print just the numerator without a slash.- Overrides:
toStringin classjava.lang.Object- Returns:
- the human-readable version in base 10
-
doubleValue
public double doubleValue()
Return a double value representation.- Returns:
- The value with double precision.
- Since:
- 2008-10-26
-
floatValue
public float floatValue()
Return a float value representation.- Returns:
- The value with single precision.
- Since:
- 2009-08-06
-
BigDecimalValue
public java.math.BigDecimal BigDecimalValue(java.math.MathContext mc)
Return a representation as BigDecimal.- Parameters:
mc- the mathematical context which determines precision, rounding mode etc- Returns:
- A representation as a BigDecimal floating point number.
- Since:
- 2008-10-26
-
toFString
public java.lang.String toFString(int digits)
Return a string in floating point format.- Parameters:
digits- The precision (number of digits)- Returns:
- The human-readable version in base 10.
- Since:
- 2008-10-25
-
max
public Rational max(Rational val)
Compares the value of this with another constant.- Parameters:
val- The other constant to compare with- Returns:
- The arithmetic maximum of this and val.
- Since:
- 2008-10-19
-
min
public Rational min(Rational val)
Compares the value of this with another constant.- Parameters:
val- The other constant to compare with- Returns:
- The arithmetic minimum of this and val.
- Since:
- 2008-10-19
-
Pochhammer
public Rational Pochhammer(java.math.BigInteger n)
Compute Pochhammer's symbol (this)_n.- Parameters:
n- The number of product terms in the evaluation.- Returns:
- Gamma(this+n)/Gamma(this) = this*(this+1)*...*(this+n-1).
- Since:
- 2008-10-25
-
Pochhammer
public Rational Pochhammer(int n)
Compute pochhammer's symbol (this)_n.- Parameters:
n- The number of product terms in the evaluation.- Returns:
- Gamma(this+n)/GAMMA(this).
- Since:
- 2008-11-13
-
isBigInteger
public boolean isBigInteger()
True if the value is integer. Equivalent to the indication whether a conversion to an integer can be exact.- Since:
- 2010-05-26
-
isInteger
public boolean isInteger()
True if the value is integer and in the range of the standard integer. Equivalent to the indication whether a conversion to an integer can be exact.- Since:
- 2010-05-26
-
isIntegerFrac
public boolean isIntegerFrac()
True if the value is a fraction of two integers in the range of the standard integer.- Since:
- 2010-05-26
-
signum
public int signum()
The sign: 1 if the number is >0, 0 if ==0, -1 if <0- Returns:
- the signum of the value.
- Since:
- 2010-05-26
-
lcmDenom
public static java.math.BigInteger lcmDenom(Rational[] vals)
Common lcm of the denominators of a set of rational values.- Parameters:
vals- The list/set of the rational values.- Returns:
- LCM(denom of first, denom of second, ..,denom of last)
- Since:
- 2012-03-02
-
normalize
protected void normalize()
Normalize to coprime numerator and denominator. Also copy a negative sign of the denominator to the numerator.- Since:
- 2008-10-19
-
-