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 BigIntegerb_MAX_SUPPORTED_MODULUSMax supported modulusstatic longMAX_SUPPORTED_MODULUSMax supported modulus which fits into machine wordstatic intMAX_SUPPORTED_MODULUS_BITSMax supported modulus bits which fits into machine word
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanfits31bitWord(long val)Returns true ifvalfits into 32-bit machine word (unsigned) and false otherwisestatic booleanfits32bitWord(long val)Returns true ifvalfits into 32-bit machine word (unsigned) and false otherwisestatic longgcd(int... integers)Returns the greatest common an array of integersstatic longgcd(int[] integers, int from, int to)Returns the greatest common an array of integersstatic intgcd(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 longgcd(long... integers)Returns the greatest common an array of longsstatic longgcd(long[] integers, int from, int to)Returns the greatest common an array of longsstatic longgcd(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 booleanisOverflowAdd(long x, long y)Tests whether the addition ofx + ywill cause long overflowstatic booleanisOverflowMultiply(long x, long y)Tests whether the multiplication ofx*ywill cause long overflowstatic intlcm(int a, int b)Returns the least common multiple of two integersstatic longlcm(long a, long b)Returns the least common multiple of two longsstatic longmod(long num, long modulus)Delegates toMath.floorMod(long, long)static longmodInverse(long num, long modulus)Returns a solution of congruencenum * x = 1 mod modulusstatic long[]perfectPowerDecomposition(long n)Tests whethernis a perfect powern == a^band returns{a, b}if so andnullotherwisestatic longpowMod(long base, long exponent, long modulus)Returnsbasein a power of non-negativeemodulomodulusstatic longpowModSigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)Returnsbasein a power of non-negativeemodulomagic.modulusstatic longpowModUnsigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)Returnsbasein a power of non-negativeemodulomagic.modulusstatic longsafeAdd(long x, long y)Delegates toMath.addExact(long, long)static longsafeMultiply(long x, long y)Delegates toMath.multiplyExact(long, long)static longsafeMultiply(long x, long y, long z)Delegates toMath.multiplyExact(long, long)static longsafeNegate(long x)Delegates toMath.negateExact(long)static longsafePow(long base, long exponent)Returnsbasein a power ofe(non negative)static longsafeSubtract(long a, long b)Delegates toMath.subtractExact(long, long)static intsafeToInt(long value)Castslongto signedintthrowing exception in case of overflow.static longsymMod(long value, long modulus)Returnsvalue mod modulusin the symmetric representation (-modulus/2 <= result <= modulus/2)static longunsafePow(long base, long exponent)Returnsbasein 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 ifvalfits into 32-bit machine word (unsigned) and false otherwise- Parameters:
val- the value- Returns:
- true if
valfits into 32-bit machine word (unsigned) and false otherwise
-
fits31bitWord
public static boolean fits31bitWord(long val)
Returns true ifvalfits into 32-bit machine word (unsigned) and false otherwise- Parameters:
val- the value- Returns:
- true if
valfits 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*ywill cause long overflow
-
isOverflowAdd
public static boolean isOverflowAdd(long x, long y)Tests whether the addition ofx + ywill cause long overflow
-
safePow
public static long safePow(long base, long exponent)Returnsbasein a power ofe(non negative)- Parameters:
base- baseexponent- exponent (non negative)- Returns:
basein a power ofe- Throws:
ArithmeticException- if the result overflows a long
-
unsafePow
public static long unsafePow(long base, long exponent)Returnsbasein a power ofe(non negative)- Parameters:
base- baseexponent- exponent (non negative)- Returns:
basein 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
aandb
-
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
aandb - 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
aandb
-
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 modulusin the symmetric representation (-modulus/2 <= result <= modulus/2)- Parameters:
value- a longmodulus- modulus- Returns:
value mod modulusin 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-aandmodulusare not coprime
-
safeToInt
public static int safeToInt(long value)
Castslongto signedintthrowing 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)Returnsbasein a power of non-negativeemodulomodulus- Parameters:
base- the baseexponent- the non-negative exponentmodulus- the modulus- Returns:
basein a power ofe
-
powModSigned
public static long powModSigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)Returnsbasein a power of non-negativeemodulomagic.modulus- Parameters:
base- the baseexponent- the non-negative exponentmagic- magic modulus- Returns:
basein a power ofe
-
powModUnsigned
public static long powModUnsigned(long base, long exponent, cc.redberry.libdivide4j.FastDivision.Magic magic)Returnsbasein a power of non-negativeemodulomagic.modulus- Parameters:
base- the baseexponent- the non-negative exponentmagic- magic modulus- Returns:
basein a power ofe
-
perfectPowerDecomposition
public static long[] perfectPowerDecomposition(long n)
Tests whethernis a perfect powern == a^band returns{a, b}if so andnullotherwise- Parameters:
n- the number- Returns:
- array
{a, b}so thatn = a^bornullisnis not a perfect power
-
-