Example usage for org.apache.commons.math3.exception.util LocalizedFormats FIRST_ELEMENT_NOT_ZERO

List of usage examples for org.apache.commons.math3.exception.util LocalizedFormats FIRST_ELEMENT_NOT_ZERO

Introduction

In this page you can find the example usage for org.apache.commons.math3.exception.util LocalizedFormats FIRST_ELEMENT_NOT_ZERO.

Prototype

LocalizedFormats FIRST_ELEMENT_NOT_ZERO

To view the source code for org.apache.commons.math3.exception.util LocalizedFormats FIRST_ELEMENT_NOT_ZERO.

Click Source Link

Usage

From source file:experiment.FastSineTransformer_bug.java

/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array/*from ww  w. j  a  v  a 2s .com*/
 * @throws MathIllegalArgumentException if the length of the data array is
 *   not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.FIRST_ELEMENT_NOT_ZERO, Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) { // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;

    x[n >> 1] = 2.0 * f[n >> 1];
    double[] data1 = { x[n >> 1], f[n >> 1] };
    log.add(1, data1, "x[n>>1] f[n>>1]");//x[n>>1] f[n>>1] exp1
    double[] data2 = { f[n >> 1] };
    log.add(2, data2, "f[n>>1]");//f[n>>1] exp2
    double tempa = 0;
    double tempb = 0;
    int tempi = 0;
    /*Bug2 change FastMath.PI/n to FastMath.PI/(n-1)+1E-10 store in Data1
     * Bug 3 change 0.5 to 0.499 for b store in Data2
     */
    /*Bug1 add a small value*/
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.499 * (f[i] - f[n - i]);
        x[i] = a + b;
        x[n - i] = a - b;
        tempa = a;
        tempb = b;
        tempi = i;
    }

    double[] data3 = { tempa, FastMath.sin(tempi * FastMath.PI / n), f[tempi] + f[n - tempi] };
    log.add(3, data3, "a");// a FastMath.sin(i*FastMath.PI/n)  (f[i]+f[n-i])
    double[] data4 = { FastMath.sin(tempi * FastMath.PI / n), (double) tempi };// FastMath.sin(i * FastMath.PI / n) i
    log.add(4, data4, "FastMath.sin(temp * FastMath.PI / n)");
    double[] data5 = { f[tempi] + f[n - tempi], f[tempi], f[n - tempi] };
    log.add(5, data5, "f[temp] + f[n - temp]");// (f[i] + f[n - i]) f[i] f[n-i]
    double[] data6 = { f[tempi] };
    log.add(6, data6, "f[temp]");//f[i]
    double[] data7 = { f[n - tempi] };
    log.add(7, data7, "f[n-temp]");//f[n-i]
    double[] data8 = { x[tempi], tempa, tempb };
    log.add(8, data8, "x[i]");//x[i] a b
    double[] data9 = { x[n - tempi], tempa, tempb };
    log.add(9, data9, "x[n-i]");//x[n-i] a b
    double[] data10 = { tempa };
    log.add(10, data10, "a");//a
    double[] data11 = { tempb, f[tempi], f[n - tempi] };
    log.add(11, data11, "b");//b
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    /*Bug 1 add a small number 0.03 to transformed[1]  */
    transformed[1] = 0.5 * y[0].getReal();
    double[] data12 = { transformed[1], y[0].getReal() };
    log.add(12, data12, "transformed[1]");//transformed[1] y[0].getReal()
    double[] data13 = { y[0].getReal() };
    log.add(13, data13, "y[0].getReal()");//y[0].getReal()
    for (int i = 1; i < (n >> 1); i++) {
        /*Bug 4 add abs to y[i].getImaginary  */
        transformed[2 * i] = -y[i].getImaginary();
        /*Bug 5 change 2*i-1 to 2*i           */
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
        tempi = i;
    }
    double[] data14 = { transformed[2 * tempi] };
    log.add(14, data14, "transformed[2*i]");// transformed[2*i]  -y[i].getImaginary();
    double[] data15 = { transformed[2 * tempi + 1], y[tempi].getReal(), transformed[2 * tempi - 1] };// transformed[2*i+1]  y[i].getReal()  transformed[2 * i - 1]
    log.add(15, data15, "transformed[2*ti+1]");
    double[] data16 = { y[tempi].getReal() };
    log.add(16, data16, "y[i].getReal()");// y[i].getReal()
    double[] data17 = { transformed[2 * tempi - 1] };
    log.add(17, data17, "transformed[2 * i - 1]");// transformed[2 * i - 1]
    log.logFile();
    log.clear();
    return transformed;
}