edu.jas.arith
Class BigDecimal

java.lang.Object
  extended by edu.jas.arith.BigDecimal
All Implemented Interfaces:
AbelianGroupElem<BigDecimal>, AbelianGroupFactory<BigDecimal>, Element<BigDecimal>, ElemFactory<BigDecimal>, GcdRingElem<BigDecimal>, MonoidElem<BigDecimal>, MonoidFactory<BigDecimal>, RingElem<BigDecimal>, RingFactory<BigDecimal>, java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<BigDecimal>

public final class BigDecimal
extends java.lang.Object
implements GcdRingElem<BigDecimal>, RingFactory<BigDecimal>

BigDecimal class to make java.math.BigDecimal available with RingElem interface. Objects of this class are immutable. Experimental, use with care, compareTo is hacked.

Author:
Heinz Kredel
See Also:
BigDecimal, Serialized Form

Field Summary
 java.math.MathContext context
           
static BigDecimal ONE
          The constant 1.
 java.math.BigDecimal val
          The data structure.
static BigDecimal ZERO
          The constant 0.
 
Constructor Summary
BigDecimal()
          Constructor for BigDecimal without parameters.
BigDecimal(java.math.BigDecimal a)
          Constructor for BigDecimal from math.BigDecimal.
BigDecimal(java.math.BigDecimal a, java.math.MathContext mc)
          Constructor for BigDecimal from math.BigDecimal.
BigDecimal(long a)
          Constructor for BigDecimal from long.
BigDecimal(long a, java.math.MathContext mc)
          Constructor for BigDecimal from long and a context.
BigDecimal(java.lang.String s)
          Constructor for BigDecimal from String.
BigDecimal(java.lang.String s, java.math.MathContext mc)
          Constructor for BigDecimal from String.
 
Method Summary
 BigDecimal abs()
          Absolute value of this.
 java.math.BigInteger characteristic()
          Characteristic of this ring.
 BigDecimal clone()
          Clone this.
 int compareTo(BigDecimal b)
          Compare to BigDecimal b.
 BigDecimal copy(BigDecimal c)
          Copy BigDecimal element c.
 BigDecimal divide(BigDecimal S)
          BigDecimal divide.
 BigDecimal[] divideAndRemainder(BigDecimal S)
          BigDecimal compute quotient and remainder.
 BigDecimal[] egcd(BigDecimal S)
          BigDecimal extended greatest common divisor.
 boolean equals(java.lang.Object b)
          Comparison with any other object.
 BigDecimal fromInteger(java.math.BigInteger a)
          Get a BigDecimal element from a math.BigDecimal.
 BigDecimal fromInteger(long a)
          Get a BigDecimal element from long.
 BigDecimal gcd(BigDecimal S)
          BigDecimal greatest common divisor.
 BigDecimal getONE()
          Get the one element.
 BigDecimal getZERO()
          Get the zero element.
 int hashCode()
          Hash code for this BigDecimal.
 BigDecimal inverse()
          Integer inverse.
 boolean isAssociative()
          Query if this ring is associative.
 boolean isCommutative()
          Query if this ring is commutative.
 boolean isField()
          Query if this ring is a field.
 boolean isONE()
          Is BigDecimal number one.
 boolean isUnit()
          Is BigDecimal number unit.
 boolean isZERO()
          Is BigDecimal number zero.
 BigDecimal multiply(BigDecimal S)
          BigDecimal multiply.
 BigDecimal negate()
          Negate this.
 BigDecimal parse(java.io.Reader r)
          BigDecimal parse from Reader.
 BigDecimal parse(java.lang.String s)
          BigDecimal parse from String.
 BigDecimal random(int n)
          BigDecimal random.
 BigDecimal random(int n, int e)
          BigDecimal random.
 BigDecimal random(int n, int e, java.util.Random rnd)
          BigDecimal random.
 BigDecimal random(int n, java.util.Random rnd)
          BigDecimal random.
 BigDecimal remainder(BigDecimal S)
          BigDecimal remainder.
 int signum()
          signum.
 BigDecimal subtract(BigDecimal S)
          BigDecimal subtract.
 BigDecimal sum(BigDecimal S)
          BigDecimal summation.
 java.lang.String toString()
          Get the String representation.
static BigDecimal valueOf(java.math.BigDecimal a)
          Get a BigDecimal element from a math.BigDecimal.
static BigDecimal valueOf(long a)
          Get a BigDecimal element from long.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

val

public final java.math.BigDecimal val
The data structure.


context

public final java.math.MathContext context

ZERO

public static final BigDecimal ZERO
The constant 0.


ONE

public static final BigDecimal ONE
The constant 1.

Constructor Detail

BigDecimal

public BigDecimal(java.math.BigDecimal a)
Constructor for BigDecimal from math.BigDecimal.

Parameters:
a - java.math.BigDecimal.

BigDecimal

public BigDecimal(java.math.BigDecimal a,
                  java.math.MathContext mc)
Constructor for BigDecimal from math.BigDecimal.

Parameters:
a - java.math.BigDecimal.
mc - MathContext.

BigDecimal

public BigDecimal(long a)
Constructor for BigDecimal from long.

Parameters:
a - long.

BigDecimal

public BigDecimal(long a,
                  java.math.MathContext mc)
Constructor for BigDecimal from long and a context.

Parameters:
a - long.
mc - MathContext.

BigDecimal

public BigDecimal(java.lang.String s)
Constructor for BigDecimal from String.

Parameters:
s - String.

BigDecimal

public BigDecimal(java.lang.String s,
                  java.math.MathContext mc)
Constructor for BigDecimal from String.

Parameters:
s - String.
mc - MathContext.

BigDecimal

public BigDecimal()
Constructor for BigDecimal without parameters.

Method Detail

clone

public BigDecimal clone()
Clone this.

Specified by:
clone in interface Element<BigDecimal>
Overrides:
clone in class java.lang.Object
See Also:
Object.clone()
Returns:
Creates and returns a copy of this Elemnt.

copy

public BigDecimal copy(BigDecimal c)
Copy BigDecimal element c.

Specified by:
copy in interface ElemFactory<BigDecimal>
Parameters:
c - BigDecimal.
Returns:
a copy of c.

getZERO

public BigDecimal getZERO()
Get the zero element.

Specified by:
getZERO in interface AbelianGroupFactory<BigDecimal>
Returns:
0.

getONE

public BigDecimal getONE()
Get the one element.

Specified by:
getONE in interface MonoidFactory<BigDecimal>
Returns:
1.

isCommutative

public boolean isCommutative()
Query if this ring is commutative.

Specified by:
isCommutative in interface MonoidFactory<BigDecimal>
Returns:
true.

isAssociative

public boolean isAssociative()
Query if this ring is associative. Floating point number addition is not associative, but multiplication is.

Specified by:
isAssociative in interface MonoidFactory<BigDecimal>
Returns:
true.

isField

public boolean isField()
Query if this ring is a field.

Specified by:
isField in interface RingFactory<BigDecimal>
Returns:
true.

characteristic

public java.math.BigInteger characteristic()
Characteristic of this ring.

Specified by:
characteristic in interface RingFactory<BigDecimal>
Returns:
characteristic of this ring.

fromInteger

public BigDecimal fromInteger(java.math.BigInteger a)
Get a BigDecimal element from a math.BigDecimal.

Specified by:
fromInteger in interface ElemFactory<BigDecimal>
Parameters:
a - math.BigDecimal.
Returns:
a as BigDecimal.

valueOf

public static BigDecimal valueOf(java.math.BigDecimal a)
Get a BigDecimal element from a math.BigDecimal.

Parameters:
a - math.BigDecimal.
Returns:
a as BigDecimal.

fromInteger

public BigDecimal fromInteger(long a)
Get a BigDecimal element from long.

Specified by:
fromInteger in interface ElemFactory<BigDecimal>
Parameters:
a - long.
Returns:
a as BigDecimal.

valueOf

public static BigDecimal valueOf(long a)
Get a BigDecimal element from long.

Parameters:
a - long.
Returns:
a as BigDecimal.

isZERO

public boolean isZERO()
Is BigDecimal number zero.

Specified by:
isZERO in interface AbelianGroupElem<BigDecimal>
See Also:
AbelianGroupElem.isZERO()
Returns:
If this is 0 then true is returned, else false.

isONE

public boolean isONE()
Is BigDecimal number one.

Specified by:
isONE in interface MonoidElem<BigDecimal>
See Also:
MonoidElem.isONE()
Returns:
true if this is 1, else false.

isUnit

public boolean isUnit()
Is BigDecimal number unit.

Specified by:
isUnit in interface MonoidElem<BigDecimal>
See Also:
MonoidElem.isUnit()
Returns:
true if this is a unit, else false.

toString

public java.lang.String toString()
Get the String representation.

Overrides:
toString in class java.lang.Object
See Also:
Object.toString()

compareTo

public int compareTo(BigDecimal b)
Compare to BigDecimal b. Experimental, is hacked.

Specified by:
compareTo in interface Element<BigDecimal>
Specified by:
compareTo in interface java.lang.Comparable<BigDecimal>
Parameters:
b - BigDecimal.
Returns:
0 if abs(this-b) < epsilon, 1 if this > b, -1 if this < b.

equals

public boolean equals(java.lang.Object b)
Comparison with any other object.

Specified by:
equals in interface Element<BigDecimal>
Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)
Returns:
true if this is equal to b, else false.

hashCode

public int hashCode()
Hash code for this BigDecimal.

Specified by:
hashCode in interface Element<BigDecimal>
Overrides:
hashCode in class java.lang.Object
See Also:
Object.hashCode()
Returns:
the hashCode.

abs

public BigDecimal abs()
Absolute value of this.

Specified by:
abs in interface AbelianGroupElem<BigDecimal>
See Also:
AbelianGroupElem.abs()
Returns:
|this|.

negate

public BigDecimal negate()
Description copied from interface: AbelianGroupElem
Negate this.

Specified by:
negate in interface AbelianGroupElem<BigDecimal>
Returns:
- this.

signum

public int signum()
signum.

Specified by:
signum in interface AbelianGroupElem<BigDecimal>
See Also:
AbelianGroupElem.signum()
Returns:
the sign of this.

subtract

public BigDecimal subtract(BigDecimal S)
BigDecimal subtract.

Specified by:
subtract in interface AbelianGroupElem<BigDecimal>
Parameters:
S - BigDecimal.
Returns:
this-S.

divide

public BigDecimal divide(BigDecimal S)
BigDecimal divide.

Specified by:
divide in interface MonoidElem<BigDecimal>
Parameters:
S - BigDecimal.
Returns:
this/S.

inverse

public BigDecimal inverse()
Integer inverse. R is a non-zero integer. S=1/R if defined else 0.

Specified by:
inverse in interface MonoidElem<BigDecimal>
See Also:
MonoidElem.inverse()
Returns:
x with this * x = 1, if it exists.

remainder

public BigDecimal remainder(BigDecimal S)
BigDecimal remainder.

Specified by:
remainder in interface MonoidElem<BigDecimal>
Parameters:
S - BigDecimal.
Returns:
this - (this/S)*S.

divideAndRemainder

public BigDecimal[] divideAndRemainder(BigDecimal S)
BigDecimal compute quotient and remainder.

Parameters:
S - BigDecimal.
Returns:
BigDecimal[] { q, r } with q = this/S and r = rem(this,S).

gcd

public BigDecimal gcd(BigDecimal S)
BigDecimal greatest common divisor.

Specified by:
gcd in interface RingElem<BigDecimal>
Parameters:
S - BigDecimal.
Returns:
gcd(this,S).

egcd

public BigDecimal[] egcd(BigDecimal S)
BigDecimal extended greatest common divisor.

Specified by:
egcd in interface RingElem<BigDecimal>
Parameters:
S - BigDecimal.
Returns:
[ gcd(this,S), a, b ] with a*this + b*S = gcd(this,S).

random

public BigDecimal random(int n)
BigDecimal random.

Specified by:
random in interface ElemFactory<BigDecimal>
Parameters:
n - such that 0 ≤ val(r) ≤ (2n-1). 0 ≤ exp(r) ≤ (100-1).
Returns:
r, a random BigDecimal.

random

public BigDecimal random(int n,
                         java.util.Random rnd)
BigDecimal random.

Specified by:
random in interface ElemFactory<BigDecimal>
Parameters:
n - such that 0 ≤ val(r) ≤ (2n-1). 0 ≤ exp(r) ≤ (100-1).
rnd - is a source for random bits.
Returns:
r, a random BigDecimal.

random

public BigDecimal random(int n,
                         int e)
BigDecimal random.

Parameters:
n - such that 0 ≤ val(r) ≤ (2n-1).
e - such that 0 ≤ exp(r) ≤ (e-1).
Returns:
r, a random BigDecimal.

random

public BigDecimal random(int n,
                         int e,
                         java.util.Random rnd)
BigDecimal random.

Parameters:
n - such that 0 ≤ val(r) ≤ (2n-1).
e - such that 0 ≤ exp(r) ≤ (e-1).
rnd - is a source for random bits.
Returns:
r, a random BigDecimal.

multiply

public BigDecimal multiply(BigDecimal S)
BigDecimal multiply.

Specified by:
multiply in interface MonoidElem<BigDecimal>
Parameters:
S - BigDecimal.
Returns:
this*S.

sum

public BigDecimal sum(BigDecimal S)
BigDecimal summation.

Specified by:
sum in interface AbelianGroupElem<BigDecimal>
Parameters:
S - BigDecimal.
Returns:
this+S.

parse

public BigDecimal parse(java.lang.String s)
BigDecimal parse from String.

Specified by:
parse in interface ElemFactory<BigDecimal>
Parameters:
s - String.
Returns:
Biginteger from s.

parse

public BigDecimal parse(java.io.Reader r)
BigDecimal parse from Reader.

Specified by:
parse in interface ElemFactory<BigDecimal>
Parameters:
r - Reader.
Returns:
next Biginteger from r.