Formatting (MATH-677).

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1208309 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastien Brisard 2011-11-30 07:22:28 +00:00
parent b7c7b8934a
commit 31d0c4eff6
1 changed files with 89 additions and 91 deletions

View File

@ -49,12 +49,10 @@ public class FastFourierTransformer implements Serializable {
/** Serializable version identifier. */ /** Serializable version identifier. */
static final long serialVersionUID = 5138259215438106000L; static final long serialVersionUID = 5138259215438106000L;
/** roots of unity */ /** The roots of unity. */
private RootsOfUnity roots = new RootsOfUnity(); private RootsOfUnity roots = new RootsOfUnity();
/** /** Construct a default transformer. */
* Construct a default transformer.
*/
public FastFourierTransformer() { public FastFourierTransformer() {
super(); super();
} }
@ -69,7 +67,7 @@ public class FastFourierTransformer implements Serializable {
* @return the complex transformed array * @return the complex transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
public Complex[] transform(double f[]) public Complex[] transform(double[] f)
throws IllegalArgumentException { throws IllegalArgumentException {
return fft(f, false); return fft(f, false);
} }
@ -90,7 +88,7 @@ public class FastFourierTransformer implements Serializable {
public Complex[] transform(UnivariateFunction f, public Complex[] transform(UnivariateFunction f,
double min, double max, int n) double min, double max, int n)
throws IllegalArgumentException { throws IllegalArgumentException {
double data[] = sample(f, min, max, n); double[] data = sample(f, min, max, n);
return fft(data, false); return fft(data, false);
} }
@ -104,7 +102,7 @@ public class FastFourierTransformer implements Serializable {
* @return the complex transformed array * @return the complex transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
public Complex[] transform(Complex f[]) public Complex[] transform(Complex[] f)
throws IllegalArgumentException { throws IllegalArgumentException {
roots.computeOmega(f.length); roots.computeOmega(f.length);
return fft(f); return fft(f);
@ -120,11 +118,11 @@ public class FastFourierTransformer implements Serializable {
* @return the complex transformed array * @return the complex transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
public Complex[] transform2(double f[]) public Complex[] transform2(double[] f)
throws IllegalArgumentException { throws IllegalArgumentException {
double scaling_coefficient = 1.0 / FastMath.sqrt(f.length); double scalingCoefficient = 1.0 / FastMath.sqrt(f.length);
return scaleArray(fft(f, false), scaling_coefficient); return scaleArray(fft(f, false), scalingCoefficient);
} }
/** /**
@ -144,9 +142,9 @@ public class FastFourierTransformer implements Serializable {
double min, double max, int n) double min, double max, int n)
throws IllegalArgumentException { throws IllegalArgumentException {
double data[] = sample(f, min, max, n); double[] data = sample(f, min, max, n);
double scaling_coefficient = 1.0 / FastMath.sqrt(n); double scalingCoefficient = 1.0 / FastMath.sqrt(n);
return scaleArray(fft(data, false), scaling_coefficient); return scaleArray(fft(data, false), scalingCoefficient);
} }
/** /**
@ -159,12 +157,12 @@ public class FastFourierTransformer implements Serializable {
* @return the complex transformed array * @return the complex transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
public Complex[] transform2(Complex f[]) public Complex[] transform2(Complex[] f)
throws IllegalArgumentException { throws IllegalArgumentException {
roots.computeOmega(f.length); roots.computeOmega(f.length);
double scaling_coefficient = 1.0 / FastMath.sqrt(f.length); double scalingCoefficient = 1.0 / FastMath.sqrt(f.length);
return scaleArray(fft(f), scaling_coefficient); return scaleArray(fft(f), scalingCoefficient);
} }
/** /**
@ -177,11 +175,11 @@ public class FastFourierTransformer implements Serializable {
* @return the complex inversely transformed array * @return the complex inversely transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
public Complex[] inverseTransform(double f[]) public Complex[] inverseTransform(double[] f)
throws IllegalArgumentException { throws IllegalArgumentException {
double scaling_coefficient = 1.0 / f.length; double scalingCoefficient = 1.0 / f.length;
return scaleArray(fft(f, true), scaling_coefficient); return scaleArray(fft(f, true), scalingCoefficient);
} }
/** /**
@ -201,9 +199,9 @@ public class FastFourierTransformer implements Serializable {
double min, double max, int n) double min, double max, int n)
throws IllegalArgumentException { throws IllegalArgumentException {
double data[] = sample(f, min, max, n); double[] data = sample(f, min, max, n);
double scaling_coefficient = 1.0 / n; double scalingCoefficient = 1.0 / n;
return scaleArray(fft(data, true), scaling_coefficient); return scaleArray(fft(data, true), scalingCoefficient);
} }
/** /**
@ -216,12 +214,12 @@ public class FastFourierTransformer implements Serializable {
* @return the complex inversely transformed array * @return the complex inversely transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
public Complex[] inverseTransform(Complex f[]) public Complex[] inverseTransform(Complex[] f)
throws IllegalArgumentException { throws IllegalArgumentException {
roots.computeOmega(-f.length); // pass negative argument roots.computeOmega(-f.length); // pass negative argument
double scaling_coefficient = 1.0 / f.length; double scalingCoefficient = 1.0 / f.length;
return scaleArray(fft(f), scaling_coefficient); return scaleArray(fft(f), scalingCoefficient);
} }
/** /**
@ -234,11 +232,11 @@ public class FastFourierTransformer implements Serializable {
* @return the complex inversely transformed array * @return the complex inversely transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
public Complex[] inverseTransform2(double f[]) public Complex[] inverseTransform2(double[] f)
throws IllegalArgumentException { throws IllegalArgumentException {
double scaling_coefficient = 1.0 / FastMath.sqrt(f.length); double scalingCoefficient = 1.0 / FastMath.sqrt(f.length);
return scaleArray(fft(f, true), scaling_coefficient); return scaleArray(fft(f, true), scalingCoefficient);
} }
/** /**
@ -258,9 +256,9 @@ public class FastFourierTransformer implements Serializable {
double min, double max, int n) double min, double max, int n)
throws IllegalArgumentException { throws IllegalArgumentException {
double data[] = sample(f, min, max, n); double[] data = sample(f, min, max, n);
double scaling_coefficient = 1.0 / FastMath.sqrt(n); double scalingCoefficient = 1.0 / FastMath.sqrt(n);
return scaleArray(fft(data, true), scaling_coefficient); return scaleArray(fft(data, true), scalingCoefficient);
} }
/** /**
@ -273,12 +271,12 @@ public class FastFourierTransformer implements Serializable {
* @return the complex inversely transformed array * @return the complex inversely transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
public Complex[] inverseTransform2(Complex f[]) public Complex[] inverseTransform2(Complex[] f)
throws IllegalArgumentException { throws IllegalArgumentException {
roots.computeOmega(-f.length); // pass negative argument roots.computeOmega(-f.length); // pass negative argument
double scaling_coefficient = 1.0 / FastMath.sqrt(f.length); double scalingCoefficient = 1.0 / FastMath.sqrt(f.length);
return scaleArray(fft(f), scaling_coefficient); return scaleArray(fft(f), scalingCoefficient);
} }
/** /**
@ -289,42 +287,42 @@ public class FastFourierTransformer implements Serializable {
* @return the complex transformed array * @return the complex transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
protected Complex[] fft(double f[], boolean isInverse) protected Complex[] fft(double[] f, boolean isInverse)
throws IllegalArgumentException { throws IllegalArgumentException {
verifyDataSet(f); verifyDataSet(f);
Complex F[] = new Complex[f.length]; Complex[] transformed = new Complex[f.length];
if (f.length == 1) { if (f.length == 1) {
F[0] = new Complex(f[0], 0.0); transformed[0] = new Complex(f[0], 0.0);
return F; return transformed;
} }
// Rather than the naive real to complex conversion, pack 2N // Rather than the naive real to complex conversion, pack 2N
// real numbers into N complex numbers for better performance. // real numbers into N complex numbers for better performance.
int N = f.length >> 1; int n = f.length >> 1;
Complex c[] = new Complex[N]; Complex[] repacked = new Complex[n];
for (int i = 0; i < N; i++) { for (int i = 0; i < n; i++) {
c[i] = new Complex(f[2*i], f[2*i+1]); repacked[i] = new Complex(f[2 * i], f[2 * i + 1]);
} }
roots.computeOmega(isInverse ? -N : N); roots.computeOmega(isInverse ? -n : n);
Complex z[] = fft(c); Complex[] z = fft(repacked);
// reconstruct the FFT result for the original array // reconstruct the FFT result for the original array
roots.computeOmega(isInverse ? -2*N : 2*N); roots.computeOmega(isInverse ? -2 * n : 2 * n);
F[0] = new Complex(2 * (z[0].getReal() + z[0].getImaginary()), 0.0); transformed[0] = new Complex(2 * (z[0].getReal() + z[0].getImaginary()), 0.0);
F[N] = new Complex(2 * (z[0].getReal() - z[0].getImaginary()), 0.0); transformed[n] = new Complex(2 * (z[0].getReal() - z[0].getImaginary()), 0.0);
for (int i = 1; i < N; i++) { for (int i = 1; i < n; i++) {
Complex A = z[N-i].conjugate(); Complex a = z[n - i].conjugate();
Complex B = z[i].add(A); Complex b = z[i].add(a);
Complex C = z[i].subtract(A); Complex c = z[i].subtract(a);
//Complex D = roots.getOmega(i).multiply(Complex.I); //Complex D = roots.getOmega(i).multiply(Complex.I);
Complex D = new Complex(-roots.getOmegaImaginary(i), Complex d = new Complex(-roots.getOmegaImaginary(i),
roots.getOmegaReal(i)); roots.getOmegaReal(i));
F[i] = B.subtract(C.multiply(D)); transformed[i] = b.subtract(c.multiply(d));
F[2*N-i] = F[i].conjugate(); transformed[2 * n - i] = transformed[i].conjugate();
} }
return scaleArray(F, 0.5); return scaleArray(transformed, 0.5);
} }
/** /**
@ -334,11 +332,11 @@ public class FastFourierTransformer implements Serializable {
* @return the complex transformed array * @return the complex transformed array
* @throws IllegalArgumentException if any parameters are invalid * @throws IllegalArgumentException if any parameters are invalid
*/ */
protected Complex[] fft(Complex data[]) protected Complex[] fft(Complex[] data)
throws IllegalArgumentException { throws IllegalArgumentException {
final int n = data.length; final int n = data.length;
final Complex f[] = new Complex[n]; final Complex[] f = new Complex[n];
// initial simple cases // initial simple cases
verifyDataSet(data); verifyDataSet(data);
@ -365,37 +363,37 @@ public class FastFourierTransformer implements Serializable {
// the bottom base-4 round // the bottom base-4 round
for (int i = 0; i < n; i += 4) { for (int i = 0; i < n; i += 4) {
final Complex a = f[i].add(f[i+1]); final Complex a = f[i].add(f[i + 1]);
final Complex b = f[i+2].add(f[i+3]); final Complex b = f[i + 2].add(f[i + 3]);
final Complex c = f[i].subtract(f[i+1]); final Complex c = f[i].subtract(f[i + 1]);
final Complex d = f[i+2].subtract(f[i+3]); final Complex d = f[i + 2].subtract(f[i + 3]);
final Complex e1 = c.add(d.multiply(Complex.I)); final Complex e1 = c.add(d.multiply(Complex.I));
final Complex e2 = c.subtract(d.multiply(Complex.I)); final Complex e2 = c.subtract(d.multiply(Complex.I));
f[i] = a.add(b); f[i] = a.add(b);
f[i+2] = a.subtract(b); f[i + 2] = a.subtract(b);
// omegaCount indicates forward or inverse transform // omegaCount indicates forward or inverse transform
f[i+1] = roots.isForward() ? e2 : e1; f[i + 1] = roots.isForward() ? e2 : e1;
f[i+3] = roots.isForward() ? e1 : e2; f[i + 3] = roots.isForward() ? e1 : e2;
} }
// iterations from bottom to top take O(N*logN) time // iterations from bottom to top take O(N*logN) time
for (int i = 4; i < n; i <<= 1) { for (int i = 4; i < n; i <<= 1) {
final int m = n / (i<<1); final int m = n / (i << 1);
for (int j = 0; j < n; j += i<<1) { for (int j = 0; j < n; j += i << 1) {
for (int k = 0; k < i; k++) { for (int k = 0; k < i; k++) {
//z = f[i+j+k].multiply(roots.getOmega(k*m)); //z = f[i+j+k].multiply(roots.getOmega(k*m));
final int k_times_m = k*m; final int km = k * m;
final double omega_k_times_m_real = roots.getOmegaReal(k_times_m); final double omegaKmReal = roots.getOmegaReal(km);
final double omega_k_times_m_imaginary = roots.getOmegaImaginary(k_times_m); final double omegaKmImag = roots.getOmegaImaginary(km);
//z = f[i+j+k].multiply(omega[k*m]); //z = f[i+j+k].multiply(omega[k*m]);
final Complex z = new Complex( final Complex z = new Complex(
f[i+j+k].getReal() * omega_k_times_m_real - f[i + j + k].getReal() * omegaKmReal
f[i+j+k].getImaginary() * omega_k_times_m_imaginary, - f[i + j + k].getImaginary() * omegaKmImag,
f[i+j+k].getReal() * omega_k_times_m_imaginary + f[i + j + k].getReal() * omegaKmImag
f[i+j+k].getImaginary() * omega_k_times_m_real); + f[i + j + k].getImaginary() * omegaKmReal);
f[i+j+k] = f[j+k].subtract(z); f[i + j + k] = f[j + k].subtract(z);
f[j+k] = f[j+k].add(z); f[j + k] = f[j + k].add(z);
} }
} }
} }
@ -427,7 +425,7 @@ public class FastFourierTransformer implements Serializable {
} }
verifyInterval(min, max); verifyInterval(min, max);
double s[] = new double[n]; double[] s = new double[n];
double h = (max - min) / n; double h = (max - min) / n;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
s[i] = f.value(min + i * h); s[i] = f.value(min + i * h);
@ -443,7 +441,7 @@ public class FastFourierTransformer implements Serializable {
* @param d the real scaling coefficient * @param d the real scaling coefficient
* @return a reference to the scaled array * @return a reference to the scaled array
*/ */
public static double[] scaleArray(double f[], double d) { public static double[] scaleArray(double[] f, double d) {
for (int i = 0; i < f.length; i++) { for (int i = 0; i < f.length; i++) {
f[i] *= d; f[i] *= d;
} }
@ -458,7 +456,7 @@ public class FastFourierTransformer implements Serializable {
* @param d the real scaling coefficient * @param d the real scaling coefficient
* @return a reference to the scaled array * @return a reference to the scaled array
*/ */
public static Complex[] scaleArray(Complex f[], double d) { public static Complex[] scaleArray(Complex[] f, double d) {
for (int i = 0; i < f.length; i++) { for (int i = 0; i < f.length; i++) {
f[i] = new Complex(d * f[i].getReal(), d * f[i].getImaginary()); f[i] = new Complex(d * f[i].getReal(), d * f[i].getImaginary());
} }
@ -481,7 +479,7 @@ public class FastFourierTransformer implements Serializable {
* @param d the data array * @param d the data array
* @throws IllegalArgumentException if array length is not power of 2 * @throws IllegalArgumentException if array length is not power of 2
*/ */
public static void verifyDataSet(double d[]) throws IllegalArgumentException { public static void verifyDataSet(double[] d) throws IllegalArgumentException {
if (!isPowerOf2(d.length)) { if (!isPowerOf2(d.length)) {
throw MathRuntimeException.createIllegalArgumentException( throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, d.length); LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, d.length);
@ -494,7 +492,7 @@ public class FastFourierTransformer implements Serializable {
* @param o the data array * @param o the data array
* @throws IllegalArgumentException if array length is not power of 2 * @throws IllegalArgumentException if array length is not power of 2
*/ */
public static void verifyDataSet(Object o[]) throws IllegalArgumentException { public static void verifyDataSet(Object[] o) throws IllegalArgumentException {
if (!isPowerOf2(o.length)) { if (!isPowerOf2(o.length)) {
throw MathRuntimeException.createIllegalArgumentException( throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, o.length); LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, o.length);
@ -687,7 +685,7 @@ public class FastFourierTransformer implements Serializable {
} }
if (vector.length != dimensionSize.length) { if (vector.length != dimensionSize.length) {
throw MathRuntimeException.createIllegalArgumentException( throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, vector.length,dimensionSize.length); LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, vector.length, dimensionSize.length);
} }
Object[] lastDimension = (Object[]) multiDimensionalComplexArray; Object[] lastDimension = (Object[]) multiDimensionalComplexArray;
@ -710,7 +708,7 @@ public class FastFourierTransformer implements Serializable {
} }
/** /**
* Get the underlying storage array * Get the underlying storage array.
* @return underlying storage array * @return underlying storage array
*/ */
public Object getArray() { public Object getArray() {
@ -738,7 +736,7 @@ public class FastFourierTransformer implements Serializable {
size *= dimensionSize[i]; size *= dimensionSize[i];
} }
int[][] vectorList = new int[size][dimensionSize.length]; int[][] vectorList = new int[size][dimensionSize.length];
for (int[] nextVector: vectorList) { for (int[] nextVector : vectorList) {
System.arraycopy(vector, 0, nextVector, 0, System.arraycopy(vector, 0, nextVector, 0,
dimensionSize.length); dimensionSize.length);
for (int i = 0; i < dimensionSize.length; i++) { for (int i = 0; i < dimensionSize.length; i++) {
@ -751,7 +749,7 @@ public class FastFourierTransformer implements Serializable {
} }
} }
for (int[] nextVector: vectorList) { for (int[] nextVector : vectorList) {
mdcm.set(get(nextVector), nextVector); mdcm.set(get(nextVector), nextVector);
} }
} }
@ -782,7 +780,7 @@ public class FastFourierTransformer implements Serializable {
private boolean isForward; private boolean isForward;
/** /**
* Build an engine for computing then <sup>th</sup> roots of unity * Build an engine for computing then <sup>th</sup> roots of unity.
*/ */
public RootsOfUnity() { public RootsOfUnity() {
@ -844,10 +842,10 @@ public class FastFourierTransformer implements Serializable {
omegaImaginaryForward[0] = 0.0; omegaImaginaryForward[0] = 0.0;
omegaImaginaryInverse[0] = 0.0; omegaImaginaryInverse[0] = 0.0;
for (int i = 1; i < absN; i++) { for (int i = 1; i < absN; i++) {
omegaReal[i] = omegaReal[i] = omegaReal[i - 1] * cosT
omegaReal[i-1] * cosT + omegaImaginaryForward[i-1] * sinT; + omegaImaginaryForward[i - 1] * sinT;
omegaImaginaryForward[i] = omegaImaginaryForward[i] = omegaImaginaryForward[i - 1] * cosT
omegaImaginaryForward[i-1] * cosT - omegaReal[i-1] * sinT; - omegaReal[i - 1] * sinT;
omegaImaginaryInverse[i] = -omegaImaginaryForward[i]; omegaImaginaryInverse[i] = -omegaImaginaryForward[i];
} }
omegaCount = absN; omegaCount = absN;
@ -855,7 +853,7 @@ public class FastFourierTransformer implements Serializable {
} }
/** /**
* Get the real part of the k<sup>th</sup> n<sup>th</sup> root of unity * Get the real part of the k<sup>th</sup> n<sup>th</sup> root of unity.
* @param k index of the n<sup>th</sup> root of unity * @param k index of the n<sup>th</sup> root of unity
* @return real part of the k<sup>th</sup> n<sup>th</sup> root of unity * @return real part of the k<sup>th</sup> n<sup>th</sup> root of unity
* @throws IllegalStateException if no roots of unity have been computed yet * @throws IllegalStateException if no roots of unity have been computed yet
@ -877,7 +875,7 @@ public class FastFourierTransformer implements Serializable {
} }
/** /**
* Get the imaginary part of the k<sup>th</sup> n<sup>th</sup> root of unity * Get the imaginary part of the k<sup>th</sup> n<sup>th</sup> root of unity.
* @param k index of the n<sup>th</sup> root of unity * @param k index of the n<sup>th</sup> root of unity
* @return imaginary part of the k<sup>th</sup> n<sup>th</sup> root of unity * @return imaginary part of the k<sup>th</sup> n<sup>th</sup> root of unity
* @throws IllegalStateException if no roots of unity have been computed yet * @throws IllegalStateException if no roots of unity have been computed yet