Example usage for org.apache.commons.math3.util ArithmeticUtils isPowerOfTwo

List of usage examples for org.apache.commons.math3.util ArithmeticUtils isPowerOfTwo

Introduction

In this page you can find the example usage for org.apache.commons.math3.util ArithmeticUtils isPowerOfTwo.

Prototype

public static boolean isPowerOfTwo(long n) 

Source Link

Document

Returns true if the argument is a power of two.

Usage

From source file:experiment.FastCosineTransformer_bug.java

/**
 * Perform the FCT algorithm (including inverse).
 *
 * @param f the real data array to be transformed
 * @return the real transformed array/*from  w w w. j a v a2  s  .  c o m*/
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two plus one
 */
protected double[] fct(double[] f) throws MathIllegalArgumentException {

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

    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
                Integer.valueOf(f.length));
    }
    if (n == 1) { // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    String funname = "cosh/";
    double tempexpression = 0;
    double[] data1 = { x[0], f[0], f[n] };
    String expressionname = "x[0]";
    log.add(1, data1, funname + expressionname);

    x[n >> 1] = f[n >> 1];

    double[] data2 = { x[n >> 1], f[n >> 1] };
    expressionname = "x[n>>1]";
    log.add(2, data2, funname + expressionname);
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    double[] data3 = { t1, f[0], f[n] };
    expressionname = "t1";
    log.add(3, data3, funname + expressionname);

    double[] data4 = { f[0] - f[n], f[0], f[n] };
    expressionname = "f[0]-f[n]";
    log.add(4, data4, funname + expressionname);

    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);

        double[] data5 = { a, f[i], f[n - i] };
        expressionname = "a";
        log.add(5, data5, funname + expressionname);

        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);

        double[] data6 = { b, FastMath.sin(i * FastMath.PI / n), (f[i] - f[n - i]) };
        expressionname = "b";
        log.add(6, data6, funname + expressionname);
        /*****bug2 store in Data2 FastMath.sin(i * FastMath.PI / n) to FastMath.sin(2*i * FastMath.PI / n)*******/
        double[] data7 = { FastMath.sin(i * FastMath.PI / n), i * FastMath.PI, n };
        expressionname = "FastMath.sin(i * FastMath.PI / n)";
        log.add(7, data7, funname + expressionname);

        double[] data8 = { (f[i] - f[n - i]), f[i], f[n - i] };
        expressionname = "f[i] - f[n - i]";
        log.add(8, data8, funname + expressionname);

        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);

        double[] data9 = { c, FastMath.cos(i * FastMath.PI / n), (f[i] - f[n - i]) };
        expressionname = "c";
        log.add(9, data9, funname + expressionname);

        double[] data10 = { FastMath.cos(i * FastMath.PI / n), i * FastMath.PI, n };
        expressionname = "FastMath.cos(i * FastMath.PI / n)";
        log.add(10, data10, funname + expressionname);

        double[] data11 = { (f[i] - f[n - i]), f[i], f[n - i] };
        expressionname = "c";
        log.add(11, data11, funname + expressionname);

        x[i] = a + b;

        double[] data12 = { x[i], a, b };
        expressionname = "x[i]";
        log.add(12, data12, funname + expressionname);

        x[n - i] = a - b;

        double[] data13 = { x[n - i], a, b };
        expressionname = "x[n-i]";
        log.add(13, data13, funname + expressionname);

        tempexpression = t1;

        t1 += c;

        double[] data14 = { t1, tempexpression, c };
        expressionname = "t1";
        log.add(14, data14, funname + expressionname);

    }

    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();

    double[] data15 = { transformed[0] };
    expressionname = "transformed[0]";
    log.add(15, data15, funname + expressionname);

    transformed[1] = t1;

    double[] data16 = { transformed[1] };
    expressionname = "transformed[1]";
    log.add(16, data16, funname + expressionname);

    for (int i = 1; i < (n >> 1); i++) {

        transformed[2 * i] = y[i].getReal();

        double[] data17 = { transformed[2 * i] };
        expressionname = "transformed[2 * i]";
        log.add(17, data17, funname + expressionname);
        /***bug 1, store in Data1, add Math.abs() on transformed[2 * i - 1] - y[i].getImaginary()***/

        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();

        double[] data18 = { transformed[2 * i + 1], transformed[2 * i - 1], y[i].getImaginary() };
        expressionname = "transformed[2 * i+1]";
        log.add(18, data18, funname + expressionname);

        double[] data20 = { transformed[2 * i - 1] - y[i].getImaginary(), transformed[2 * i - 1],
                y[i].getImaginary() };
        expressionname = "transformed[2 * i - 1] - y[i].getImaginary()";
        log.add(20, data20, funname + expressionname);
    }

    transformed[n] = y[n >> 1].getReal();

    double[] data19 = { transformed[n] };
    expressionname = "transformed[n]";
    log.add(19, data19, funname + expressionname);

    log.logFile();
    log.clear();
    return transformed;
}

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  w w  w.  j  a v a 2s.  c  o m
 * @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;
}

From source file:experiment.FastCosineTransformer_bug2.java

/**
 * Perform the FCT algorithm (including inverse).
 * /*from   www .  jav a 2  s. c  om*/
 * @param f
 *            the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException
 *             if the length of the data array is not a power of two plus
 *             one
 */
protected double[] fct(double[] f) throws MathIllegalArgumentException {
    final double[] transformed = new double[f.length];
    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
                Integer.valueOf(f.length));
    }
    if (n == 1) { // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }
    test test1 = new test();
    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    String funname = "cosh/";
    double tempexpression = 0;
    double ta = 3.24, tb = 2.31, tc = 7.86, td = 5.12;
    int te = 2;
    boolean tf = false;
    x[n >> 1] = f[n >> 1];
    ta = tb + tc + mid((int) ta + 1, (int) tb, (int) tc) + td;
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    ta = (te >> 2) + tc % tb + td;
    ta = tb + tc + td;
    ta = tb + tc - td;
    ta = tb + tc + td + te;
    ta = tb * tc * td;
    ta = tb * tc / td;
    ta = tb * tc * td * te;
    ta = ta * ta + tb * tb + tc * tc;
    ta = tc - (td + te);
    ta = tc + tb - (td + te + tc);
    ta = tc * tb / tc + test1.a + 3;
    ta = tc + tb / td - test1.f.a;
    ta = td + Math.cos(ta + tc - td - te * tb) + tb;
    ta = Math.min(tc, td + 1) + 1;
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);

        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);

        /*****
         * bug2 store in Data2 FastMath.sin(i * FastMath.PI / n) to
         * FastMath.sin(2*i * FastMath.PI / n)
         *******/

        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);

        x[i] = a + b;

        x[n - i] = a - b;

        tempexpression = t1;

        t1 = t1 + c;

    }

    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();

    transformed[1] = t1;

    for (int i = 1; i < (n >> 1); i++) {

        transformed[2 * i] = y[i].getReal();

        /***
         * bug 1, store in Data1, add Math.abs() on transformed[2 * i - 1] -
         * y[i].getImaginary()
         ***/

        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();

    }

    transformed[n] = y[n >> 1].getReal();

    return transformed;
}