org.spaceroots.rkcheck
Class QuadraticSurd
- java.lang.Object
-
- org.spaceroots.rkcheck.QuadraticSurd
-
public class QuadraticSurd extends java.lang.ObjectThis 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 = ------------------ qwhere 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 = 0The 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.
-
-
Constructor Summary
Constructors Constructor and Description 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description static QuadraticSurdadd(QuadraticSurd qs1, QuadraticSurd qs2)Add two quadratic surds.voidaddToSelf(QuadraticSurd qs)Add a quadratic surd to the instance.java.math.BigDecimalbigDecimalValue(int scale)Returns the value of the underlying number as a BigDecimal.static QuadraticSurddivide(QuadraticSurd qs, long l)Divide a quadratic surd by an integerstatic QuadraticSurddivide(QuadraticSurd qs1, QuadraticSurd qs2)Divide two quadratic surds.voiddivideSelf(long l)Divide the instance by an integer.voiddivideSelf(QuadraticSurd qs)Divide the instance by a quadratic surd.doubledoubleValue()Returns the value of the underlying number as a double.booleanequals(java.lang.Object o)Check if the instance is equal to another quadratic surd.java.math.BigIntegergetDenominator()Get the denominator.java.math.BigIntegergetRationalNumerator()Get the numerator of the rational part.java.math.BigIntegergetRootCoefficient()Get the coefficient of the irrational part.java.math.BigIntegergetRootElement()Get the root element of the irrational part.inthashCode()Returns a hash code value for the object.static QuadraticSurdinvert(QuadraticSurd qs)Invert a quadratic surd.voidinvertSelf()Invert the instance.booleanisInteger()Check if the number is integer.booleanisOne()Check if the instance is equal to 1.booleanisRational()Check if the instance holds a rational number.booleanisZero()Check if the instance is equal to 0.static QuadraticSurdmultiply(QuadraticSurd qs, long l)Multiply a quadratic surd by an integer.static QuadraticSurdmultiply(QuadraticSurd qs1, QuadraticSurd qs2)Multiply two quadratic surds.voidmultiplySelf(long l)Multiply the instance by an integer.voidmultiplySelf(QuadraticSurd qs)Multiply the instance by a quadratic surd.static QuadraticSurdnegate(QuadraticSurd qs)Negate a quadratic surd.voidnegateSelf()Negate the instancebooleansameField(QuadraticSurd qs)Check if a quadratic surd belong to the same field as the instance.static QuadraticSurdsubtract(QuadraticSurd qs1, QuadraticSurd qs2)Subtract two quadratic surds.voidsubtractFromSelf(QuadraticSurd qs)Subtract a quadratic surd to the instance.java.lang.StringtoString()Returns a string representation of the underlying number.
-
-
-
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 surddenominator- 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 surddenominator- 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 numeratorp2- coefficient of the root part of the numeratord- rootq- denominator- Throws:
java.lang.ArithmeticException- if the root is negativejava.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 numeratorp2- coefficient of the root part of the numeratord- rootq- denominator- Throws:
java.lang.ArithmeticException- if the root is negativejava.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 approximatetolerance- 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 surdqs2- 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 surdqs2- 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 surdl- 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 surdqs2- 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.ArithmeticExceptionDivide 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 surdl- 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 zerojava.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 surdqs2- second quadratic surd- Returns:
- a new quadratic surd which is the quotient of qs1 by qs2
- Throws:
java.lang.ArithmeticException- if qs2 is zerojava.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.ArithmeticExceptionInvert 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.- Overrides:
equalsin classjava.lang.Object- 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.- Overrides:
hashCodein classjava.lang.Object- 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 = ------------------ qthen 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 = ------------------ qthen 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 = ------------------ qthen 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 = ------------------ qthen 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.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of the underlying number
-
-
DataMelt 3.0 © DataMelt by jWork.ORG