Package cc.redberry.rings.poly
Class MachineArithmetic
- java.lang.Object
-
- cc.redberry.rings.poly.MachineArithmetic
-
public final class MachineArithmetic extends Object
Helper methods for arithmetic with machine numbers.- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description static BigInteger
b_MAX_SUPPORTED_MODULUS
Max supported modulusstatic long
MAX_SUPPORTED_MODULUS
Max supported modulus which fits into machine wordstatic int
MAX_SUPPORTED_MODULUS_BITS
Max supported modulus bits which fits into machine word
-
Method Summary
Modifier and Type Method Description static boolean
fits31bitWord(long val)
Returns true ifval
fits into 32-bit machine word (unsigned) and false otherwisestatic boolean
fits32bitWord(long val)
Returns true ifval
fits into 32-bit machine word (unsigned) and false otherwisestatic long
gcd(int... integers)
Returns the greatest common an array of integersstatic long
gcd(int[] integers, int from, int to)
Returns the greatest common an array of integersstatic int
gcd(int p, int q)
Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method.static long
gcd(long... integers)
Returns the greatest common an array of longsstatic long
gcd(long[] integers, int from, int to)
Returns the greatest common an array of longsstatic long
gcd(long p, long q)
Returns the greatest common divisor of two longs.static long[]
gcdExtended(long a, long b)
Runs extended Euclidean algorithm to compute[gcd(a,b), x, y]
such thatx * a + y * b = gcd(a, b)
static boolean
isOverflowAdd(long x, long y)
Tests whether the addition ofx + y
will cause long overflowstatic boolean
isOverflowMultiply(long x, long y)
Tests whether the multiplication ofx*y
will cause long overflowstatic int
lcm(int a, int b)
Returns the least common multiple of two integersstatic long
lcm(long a, long b)
Returns the least common multiple of two longsstatic long
mod(long num, long modulus)
Delegates toMath.floorMod(long, long)
static long
modInverse(long num, long modulus)
Returns a solution of congruencenum * x = 1 mod modulus
static long[]
perfectPowerDecomposition(long n)
Tests whethern
is a perfect powern == a^b
and returns{a, b}
if so andnull
otherwisestatic long
powMod(long base, long exponent, long modulus)
Returnsbase
in a power of non-negativee
modulomodulus
static long
powModSigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)
Returnsbase
in a power of non-negativee
modulomagic.modulus
static long
powModUnsigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)
Returnsbase
in a power of non-negativee
modulomagic.modulus
static long
safeAdd(long x, long y)
Delegates toMath.addExact(long, long)
static long
safeMultiply(long x, long y)
Delegates toMath.multiplyExact(long, long)
static long
safeMultiply(long x, long y, long z)
Delegates toMath.multiplyExact(long, long)
static long
safeNegate(long x)
Delegates toMath.negateExact(long)
static long
safePow(long base, long exponent)
Returnsbase
in a power ofe
(non negative)static long
safeSubtract(long a, long b)
Delegates toMath.subtractExact(long, long)
static int
safeToInt(long value)
Castslong
to signedint
throwing exception in case of overflow.static long
symMod(long value, long modulus)
Returnsvalue mod modulus
in the symmetric representation (-modulus/2 <= result <= modulus/2
)static long
unsafePow(long base, long exponent)
Returnsbase
in a power ofe
(non negative)
-
-
-
Field Detail
-
MAX_SUPPORTED_MODULUS_BITS
public static final int MAX_SUPPORTED_MODULUS_BITS
Max supported modulus bits which fits into machine word- See Also:
- Constant Field Values
-
MAX_SUPPORTED_MODULUS
public static final long MAX_SUPPORTED_MODULUS
Max supported modulus which fits into machine word- See Also:
- Constant Field Values
-
b_MAX_SUPPORTED_MODULUS
public static final BigInteger b_MAX_SUPPORTED_MODULUS
Max supported modulus
-
-
Method Detail
-
fits32bitWord
public static boolean fits32bitWord(long val)
Returns true ifval
fits into 32-bit machine word (unsigned) and false otherwise- Parameters:
val
- the value- Returns:
- true if
val
fits into 32-bit machine word (unsigned) and false otherwise
-
fits31bitWord
public static boolean fits31bitWord(long val)
Returns true ifval
fits into 32-bit machine word (unsigned) and false otherwise- Parameters:
val
- the value- Returns:
- true if
val
fits into 32-bit machine word (unsigned) and false otherwise
-
safeMultiply
public static long safeMultiply(long x, long y)
Delegates toMath.multiplyExact(long, long)
- Throws:
ArithmeticException
- if the result overflows a long
-
safeMultiply
public static long safeMultiply(long x, long y, long z)
Delegates toMath.multiplyExact(long, long)
- Throws:
ArithmeticException
- if the result overflows a long
-
safeAdd
public static long safeAdd(long x, long y)
Delegates toMath.addExact(long, long)
- Throws:
ArithmeticException
- if the result overflows a long
-
safeSubtract
public static long safeSubtract(long a, long b)
Delegates toMath.subtractExact(long, long)
- Throws:
ArithmeticException
- if the result overflows a long
-
safeNegate
public static long safeNegate(long x)
Delegates toMath.negateExact(long)
- Throws:
ArithmeticException
- if the result overflows a long
-
isOverflowMultiply
public static boolean isOverflowMultiply(long x, long y)
Tests whether the multiplication ofx*y
will cause long overflow
-
isOverflowAdd
public static boolean isOverflowAdd(long x, long y)
Tests whether the addition ofx + y
will cause long overflow
-
safePow
public static long safePow(long base, long exponent)
Returnsbase
in a power ofe
(non negative)- Parameters:
base
- baseexponent
- exponent (non negative)- Returns:
base
in a power ofe
- Throws:
ArithmeticException
- if the result overflows a long
-
unsafePow
public static long unsafePow(long base, long exponent)
Returnsbase
in a power ofe
(non negative)- Parameters:
base
- baseexponent
- exponent (non negative)- Returns:
base
in a power ofe
-
gcd
public static long gcd(long p, long q)
Returns the greatest common divisor of two longs.- Parameters:
p
- a longq
- a long- Returns:
- greatest common divisor of
a
andb
-
gcd
public static int gcd(int p, int q)
Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method. See Knuth 4.5.2 algorithm B. The algorithm is due to Josef Stein (1961).
Special cases:- The invocations
gcd(Integer.MIN_VALUE, Integer.MIN_VALUE)
,gcd(Integer.MIN_VALUE, 0)
andgcd(0, Integer.MIN_VALUE)
throw anArithmeticException
, because the result would be 2^31, which is too large for an int value. - The result of
gcd(x, x)
,gcd(0, x)
andgcd(x, 0)
is the absolute value ofx
, except for the special cases above. - The invocation
gcd(0, 0)
is the only one which returns0
.
- Parameters:
p
- Number.q
- Number.- Returns:
- the greatest common divisor (never negative).
- The invocations
-
gcdExtended
public static long[] gcdExtended(long a, long b)
Runs extended Euclidean algorithm to compute[gcd(a,b), x, y]
such thatx * a + y * b = gcd(a, b)
- Parameters:
a
- a longb
- a long- Returns:
- array of
[gcd(a,b), x, y]
such thatx * a + y * b = gcd(a, b)
-
lcm
public static long lcm(long a, long b)
Returns the least common multiple of two longs- Parameters:
a
- a longb
- a long- Returns:
- least common multiple of
a
andb
- Throws:
ArithmeticException
- if the result overflows a long
-
lcm
public static int lcm(int a, int b)
Returns the least common multiple of two integers- Parameters:
a
- a numberb
- a number- Returns:
- least common multiple of
a
andb
-
gcd
public static long gcd(long[] integers, int from, int to)
Returns the greatest common an array of longs- Parameters:
integers
- array of longsfrom
- from position (inclusive)to
- to position (exclusive)- Returns:
- greatest common divisor of array
-
gcd
public static long gcd(long... integers)
Returns the greatest common an array of longs- Parameters:
integers
- array of longs- Returns:
- greatest common divisor of array
-
gcd
public static long gcd(int[] integers, int from, int to)
Returns the greatest common an array of integers- Parameters:
integers
- array of integersfrom
- from position (inclusive)to
- to position (exclusive)- Returns:
- greatest common divisor of array
-
gcd
public static long gcd(int... integers)
Returns the greatest common an array of integers- Parameters:
integers
- array of integers- Returns:
- greatest common divisor of array
-
mod
public static long mod(long num, long modulus)
Delegates toMath.floorMod(long, long)
-
symMod
public static long symMod(long value, long modulus)
Returnsvalue mod modulus
in the symmetric representation (-modulus/2 <= result <= modulus/2
)- Parameters:
value
- a longmodulus
- modulus- Returns:
value mod modulus
in the symmetric representation (-modulus/2 <= result <= modulus/2
)
-
modInverse
public static long modInverse(long num, long modulus)
Returns a solution of congruencenum * x = 1 mod modulus
- Parameters:
num
- basemodulus
- modulus- Returns:
a^(-1) mod p
- Throws:
IllegalArgumentException
-a
andmodulus
are not coprime
-
safeToInt
public static int safeToInt(long value)
Castslong
to signedint
throwing exception in case of overflow.- Parameters:
value
- the long- Returns:
- int value
- Throws:
ArithmeticException
- if the result overflows a long
-
powMod
public static long powMod(long base, long exponent, long modulus)
Returnsbase
in a power of non-negativee
modulomodulus
- Parameters:
base
- the baseexponent
- the non-negative exponentmodulus
- the modulus- Returns:
base
in a power ofe
-
powModSigned
public static long powModSigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)
Returnsbase
in a power of non-negativee
modulomagic.modulus
- Parameters:
base
- the baseexponent
- the non-negative exponentmagic
- magic modulus- Returns:
base
in a power ofe
-
powModUnsigned
public static long powModUnsigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)
Returnsbase
in a power of non-negativee
modulomagic.modulus
- Parameters:
base
- the baseexponent
- the non-negative exponentmagic
- magic modulus- Returns:
base
in a power ofe
-
perfectPowerDecomposition
public static long[] perfectPowerDecomposition(long n)
Tests whethern
is a perfect powern == a^b
and returns{a, b}
if so andnull
otherwise- Parameters:
n
- the number- Returns:
- array
{a, b}
so thatn = a^b
ornull
isn
is not a perfect power
-
-