BigIntegers using the
FFT algorithm.
This code is based on bigint by Timothy Buktu.
References:
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| public static final double | |
| private static final int | FFT_THRESHOLD
The threshold value for using floating point FFT multiplication. |
| private static final int | MAX_MAG_LENGTH
This constant limits |
| private static volatile FftMultiplier. | ROOTS2_CACHE
Sets of complex roots of unity. |
| private static volatile FftMultiplier. | ROOTS3_CACHE
Sets of complex roots of unity. |
| private static final int | ROOTS3_CACHE_SIZE
for FFTs of length up to 3*2^19 |
| private static final int | ROOTS_CACHE2_SIZE
for FFTs of length up to 2^19 |
| public static final double | |
| private static final int | TOOM_COOK_THRESHOLD
The threshold value for using 3-way Toom-Cook multiplication. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv static int | Returns: the maximum number of bitslength of this number in bits bitLen)Returns the maximum number of bits that one double precision number can fit without causing the multiplication to be incorrect. |
| private static FftMultiplier. | calculateRootsOfUnity(int n)
Returns n-th complex roots of unity for the angles 0..pi/2, suitable for a transform of length n. |
| private static void | fft(FftMultiplier.
input and output, must be a power of two in size a,an array that contains one set of roots at indices
log2(a.length), log2(a.length)-2, log2(a.length)-4, ...
Each roots[s] must contain 2^s roots of unity such that
rootsroots[s][k] = e^(pi*k*i/(2*roots.length)),
i.e., they must cover the first quadrant.Performs an FFT of length 2^n on the vector |
| private static void | fft3(FftMultiplier.
inputs / outputs for the first FFT coefficient a0,inputs / outputs for the second FFT coefficient a1,inputs / outputs for the third FFT coefficient a2,1 for a forward FFT, -1 for an inverse FFT sign, double 1 for a forward FFT, 1/3 for an inverse FFT scale)Performs FFTs or IFFTs of size 3 on the vector |
| private static void | fftMixedRadix(FftMultiplier.
input and output, must be 3*2^n in size for some n>=2 a,an array that contains one set of roots at indices
log2(a.length/3), log2(a.length/3)-2, log2(a.length/3)-4, ...
Each roots[s] must contain 2^s roots of unity such that
roots2,roots[s][k] = e^(pi*k*i/(2*roots.length)),
i.e., they must cover the first quadrant.must be the same length as roots3a and contain roots of
unity such that roots[k] = e^(pi*k*i/(2*roots3.length)),
i.e., they need to cover the first quadrant.Performs an FFT of length 3*2^n on the vector |
| pack-priv static BigInteger | |
| private static FftMultiplier. | |
| private static FftMultiplier. | |
| private static void | ifft(FftMultiplier.
input and output, must be a power of two in size a,an array that contains one set of roots at indices
log2(a.length), log2(a.length)-2, log2(a.length)-4, ...
Each roots[s] must contain 2^s roots of unity such that
rootsroots[s][k] = e^(pi*k*i/(2*roots.length)),
i.e., they must cover the first quadrant.Performs an inverse FFT of length 2^n on the vector |
| private static void | ifftMixedRadix(FftMultiplier.
input and output, must be 3*2^n in size for some n>=2 a,an array that contains one set of roots at indices
log2(a.length/3), log2(a.length/3)-2, log2(a.length/3)-4, ...
Each roots[s] must contain 2^s roots of unity such that
roots2,roots[s][k] = e^(pi*k*i/(2*roots.length)),
i.e., they must cover the first quadrant.must be the same length as roots3a and contain roots of
unity such that roots[k] = e^(pi*k*i/(2*roots3.length)),
i.e., they need to cover the first quadrant.Performs an inverse FFT of length 3*2^n on the vector |
| pack-priv static BigInteger | Returns: this * valvalue a a, BigInteger value b b)Returns a BigInteger whose value is |
| pack-priv static BigInteger | Returns: a*bvalue a a, BigInteger value b b)Multiplies two BigIntegers using a floating-point FFT. |
| pack-priv static BigInteger | Returns: this<sup>2</sup>Returns a BigInteger whose value is |
| pack-priv static BigInteger | |
| pack-priv static FftMultiplier. | toFftVector(byte[] mag, int fftLen, int bitsPerFftPoint)
Converts this BigInteger into an array of complex numbers suitable for an FFT. |
| COS_0_25 | back to summary |
|---|---|
| public static final double COS_0_25 | |
| FFT_THRESHOLD | back to summary |
|---|---|
| private static final int FFT_THRESHOLD The threshold value for using floating point FFT multiplication. If the number of bits in each mag array is greater than the Toom-Cook threshold, and the number of bits in at least one of the mag arrays is greater than this threshold, then FFT multiplication will be used. | |
| MAX_MAG_LENGTH | back to summary |
|---|---|
| private static final int MAX_MAG_LENGTH This constant limits | |
| ROOTS2_CACHE | back to summary |
|---|---|
| private static volatile FftMultiplier. Sets of complex roots of unity. The set at index k contains 2^k elements representing all (2^(k+2))-th roots between 0 and pi/2. Used for FFT multiplication. | |
| ROOTS3_CACHE | back to summary |
|---|---|
| private static volatile FftMultiplier. Sets of complex roots of unity. The set at index k contains 3*2^k elements representing all (3*2^(k+2))-th roots between 0 and pi/2. Used for FFT multiplication. | |
| ROOTS3_CACHE_SIZE | back to summary |
|---|---|
| private static final int ROOTS3_CACHE_SIZE for FFTs of length up to 3*2^19 | |
| ROOTS_CACHE2_SIZE | back to summary |
|---|---|
| private static final int ROOTS_CACHE2_SIZE for FFTs of length up to 2^19 | |
| SIN_0_25 | back to summary |
|---|---|
| public static final double SIN_0_25 | |
| TOOM_COOK_THRESHOLD | back to summary |
|---|---|
| private static final int TOOM_COOK_THRESHOLD The threshold value for using 3-way Toom-Cook multiplication. | |
| FftMultiplier | back to summary |
|---|---|
| pack-priv FftMultiplier() | |
| bitsPerFftPoint | back to summary |
|---|---|
| pack-priv static int bitsPerFftPoint(int bitLen) Returns the maximum number of bits that one double precision number can fit without causing the multiplication to be incorrect.
| |
| calculateRootsOfUnity | back to summary |
|---|---|
| private static FftMultiplier. Returns n-th complex roots of unity for the angles 0..pi/2, suitable for a transform of length n. They are used as twiddle factors and as weights for the right-angle transform. n must be 1 or an even number. | |
| fft | back to summary |
|---|---|
| private static void fft(FftMultiplier. Performs an FFT of length 2^n on the vector
| |
| fft3 | back to summary |
|---|---|
| private static void fft3(FftMultiplier. Performs FFTs or IFFTs of size 3 on the vector
| |
| fftMixedRadix | back to summary |
|---|---|
| private static void fftMixedRadix(FftMultiplier. Performs an FFT of length 3*2^n on the vector
| |
| fromFftVector | back to summary |
|---|---|
| pack-priv static BigInteger fromFftVector(FftMultiplier. | |
| getRootsOfUnity2 | back to summary |
|---|---|
| private static FftMultiplier. Returns sets of complex roots of unity. For k=logN, logN-2, logN-4, ..., the return value contains all k-th roots between 0 and pi/2.
| |
| getRootsOfUnity3 | back to summary |
|---|---|
| private static FftMultiplier. Returns sets of complex roots of unity. For k=logN, logN-2, logN-4, ..., the return value contains all k-th roots between 0 and pi/2.
| |
| ifft | back to summary |
|---|---|
| private static void ifft(FftMultiplier. Performs an inverse FFT of length 2^n on the vector
| |
| ifftMixedRadix | back to summary |
|---|---|
| private static void ifftMixedRadix(FftMultiplier. Performs an inverse FFT of length 3*2^n on the vector
| |
| multiply | back to summary |
|---|---|
| pack-priv static BigInteger multiply(BigInteger a, BigInteger b) Returns a BigInteger whose value is Implementation Note An implementation may offer better algorithmic
performance when
| |
| multiplyFft | back to summary |
|---|---|
| pack-priv static BigInteger multiplyFft(BigInteger a, BigInteger b) Multiplies two BigIntegers using a floating-point FFT. Floating-point math is inaccurate; to ensure the output of the FFT and IFFT rounds to the correct result for every input, the provably safe FFT error bounds from "Rapid Multiplication Modulo The Sum And Difference of Highly Composite Numbers" by Colin Percival, pg. 392 (fft.pdf) are used, the vector is "balanced" before the FFT, and accurate twiddle factors are used. This implementation incorporates several features compared to the standard FFT algorithm (Cooley Tukey FFT algorithm):
| |
| square | back to summary |
|---|---|
| pack-priv static BigInteger square(BigInteger a) Returns a BigInteger whose value is
| |
| squareFft | back to summary |
|---|---|
| pack-priv static BigInteger squareFft(BigInteger a) | |
| toFftVector | back to summary |
|---|---|
| pack-priv static FftMultiplier. Converts this BigInteger into an array of complex numbers suitable for an FFT. Populates the real parts and sets the imaginary parts to zero. | |
| Modifier and Type | Field and Description |
|---|---|
| private final double[] | a
This arrays contains complex numbers. |
| private static final int | COMPLEX_SIZE_SHIFT
A complex number in an FFT double[] vector occupies 2^1 array elements. |
| private static final int | |
| private final int | length
The number of complex numbers stored in this vector. |
| private final int | offset
Offset to the real part of a complex number. |
| private static final int |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv | ComplexVector(FftMultiplier.
the other vector c,start index of the view from, int end index of the view to)Creates a view on another vector. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| pack-priv void | addInto(int idxa, FftMultiplier.
|
| pack-priv void | addTimesIInto(int idxa, FftMultiplier.
|
| pack-priv void | applyInverseWeights(FftMultiplier.
Multiplies the elements of an FFT vector by 1/weight. |
| pack-priv void | applyWeights(FftMultiplier.
Multiplies the elements of an FFT vector by weights. |
| pack-priv void | |
| pack-priv double | |
| pack-priv void | |
| private int | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | multiplyConjugateInto(int idxa, FftMultiplier.
|
| pack-priv void | multiplyConjugateTimesI(int idxa, FftMultiplier.
Multiplies |
| pack-priv void | |
| pack-priv void | |
| pack-priv double | |
| pack-priv double | |
| pack-priv void | |
| private int | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | subtractInto(int idxa, FftMultiplier.
|
| pack-priv void | subtractTimesIInto(int idxa, FftMultiplier.
|
| pack-priv void |
| a | back to summary |
|---|---|
| private final double[] a This arrays contains complex numbers. A complex number occupies 2 consecutive array elements: the real part and then the imaginary part. | |
| COMPLEX_SIZE_SHIFT | back to summary |
|---|---|
| private static final int COMPLEX_SIZE_SHIFT A complex number in an FFT double[] vector occupies 2^1 array elements. | |
| IMAG | back to summary |
|---|---|
| private static final int IMAG | |
| length | back to summary |
|---|---|
| private final int length The number of complex numbers stored in this vector. | |
| offset | back to summary |
|---|---|
| private final int offset Offset to the real part of a complex number. | |
| REAL | back to summary |
|---|---|
| private static final int REAL | |
| ComplexVector | back to summary |
|---|---|
| pack-priv ComplexVector(int length) | |
| ComplexVector | back to summary |
|---|---|
| pack-priv ComplexVector(FftMultiplier. Creates a view on another vector.
| |
| add | back to summary |
|---|---|
| pack-priv void add(int idxa, FftMultiplier. | |
| addInto | back to summary |
|---|---|
| pack-priv void addInto(int idxa, FftMultiplier. | |
| addTimesIInto | back to summary |
|---|---|
| pack-priv void addTimesIInto(int idxa, FftMultiplier. | |
| applyInverseWeights | back to summary |
|---|---|
| pack-priv void applyInverseWeights(FftMultiplier. Multiplies the elements of an FFT vector by 1/weight. Used for the right-angle convolution. | |
| applyWeights | back to summary |
|---|---|
| pack-priv void applyWeights(FftMultiplier. Multiplies the elements of an FFT vector by weights. Doing this makes a regular FFT convolution a right-angle convolution. | |
| copyInto | back to summary |
|---|---|
| pack-priv void copyInto(int idxa, FftMultiplier. | |
| imag | back to summary |
|---|---|
| pack-priv double imag(int idxa) | |
| imag | back to summary |
|---|---|
| pack-priv void imag(int idxa, double value) | |
| imagIdx | back to summary |
|---|---|
| private int imagIdx(int idxa) | |
| multiply | back to summary |
|---|---|
| pack-priv void multiply(int idxa, FftMultiplier. Multiplies | |
| multiplyByIAnd | back to summary |
|---|---|
| pack-priv void multiplyByIAnd(int idxa, FftMultiplier. Multiplies | |
| multiplyConjugate | back to summary |
|---|---|
| pack-priv void multiplyConjugate(int idxa, FftMultiplier. Multiplies | |
| multiplyConjugateInto | back to summary |
|---|---|
| pack-priv void multiplyConjugateInto(int idxa, FftMultiplier. | |
| multiplyConjugateTimesI | back to summary |
|---|---|
| pack-priv void multiplyConjugateTimesI(int idxa, FftMultiplier. Multiplies | |
| multiplyInto | back to summary |
|---|---|
| pack-priv void multiplyInto(int idxa, FftMultiplier. | |
| multiplyPointwise | back to summary |
|---|---|
| pack-priv void multiplyPointwise(FftMultiplier. | |
| part | back to summary |
|---|---|
| pack-priv double part(int idxa, int part) | |
| real | back to summary |
|---|---|
| pack-priv double real(int idxa) | |
| real | back to summary |
|---|---|
| pack-priv void real(int idxa, double value) | |
| realIdx | back to summary |
|---|---|
| private int realIdx(int idxa) | |
| set | back to summary |
|---|---|
| pack-priv void set(int idxa, double real, double imag) | |
| squarePointwise | back to summary |
|---|---|
| pack-priv void squarePointwise() The result is placed in the argument | |
| subtractInto | back to summary |
|---|---|
| pack-priv void subtractInto(int idxa, FftMultiplier. | |
| subtractTimesIInto | back to summary |
|---|---|
| pack-priv void subtractTimesIInto(int idxa, FftMultiplier. | |
| timesTwoToThe | back to summary |
|---|---|
| pack-priv void timesTwoToThe(int idxa, int n) | |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| pack-priv void | |
| pack-priv void | addInto(FftMultiplier.
Adds c to this number and stores the result in destination. |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | addTimesIInto(FftMultiplier.
Adds c*i to this number and stores the result in destination. |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void |
| MutableComplex | back to summary |
|---|---|
| pack-priv MutableComplex() | |
| add | back to summary |
|---|---|
| pack-priv void add(FftMultiplier. | |
| add | back to summary |
|---|---|
| pack-priv void add(FftMultiplier. | |
| addInto | back to summary |
|---|---|
| pack-priv void addInto(FftMultiplier. Adds c to this number and stores the result in destination. Leaves this number unmodified. | |
| addTimesI | back to summary |
|---|---|
| pack-priv void addTimesI(FftMultiplier. Adds c*i to this number. | |
| addTimesI | back to summary |
|---|---|
| pack-priv void addTimesI(FftMultiplier. | |
| addTimesIInto | back to summary |
|---|---|
| pack-priv void addTimesIInto(FftMultiplier. Adds c*i to this number and stores the result in destination. Leaves this number unmodified. | |
| copyInto | back to summary |
|---|---|
| pack-priv void copyInto(FftMultiplier. | |
| multiply | back to summary |
|---|---|
| pack-priv void multiply(FftMultiplier. | |
| multiplyConjugate | back to summary |
|---|---|
| pack-priv void multiplyConjugate(FftMultiplier. multiplies this number by the conjugate of c. | |
| set | back to summary |
|---|---|
| pack-priv void set(FftMultiplier. | |
| squareInto | back to summary |
|---|---|
| pack-priv void squareInto(FftMultiplier. | |
| subtract | back to summary |
|---|---|
| pack-priv void subtract(FftMultiplier. | |
| subtract | back to summary |
|---|---|
| pack-priv void subtract(FftMultiplier. | |
| subtractInto | back to summary |
|---|---|
| pack-priv void subtractInto(FftMultiplier. | |
| subtractInto | back to summary |
|---|---|
| pack-priv void subtractInto(FftMultiplier. | |
| subtractTimesI | back to summary |
|---|---|
| pack-priv void subtractTimesI(FftMultiplier. | |
| subtractTimesI | back to summary |
|---|---|
| pack-priv void subtractTimesI(FftMultiplier. | |
| subtractTimesIInto | back to summary |
|---|---|
| pack-priv void subtractTimesIInto(FftMultiplier. | |