JScience v3.3

org.jscience.mathematics.numbers
Class Rational

java.lang.Object
  extended by javolution.context.RealtimeObject
      extended by org.jscience.mathematics.numbers.Number<Rational>
          extended by org.jscience.mathematics.numbers.Rational
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Rational>, Realtime, Immutable, Field<Rational>, GroupAdditive<Rational>, GroupMultiplicative<Rational>, Ring<Rational>, Structure<Rational>

public final class Rational
extends Number<Rational>
implements Field<Rational>

This class represents the ratio of two LargeInteger numbers.

Instances of this class are immutable and can be used to find exact solutions to linear equations with the Matrix class.

Version:
3.0, February 13, 2006
Author:
Jean-Marie Dautelle
See Also:
Wikipedia: Rational Numbers, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javolution.context.RealtimeObject
RealtimeObject.Factory<T extends RealtimeObject>
 
Nested classes/interfaces inherited from interface javolution.context.Realtime
Realtime.ObjectSpace
 
Field Summary
static Rational ONE
          The Rational representing the multiplicative identity.
protected static XMLFormat<Rational> XML
          Holds the default XML representation for rational numbers.
static Rational ZERO
          The Rational representing the additive identity.
 
Method Summary
 Rational abs()
          Returns the absolute value of this rational number.
 int compareTo(Rational that)
          Compares two rational number numerically.
 Rational divide(Rational that)
          Returns this rational number divided by the one specified.
 double doubleValue()
          Returns the value of this rational number as a double.
 boolean equals(java.lang.Object that)
          Compares this rational number against the specified object.
 LargeInteger getDividend()
          Returns the smallest dividend of the fraction representing this rational number.
 LargeInteger getDivisor()
          Returns the smallest divisor of the fraction representing this rational (always positive).
 int hashCode()
          Returns the hash code for this rational number.
 Rational inverse()
          Returns the inverse of this rational number.
 boolean isLargerThan(Rational that)
          Compares the absolute value of two rational numbers.
 long longValue()
          Returns the value of this rational number as a long.
 Rational minus(Rational that)
          Returns the difference between this rational number and the one specified.
 boolean move(Realtime.ObjectSpace os)
          Moves this real-time object to the specified object space.
 Rational opposite()
          Returns the opposite of this rational number.
 Rational plus(Rational that)
          Returns the sum of this rational number with the one specified.
 LargeInteger round()
          Returns the closest integer to this rational number.
 Rational times(Rational that)
          Returns the product of this rational number with the one specified.
 Text toText()
          Returns the decimal text representation of this number.
static Rational valueOf(java.lang.CharSequence chars)
          Returns the rational number for the specified character sequence.
static Rational valueOf(LargeInteger dividend, LargeInteger divisor)
          Returns the rational number for the specified large integer dividend and divisor.
static Rational valueOf(long dividend, long divisor)
          Returns the rational number for the specified integer dividend and divisor.
 
Methods inherited from class org.jscience.mathematics.numbers.Number
byteValue, floatValue, intValue, isGreaterThan, isLessThan, pow, shortValue
 
Methods inherited from class javolution.context.RealtimeObject
export, isLocal, moveHeap, preserve, toString, unpreserve
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

XML

protected static final XMLFormat<Rational> XML
Holds the default XML representation for rational numbers. This representation consists of a simple value attribute holding the textual representation.


ZERO

public static final Rational ZERO
The Rational representing the additive identity.


ONE

public static final Rational ONE
The Rational representing the multiplicative identity.

Method Detail

valueOf

public static Rational valueOf(long dividend,
                               long divisor)
Returns the rational number for the specified integer dividend and divisor.

Parameters:
dividend - the dividend value.
divisor - the divisor value.
Returns:
dividend / divisor
Throws:
java.lang.ArithmeticException - if divisor == 0

valueOf

public static Rational valueOf(LargeInteger dividend,
                               LargeInteger divisor)
Returns the rational number for the specified large integer dividend and divisor.

Parameters:
dividend - the dividend value.
divisor - the divisor value.
Returns:
dividend / divisor
Throws:
java.lang.ArithmeticException - if divisor.isZero()

valueOf

public static Rational valueOf(java.lang.CharSequence chars)
Returns the rational number for the specified character sequence.

Parameters:
chars - the character sequence.
Returns:
the corresponding rational number.

getDividend

public LargeInteger getDividend()
Returns the smallest dividend of the fraction representing this rational number.

Returns:
this rational dividend.

getDivisor

public LargeInteger getDivisor()
Returns the smallest divisor of the fraction representing this rational (always positive).

Returns:
this rational divisor.

round

public LargeInteger round()
Returns the closest integer to this rational number.

Returns:
this rational rounded to the nearest integer.

opposite

public Rational opposite()
Returns the opposite of this rational number.

Specified by:
opposite in interface GroupAdditive<Rational>
Returns:
-this.

plus

public Rational plus(Rational that)
Returns the sum of this rational number with the one specified.

Specified by:
plus in interface GroupAdditive<Rational>
Parameters:
that - the rational number to be added.
Returns:
this + that.

minus

public Rational minus(Rational that)
Returns the difference between this rational number and the one specified.

Overrides:
minus in class Number<Rational>
Parameters:
that - the rational number to be subtracted.
Returns:
this - that.

times

public Rational times(Rational that)
Returns the product of this rational number with the one specified.

Specified by:
times in interface GroupMultiplicative<Rational>
Specified by:
times in interface Ring<Rational>
Parameters:
that - the rational number multiplier.
Returns:
this ยท that.

inverse

public Rational inverse()
Returns the inverse of this rational number.

Specified by:
inverse in interface GroupMultiplicative<Rational>
Returns:
1 / this.
Throws:
java.lang.ArithmeticException - if dividend.isZero()

divide

public Rational divide(Rational that)
Returns this rational number divided by the one specified.

Parameters:
that - the rational number divisor.
Returns:
this / that.
Throws:
java.lang.ArithmeticException - if that.equals(ZERO)

abs

public Rational abs()
Returns the absolute value of this rational number.

Returns:
|this|.

isLargerThan

public boolean isLargerThan(Rational that)
Compares the absolute value of two rational numbers.

Specified by:
isLargerThan in class Number<Rational>
Parameters:
that - the rational number to be compared with.
Returns:
|this| > |that|

toText

public Text toText()
Returns the decimal text representation of this number.

Specified by:
toText in interface Realtime
Specified by:
toText in class Number<Rational>
Returns:
the text representation of this number.

equals

public boolean equals(java.lang.Object that)
Compares this rational number against the specified object.

Specified by:
equals in class Number<Rational>
Parameters:
that - the object to compare with.
Returns:
true if the objects are the same; false otherwise.

hashCode

public int hashCode()
Returns the hash code for this rational number.

Specified by:
hashCode in class Number<Rational>
Returns:
the hash code value.

longValue

public long longValue()
Returns the value of this rational number as a long.

Specified by:
longValue in class Number<Rational>
Returns:
the numeric value represented by this rational after conversion to type long.

doubleValue

public double doubleValue()
Returns the value of this rational number as a double.

Specified by:
doubleValue in class Number<Rational>
Returns:
the numeric value represented by this rational after conversion to type double.

compareTo

public int compareTo(Rational that)
Compares two rational number numerically.

Specified by:
compareTo in interface java.lang.Comparable<Rational>
Specified by:
compareTo in class Number<Rational>
Parameters:
that - the rational number to compare with.
Returns:
-1, 0 or 1 as this rational number is numerically less than, equal to, or greater than that.

move

public boolean move(Realtime.ObjectSpace os)
Description copied from class: Number
Moves this real-time object to the specified object space.

Specified by:
move in interface Realtime
Overrides:
move in class Number<Rational>
Parameters:
os - the object space to move this real-time object to.
Returns:
true if the move has to be propagated to external real-time references; false otherwise.

JScience v3.3

Copyright © 2006 JScience.