org.spaceroots.rkcheck
Class QuadraticSurd

java.lang.Object
  extended byorg.spaceroots.rkcheck.QuadraticSurd

public class QuadraticSurd
extends java.lang.Object

This class implements quadratic irrational numbers (which are also called quadratic surds).

Quadratic irrational numbers are roots of quadratic polynoms with integer coefficients. They can be written:

         p1 + p2 sqrt (d)
   z =  ------------------
               q
   
where p1 and p2 are integers, d is a square free strictly positive integer and q is a strictly positive integer. This number is a root of:
     2  2                2       2
    q  X  - 2 q p1 X + p1  - d p2  = 0
   

The set of quadratic irrational numbers associated with a given number d is a field denoted F[sqrt(d)]. This class implements computation within such a field.

Version:
$Id: QuadraticSurd.java,v 1.4 2004/05/23 19:41:36 luc Exp $
Author:
L. Maisonobe

Constructor Summary
QuadraticSurd()
          Simple constructor.
QuadraticSurd(java.math.BigDecimal r, java.math.BigDecimal tolerance)
          Simple constructor.
QuadraticSurd(java.math.BigInteger l)
          Simple constructor.
QuadraticSurd(java.math.BigInteger numerator, java.math.BigInteger denominator)
          Simple constructor.
QuadraticSurd(java.math.BigInteger p1, java.math.BigInteger p2, java.math.BigInteger d, java.math.BigInteger q)
          Simple constructor.
QuadraticSurd(long l)
          Simple constructor.
QuadraticSurd(long numerator, long denominator)
          Simple constructor.
QuadraticSurd(long p1, long p2, long d, long q)
          Simple constructor.
QuadraticSurd(QuadraticSurd qs)
          Copy-constructor.
 
Method Summary
static QuadraticSurd add(QuadraticSurd qs1, QuadraticSurd qs2)
          Add two quadratic surds.
 void addToSelf(QuadraticSurd qs)
          Add a quadratic surd to the instance.
 java.math.BigDecimal bigDecimalValue(int scale)
          Returns the value of the underlying number as a BigDecimal.
static QuadraticSurd divide(QuadraticSurd qs, long l)
          Divide a quadratic surd by an integer
static QuadraticSurd divide(QuadraticSurd qs1, QuadraticSurd qs2)
          Divide two quadratic surds.
 void divideSelf(long l)
          Divide the instance by an integer.
 void divideSelf(QuadraticSurd qs)
          Divide the instance by a quadratic surd.
 double doubleValue()
          Returns the value of the underlying number as a double.
 boolean equals(java.lang.Object o)
          Check if the instance is equal to another quadratic surd.
 java.math.BigInteger getDenominator()
          Get the denominator.
 java.math.BigInteger getRationalNumerator()
          Get the numerator of the rational part.
 java.math.BigInteger getRootCoefficient()
          Get the coefficient of the irrational part.
 java.math.BigInteger getRootElement()
          Get the root element of the irrational part.
 int hashCode()
          Returns a hash code value for the object.
static QuadraticSurd invert(QuadraticSurd qs)
          Invert a quadratic surd.
 void invertSelf()
          Invert the instance.
 boolean isInteger()
          Check if the number is integer.
 boolean isOne()
          Check if the instance is equal to 1.
 boolean isRational()
          Check if the instance holds a rational number.
 boolean isZero()
          Check if the instance is equal to 0.
static QuadraticSurd multiply(QuadraticSurd qs, long l)
          Multiply a quadratic surd by an integer.
static QuadraticSurd multiply(QuadraticSurd qs1, QuadraticSurd qs2)
          Multiply two quadratic surds.
 void multiplySelf(long l)
          Multiply the instance by an integer.
 void multiplySelf(QuadraticSurd qs)
          Multiply the instance by a quadratic surd.
static QuadraticSurd negate(QuadraticSurd qs)
          Negate a quadratic surd.
 void negateSelf()
          Negate the instance
 boolean sameField(QuadraticSurd qs)
          Check if a quadratic surd belong to the same field as the instance.
static QuadraticSurd subtract(QuadraticSurd qs1, QuadraticSurd qs2)
          Subtract two quadratic surds.
 void subtractFromSelf(QuadraticSurd qs)
          Subtract a quadratic surd to the instance.
 java.lang.String toString()
          Returns a string representation of the underlying number.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

QuadraticSurd

public QuadraticSurd()
Simple constructor. Build a null quadratic surd


QuadraticSurd

public QuadraticSurd(long l)
Simple constructor. Build a quadratic surd from a single integer

Parameters:
l - value of the quadratic surd

QuadraticSurd

public QuadraticSurd(java.math.BigInteger l)
Simple constructor. Build a quadratic surd from a single integer

Parameters:
l - value of the quadratic surd

QuadraticSurd

public QuadraticSurd(long numerator,
                     long denominator)
Simple constructor. Build a quadratic surd from a numerator and a denominator. This is a simple rational number.

Parameters:
numerator - numerator of the quadratic surd
denominator - denominator of the quadratic surd
Throws:
java.lang.ArithmeticException - if the denominator is zero

QuadraticSurd

public QuadraticSurd(java.math.BigInteger numerator,
                     java.math.BigInteger denominator)
Simple constructor. Build a quadratic surd from a numerator and a denominator. This is a simple rational number.

Parameters:
numerator - numerator of the quadratic surd
denominator - denominator of the quadratic surd
Throws:
java.lang.ArithmeticException - if the denominator is zero

QuadraticSurd

public QuadraticSurd(long p1,
                     long p2,
                     long d,
                     long q)
Simple constructor. Build a quadratic surd from its coordinates

Parameters:
p1 - integer part of the numerator
p2 - coefficient of the root part of the numerator
d - root
q - denominator
Throws:
java.lang.ArithmeticException - if the root is negative
java.lang.ArithmeticException - if the denominator is zero

QuadraticSurd

public QuadraticSurd(java.math.BigInteger p1,
                     java.math.BigInteger p2,
                     java.math.BigInteger d,
                     java.math.BigInteger q)
Simple constructor. Build a quadratic surd from its coordinates

Parameters:
p1 - integer part of the numerator
p2 - coefficient of the root part of the numerator
d - root
q - denominator
Throws:
java.lang.ArithmeticException - if the root is negative
java.lang.ArithmeticException - if the denominator is zero

QuadraticSurd

public QuadraticSurd(java.math.BigDecimal r,
                     java.math.BigDecimal tolerance)
Simple constructor. Build a quadratic surd by approximating a BigDecimal to a given tolerance using continued fractions

Parameters:
r - real number to approximate
tolerance - tolerance allowed for the approximation

QuadraticSurd

public QuadraticSurd(QuadraticSurd qs)
Copy-constructor.

Parameters:
qs - quadratic surd to copy
Method Detail

sameField

public boolean sameField(QuadraticSurd qs)
Check if a quadratic surd belong to the same field as the instance.

Parameters:
qs - quadratic surd to check against the instance
Returns:
true if the specified quadratic surd belong to the same field as the instance

negateSelf

public void negateSelf()
Negate the instance


negate

public static QuadraticSurd negate(QuadraticSurd qs)
Negate a quadratic surd.

Parameters:
qs - quadratic surd to negate
Returns:
a new quadratic surd which is the opposite of r

addToSelf

public void addToSelf(QuadraticSurd qs)
               throws java.lang.ArithmeticException
Add a quadratic surd to the instance.

Parameters:
qs - quadratic surd to add.
Throws:
java.lang.ArithmeticException - if the two quadratic surds do not belong to the same field (i.e. if they do not share the same root)

add

public static QuadraticSurd add(QuadraticSurd qs1,
                                QuadraticSurd qs2)
                         throws java.lang.ArithmeticException
Add two quadratic surds.

Parameters:
qs1 - first quadratic surd
qs2 - second quadratic surd
Returns:
a new quadratic surd which is the sum of qs1 and qs2
Throws:
java.lang.ArithmeticException - if the two quadratic surds do not belong to the same field (i.e. if they do not share the same root)

subtractFromSelf

public void subtractFromSelf(QuadraticSurd qs)
                      throws java.lang.ArithmeticException
Subtract a quadratic surd to the instance.

Parameters:
qs - quadratic surd to subtract.
Throws:
java.lang.ArithmeticException - if the two quadratic surds do not belong to the same field (i.e. if they do not share the same root)

subtract

public static QuadraticSurd subtract(QuadraticSurd qs1,
                                     QuadraticSurd qs2)
                              throws java.lang.ArithmeticException
Subtract two quadratic surds.

Parameters:
qs1 - first quadratic surd
qs2 - second quadratic surd
Returns:
a new quadratic surd which is the difference qs1 minus qs2
Throws:
java.lang.ArithmeticException - if the two quadratic surds do not belong to the same field (i.e. if they do not share the same root)

multiplySelf

public void multiplySelf(long l)
Multiply the instance by an integer.

Parameters:
l - integer to multiply by

multiply

public static QuadraticSurd multiply(QuadraticSurd qs,
                                     long l)
Multiply a quadratic surd by an integer.

Parameters:
qs - quadratic surd
l - integer to multiply by

multiplySelf

public void multiplySelf(QuadraticSurd qs)
                  throws java.lang.ArithmeticException
Multiply the instance by a quadratic surd.

Parameters:
qs - quadratic surd to multiply by
Throws:
java.lang.ArithmeticException - if the two quadratic surds do not belong to the same field (i.e. if they do not share the same root)

multiply

public static QuadraticSurd multiply(QuadraticSurd qs1,
                                     QuadraticSurd qs2)
                              throws java.lang.ArithmeticException
Multiply two quadratic surds.

Parameters:
qs1 - first quadratic surd
qs2 - second quadratic surd
Returns:
a new quadratic surd which is the product of qs1 and qs2
Throws:
java.lang.ArithmeticException - if the two quadratic surds do not belong to the same field (i.e. if they do not share the same root)

divideSelf

public void divideSelf(long l)
                throws java.lang.ArithmeticException
Divide the instance by an integer.

Parameters:
l - integer to divide by
Throws:
java.lang.ArithmeticException - if l is zero

divide

public static QuadraticSurd divide(QuadraticSurd qs,
                                   long l)
                            throws java.lang.ArithmeticException
Divide a quadratic surd by an integer

Parameters:
qs - quadratic surd
l - integer
Returns:
a new quadratic surd which is the quotient of qs by l
Throws:
java.lang.ArithmeticException - if l is zero

divideSelf

public void divideSelf(QuadraticSurd qs)
                throws java.lang.ArithmeticException
Divide the instance by a quadratic surd.

Parameters:
qs - quadratic surd to divide by
Throws:
java.lang.ArithmeticException - if qs is zero
java.lang.ArithmeticException - if the two quadratic surds do not belong to the same field (i.e. if they do not share the same root)

divide

public static QuadraticSurd divide(QuadraticSurd qs1,
                                   QuadraticSurd qs2)
Divide two quadratic surds.

Parameters:
qs1 - first quadratic surd
qs2 - second quadratic surd
Returns:
a new quadratic surd which is the quotient of qs1 by qs2
Throws:
java.lang.ArithmeticException - if qs2 is zero
java.lang.ArithmeticException - if the two quadratic surds do not belong to the same field (i.e. if they do not share the same root)

invertSelf

public void invertSelf()
                throws java.lang.ArithmeticException
Invert the instance. Replace the instance by its inverse.

Throws:
java.lang.ArithmeticException - if the instance is zero

invert

public static QuadraticSurd invert(QuadraticSurd qs)
                            throws java.lang.ArithmeticException
Invert a quadratic surd.

Parameters:
qs - quadratic surd to invert
Returns:
a new quadratic surd which is the inverse of qs
Throws:
java.lang.ArithmeticException - if qs is zero

isZero

public boolean isZero()
Check if the instance is equal to 0.

Returns:
true if the instance is equal to 0

isOne

public boolean isOne()
Check if the instance is equal to 1.

Returns:
true if the instance is equal to 1

isInteger

public boolean isInteger()
Check if the number is integer.

Returns:
true if the number is an integer

isRational

public boolean isRational()
Check if the instance holds a rational number.

Returns:
true if the instance holds a rational number

equals

public boolean equals(java.lang.Object o)
Check if the instance is equal to another quadratic surd. Equality here is having the same value.

Returns:
true if the object is a quadratic surd which has the same value as the instance

hashCode

public int hashCode()
Returns a hash code value for the object. The hash code value is computed from the reduced coefficents, hence equal quadratic surds have the same hash code, as required by the method specification.

Returns:
a hash code value for this object.

getRationalNumerator

public java.math.BigInteger getRationalNumerator()
Get the numerator of the rational part.

If the instance is a number of the form:

         p1 + p2 sqrt (d)
   z =  ------------------
               q
 
then this method returns p1

Returns:
numerator of the rational part

getRootCoefficient

public java.math.BigInteger getRootCoefficient()
Get the coefficient of the irrational part.

If the instance is a number of the form:

         p1 + p2 sqrt (d)
   z =  ------------------
               q
 
then this method returns p2

Returns:
coefficient of the irrational part

getRootElement

public java.math.BigInteger getRootElement()
Get the root element of the irrational part.

If the instance is a number of the form:

         p1 + p2 sqrt (d)
   z =  ------------------
               q
 
then this method returns d

Returns:
root element of the irrational part

getDenominator

public java.math.BigInteger getDenominator()
Get the denominator.

If the instance is a number of the form:

         p1 + p2 sqrt (d)
   z =  ------------------
               q
 
then this method returns q

Returns:
denominator

doubleValue

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

Returns:
the numeric value represented by this object after conversion to type double.

bigDecimalValue

public java.math.BigDecimal bigDecimalValue(int scale)
Returns the value of the underlying number as a BigDecimal.

Parameters:
scale - scale of the BigDecimal to return
Returns:
the numeric value represented by this object after conversion to type BigDecimal.

toString

public java.lang.String toString()
Returns a string representation of the underlying number.

Returns:
a string representation of the underlying number


Copyright © 2002-2004 Luc Maisonobe. All Rights Reserved.