|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Number
com.dautelle.math.numbers.RealtimeNumber
com.dautelle.math.numbers.LargeInteger
This class represents an immutable integer number of arbitrary size.
It has the following advantages over the
java.math.BigInteger
class:
PoolContext
(e.g. add
operation 5x faster).Operable
interface for modular
arithmetic and can be used in conjonction with the Matrix
class to resolve modulo equations (ref. number theory).
setModulus
,
Karatsuba Multiplication -- from MathWorld,
Serialized FormNested Class Summary | |
static class |
LargeInteger.Value
This inner class represents a mutable image of an immutable LargeInteger . |
Nested classes inherited from class com.dautelle.math.numbers.RealtimeNumber |
RealtimeNumber.Factory |
Field Summary | |
static LargeInteger |
ONE
The large integer representing the multiplicative identity. |
static LargeInteger |
ZERO
The large integer representing the additive identity. |
Method Summary | |
LargeInteger |
abs()
Returns the absolute value of this large integer. |
LargeInteger |
add(LargeInteger that)
Returns the sum of this large integer with the one specified. |
java.lang.StringBuffer |
appendTo(java.lang.StringBuffer sb,
int radix)
Appends the text representation of this large integer in the specified radix to the StringBuffer argument. |
int |
bitLength()
Returns the minimal number of bits to represent this large integer in the minimal two's-complement (sign excluded). |
int |
compareTo(java.lang.Object that)
Compares two large integer numerically. |
LargeInteger |
divide(int i)
Returns this large integer divided by the specified int . |
LargeInteger |
divide(LargeInteger that)
Returns this large integer divided by the one specified (integer division). |
double |
doubleValue()
Returns the value of this large integeras a double . |
LargeInteger |
E(int n)
Returns the product of this large integer with specified power of 10. |
boolean |
equals(java.lang.Object that)
Compares this large integer against the specified object. |
java.lang.Object |
export()
Exports this object out of the current pool context. |
float |
floatValue()
Returns the value of this large integer as a float . |
LargeInteger |
gcd(LargeInteger that)
Returns the greatest common divisor of this large integer and the one specified. |
LargeInteger |
getRemainder()
Returns the final undivided part after division that is less or of lower degree than the divisor. |
int |
hashCode()
Returns the hash code for this large integer number. |
int |
intValue()
Returns the low order bits of this large integer as an int . |
LargeInteger |
inverseScaled(int precision)
Returns a scaled approximation of 1 / this . |
boolean |
isLargerThan(LargeInteger that)
Indicates if this large integer is larger than the one specified in absolute value. |
boolean |
isNegative()
Indicates if this large integer is less than ZERO . |
boolean |
isPositive()
Indicates if this large integer is greater than ZERO
(ZERO is not included). |
boolean |
isZero()
Indicates if this large integer is equal to ZERO . |
long |
longValue()
Returns the low order bits of this large integer as a long . |
LargeInteger |
mod(LargeInteger m)
Returns this large integer modulo the specified large integer. |
LargeInteger |
multiply(LargeInteger that)
Returns the product of this large integer with the one specified. |
LargeInteger |
multiply(long l)
Returns the product of this large integer with the specified long . |
LargeInteger |
negate()
Returns the negation of this large integer. |
Operable |
opposite()
Returns the additive inverse of this object. |
Operable |
plus(Operable that)
Returns the sum of this object with the one specified. |
Operable |
reciprocal()
Returns the modular inverse of this large integer. |
static void |
setModulus(LargeInteger modulus)
Sets the context local modulus for modular arithmetic (used by Operable operations only). |
LargeInteger |
shiftLeft(int n)
Returns the value of this large integer after performing a binary shift to left. |
LargeInteger |
shiftRight(int n)
Returns the value of this large integer after performing a binary shift to right with sign extension (-1 >> 1 == -1) . |
LargeInteger |
subtract(LargeInteger that)
Returns the difference between this large integer and the one specified. |
Operable |
times(Operable that)
Returns the product of this object with the one specified. |
int |
toByteArray(byte[] bytes,
int offset)
Returns the two's-complement binary representation of this large integer. |
java.lang.Object |
toHeap()
Moves this object to the heap. |
java.lang.String |
toString()
Returns the decimal text representation of this large integer. |
java.lang.String |
toString(int radix)
Returns the text representation of this large integer in the specified radix. |
void |
toXml(XmlElement xml)
Sets the attributes and content of the XML element corresponding to this object. |
static LargeInteger |
valueOf(java.math.BigInteger bigInteger)
Returns the large integer corresponding to the specified BigInteger instance. |
static LargeInteger |
valueOf(byte[] bytes,
int offset,
int length)
Returns the large integer of specified two's-complement binary representation. |
static LargeInteger |
valueOf(java.lang.CharSequence chars)
Returns the large integer for the specified character sequence in decimal number. |
static LargeInteger |
valueOf(java.lang.CharSequence chars,
int radix)
Returns the large integer for the specified character sequence stated in the specified radix. |
static LargeInteger |
valueOf(long value)
Returns the large integer of specified long value. |
static LargeInteger |
valueOf(XmlElement xml)
XML factory method. |
Methods inherited from class com.dautelle.math.numbers.RealtimeNumber |
clone, isLocalObject, isPoolObject, pow, recycle |
Methods inherited from class java.lang.Number |
byteValue, shortValue |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final LargeInteger ZERO
public static final LargeInteger ONE
Method Detail |
public static LargeInteger valueOf(long value)
long
value.
value
- the long
value.
public static LargeInteger valueOf(byte[] bytes, int offset, int length)
Note: This representation is consitent with java.lang.BigInteger
byte array representation and can be used for conversion
between the two classes.
bytes
- the binary representation (two's-complement).offset
- the offset at which to start reading the bytes.length
- the maximum number of bytes to read.
java.lang.IndexOutOfBoundsException
- if offset + length > bytes.length
toByteArray(byte[], int)
public static LargeInteger valueOf(java.lang.CharSequence chars)
chars
- the character sequence.
valueOf(chars, 10)
public static LargeInteger valueOf(java.lang.CharSequence chars, int radix)
'+'
or a minus sign '-'
.
chars
- the character sequence to parse.radix
- the radix to be used while parsing.
java.lang.NumberFormatException
- if the specified character sequence does
not contain a parsable large integer.public static LargeInteger valueOf(java.math.BigInteger bigInteger)
BigInteger
instance.
bigInteger
- the big integer instance.
public static LargeInteger valueOf(XmlElement xml)
xml
- the XML element describing the integer to return
(e.g. <math:Integer value="0"/>
).
valueOf(CharSequence)
public boolean isPositive()
ZERO
(ZERO
is not included).
this > ZERO
public boolean isNegative()
ZERO
.
this < ZERO
public boolean isZero()
ZERO
.
this == ZERO
public LargeInteger getRemainder()
divide(int)
operation and is not considered as part of
this large integer (ignored by all methods).
public boolean isLargerThan(LargeInteger that)
that
- the integer to be compared with.
this.abs().compareTo(that.abs()) > 0
.public int toByteArray(byte[] bytes, int offset)
Note: This representation is consitent with java.lang.BigInteger
byte array representation and can be used for conversion
between the two classes.
bytes
- the bytes to hold the binary representation
(two's-complement) of this large integer.offset
- the offset at which to start writing the bytes.
java.lang.IndexOutOfBoundsException
- if bytes.length < (bitLength() >> 3) + 1
valueOf(byte[], int, int)
,
bitLength()
public int bitLength()
public LargeInteger negate()
-this
.public LargeInteger add(LargeInteger that)
that
- the integer to be added.
this + that
.public LargeInteger subtract(LargeInteger that)
that
- the integer to be subtracted.
this - that
.public LargeInteger multiply(long l)
long
.
l
- the long
multiplier.
this * l
.public LargeInteger multiply(LargeInteger that)
that
- the large integer multiplier.
this * that
.public LargeInteger divide(int i)
int
.
The remainder of this division is accessible using getRemainder()
.
i
- the int
divisor.
this / i
and this % i
(getRemainder()
)
java.lang.ArithmeticException
- if i == 0
public LargeInteger divide(LargeInteger that)
reciprocal()
).
The remainder of this division is accessible using getRemainder()
.
that
- the integer divisor.
this / that
and this % that
(getRemainder()
)
java.lang.ArithmeticException
- if that.equals(ZERO)
public LargeInteger inverseScaled(int precision)
1 / this
.
precision
- the requested precision (reciprocal error being ± 1).
2(precision + this.bitLength()) / this
java.lang.ArithmeticException
- if this.isZero()
public LargeInteger mod(LargeInteger m)
m
- the modulus.
this mod m
java.lang.ArithmeticException
- if !m.isPositive()
public LargeInteger gcd(LargeInteger that)
that
- the other number to compute the GCD with.
ZERO
if
(this.isZero() && that.isZero())
.public LargeInteger abs()
abs(this)
.public LargeInteger shiftLeft(int n)
n
, may be negative,
in which case this method performs a right shift.
n
- the shift distance, in bits.
this << n
.shiftRight(int)
public LargeInteger shiftRight(int n)
(-1 >> 1 == -1)
.
The shift distance, n
, may be negative, in which case
this method performs a left shift.
n
- the shift distance, in bits.
this >> n
.shiftLeft(int)
public LargeInteger E(int n)
LargeInteger billion = LargeInteger.ONE.E(9); // 1E9 LargeInteger million = billion.E(-3);
n
- the decimal exponent.
this * 10n
public java.lang.StringBuffer appendTo(java.lang.StringBuffer sb, int radix)
StringBuffer
argument.
sb
- the StringBuffer
to append.radix
- the radix of the representation.
StringBuffer
.public java.lang.String toString()
public java.lang.String toString(int radix)
radix
- the radix of the representation.
public boolean equals(java.lang.Object that)
that
- the object to compare with.
true
if the objects are the same; false
otherwise.public int hashCode()
public int intValue()
int
.
Note: This conversion can lose information about the overall magnitude of the integer value and may return a result with the opposite sign.
int
.public long longValue()
long
.
Note: This conversion can lose information about the overall magnitude of the integer value and may return a result with the opposite sign.
long
.public float floatValue()
float
.
float
.public double doubleValue()
double
.
double
.public int compareTo(java.lang.Object that)
compareTo
in interface java.lang.Comparable
that
- the integer to compare with.
that
.
java.lang.ClassCastException
- that
is not a
large integer.public static void setModulus(LargeInteger modulus)
Operable
operations only). If the modulus is not set
the reciprocal()
operation raises
IllegalStateException
.
modulus
- the new modulus or null
to unset the modulus.
java.lang.IllegalArgumentException
- if modulus <= 0
LocalContext
,
plus
,
opposite
,
times
,
reciprocal
public Operable plus(Operable that)
Operable
plus
in interface Operable
that
- the object to be added.
this + that
.public Operable opposite()
Operable
this.plus(this.opposite()) == ZERO
,
with ZERO
being the additive identity.
opposite
in interface Operable
-this
.public Operable times(Operable that)
Operable
times
in interface Operable
that
- the object multiplier.
this * that
.public Operable reciprocal()
reciprocal
in interface Operable
this.times(this.reciprocal()) modulo m = ONE
m being the local modulus.
java.lang.IllegalStateException
- if the modulus is not set.setModulus(com.dautelle.math.numbers.LargeInteger)
public void toXml(XmlElement xml)
Representable
toXml
in interface Representable
xml
- the XML element to set according to this object's desired XML
representation.public java.lang.Object export()
Realtime
Realtime
members are exported as well (recursion).
This method affects only objects belonging to the current pool context.
To avoid pool depletion, a "free" object from the outer pool
is moved to replace the object being exported.
export
in interface Realtime
export
in class RealtimeNumber
public java.lang.Object toHeap()
Realtime
Realtime
members are moved to the heap as well (recursion).
This method affects only objects belonging to a pool context
(current or not).
toHeap
in interface Realtime
toHeap
in class RealtimeNumber
|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |