Search in sources :

Example 1 with DoubleDoubleFunction

use of cern.colt.function.DoubleDoubleFunction in project tdq-studio-se by Talend.

the class TestMatrix2D method doubleTest13.

/**
 */
public static void doubleTest13() {
    double[] values = { 0, 1, 2, 3 };
    DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);
    System.out.println(matrix);
    // Sum( x[i]*x[i] )
    System.out.println(matrix.viewSelection(new cern.colt.function.DoubleProcedure() {

        public final boolean apply(double a) {
            return a % 2 == 0;
        }
    }));
    // --> 14
    // Sum( x[i]*x[i] )
    System.out.println(matrix.aggregate(F.plus, F.square));
    // --> 14
    // Sum( x[i]*x[i]*x[i] )
    System.out.println(matrix.aggregate(F.plus, F.pow(3)));
    // --> 36
    // Sum( x[i] )
    System.out.println(matrix.aggregate(F.plus, F.identity));
    // --> 6
    // Min( x[i] )
    System.out.println(matrix.aggregate(F.min, F.identity));
    // --> 0
    // Max( Sqrt(x[i]) / 2 )
    System.out.println(matrix.aggregate(F.max, F.chain(F.div(2), F.sqrt)));
    // --> 0.8660254037844386
    // Number of all cells with 0 <= value <= 2
    System.out.println(matrix.aggregate(F.plus, F.between(0, 2)));
    // --> 3
    // Number of all cells with 0.8 <= Log2(value) <= 1.2
    System.out.println(matrix.aggregate(F.plus, F.chain(F.between(0.8, 1.2), F.log2)));
    // --> 1
    // Product( x[i] )
    System.out.println(matrix.aggregate(F.mult, F.identity));
    // --> 0
    // Product( x[i] ) of all x[i] > limit
    final double limit = 1;
    DoubleFunction f = new DoubleFunction() {

        public final double apply(double a) {
            return a > limit ? a : 1;
        }
    };
    System.out.println(matrix.aggregate(F.mult, f));
    // --> 6
    // Sum( (x[i]+y[i])^2 )
    DoubleMatrix1D otherMatrix1D = matrix.copy();
    System.out.println(matrix.aggregate(otherMatrix1D, F.plus, F.chain(F.square, F.plus)));
    // --> 56
    matrix.assign(F.plus(1));
    otherMatrix1D = matrix.copy();
    // otherMatrix1D.zMult(3);
    System.out.println(matrix);
    System.out.println(otherMatrix1D);
    // Sum(Math.PI * Math.log(otherMatrix1D[i] / matrix[i]))
    System.out.println(matrix.aggregate(otherMatrix1D, F.plus, F.chain(F.mult(Math.PI), F.chain(F.log, F.swapArgs(F.div)))));
    // or, perhaps less error prone and more readable:
    System.out.println(matrix.aggregate(otherMatrix1D, F.plus, new DoubleDoubleFunction() {

        public double apply(double a, double b) {
            return Math.PI * Math.log(b / a);
        }
    }));
    DoubleMatrix3D x = cern.colt.matrix.DoubleFactory3D.dense.ascending(2, 2, 2);
    System.out.println(x);
    // Sum( x[slice,row,col]*x[slice,row,col] )
    System.out.println(x.aggregate(F.plus, F.square));
    // --> 140
    DoubleMatrix3D y = x.copy();
    // Sum( (x[i]+y[i])^2 )
    System.out.println(x.aggregate(y, F.plus, F.chain(F.square, F.plus)));
    // --> 560
    System.out.println(matrix.assign(F.random()));
    System.out.println(matrix.assign(new cern.jet.random.Poisson(5, cern.jet.random.Poisson.makeDefaultGenerator())));
}
Also used : DoubleMatrix3D(cern.colt.matrix.DoubleMatrix3D) DoubleFunction(cern.colt.function.DoubleFunction) DoubleDoubleFunction(cern.colt.function.DoubleDoubleFunction) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DoubleDoubleFunction(cern.colt.function.DoubleDoubleFunction)

Example 2 with DoubleDoubleFunction

use of cern.colt.function.DoubleDoubleFunction in project tdq-studio-se by Talend.

the class Functions method demo1.

/**
 * Demonstrates usage of this class.
 */
public static void demo1() {
    cern.jet.math.Functions F = cern.jet.math.Functions.functions;
    double a = 0.5;
    double b = 0.2;
    double v = Math.sin(a) + Math.pow(Math.cos(b), 2);
    System.out.println(v);
    DoubleDoubleFunction f = F.chain(F.plus, F.sin, F.chain(F.square, F.cos));
    // DoubleDoubleFunction f = F.chain(plus,sin,F.chain(square,cos));
    System.out.println(f.apply(a, b));
    DoubleDoubleFunction g = new DoubleDoubleFunction() {

        public final double apply(double x, double y) {
            return Math.sin(x) + Math.pow(Math.cos(y), 2);
        }
    };
    System.out.println(g.apply(a, b));
    DoubleFunction m = F.plus(3);
    DoubleFunction n = F.plus(4);
    System.out.println(m.apply(0));
    System.out.println(n.apply(0));
}
Also used : DoubleDoubleFunction(cern.colt.function.DoubleDoubleFunction) DoubleFunction(cern.colt.function.DoubleFunction) DoubleDoubleFunction(cern.colt.function.DoubleDoubleFunction)

Example 3 with DoubleDoubleFunction

use of cern.colt.function.DoubleDoubleFunction in project tdq-studio-se by Talend.

the class Functions method demo2.

/**
 * Benchmarks and demonstrates usage of trivial and complex functions.
 */
public static void demo2(int size) {
    cern.jet.math.Functions F = cern.jet.math.Functions.functions;
    System.out.println("\n\n");
    double a = 0.0;
    double b = 0.0;
    double v = Math.abs(Math.sin(a) + Math.pow(Math.cos(b), 2));
    // double v = Math.sin(a) + Math.pow(Math.cos(b),2);
    // double v = a + b;
    System.out.println(v);
    // DoubleDoubleFunction f = F.chain(F.plus,F.identity,F.identity);
    DoubleDoubleFunction f = F.chain(F.abs, F.chain(F.plus, F.sin, F.chain(F.square, F.cos)));
    // DoubleDoubleFunction f = F.chain(F.plus,F.sin,F.chain(F.square,F.cos));
    // DoubleDoubleFunction f = F.plus;
    System.out.println(f.apply(a, b));
    DoubleDoubleFunction g = new DoubleDoubleFunction() {

        public final double apply(double x, double y) {
            return Math.abs(Math.sin(x) + Math.pow(Math.cos(y), 2));
        }
    };
    System.out.println(g.apply(a, b));
    // emptyLoop
    cern.colt.Timer emptyLoop = new cern.colt.Timer().start();
    a = 0;
    b = 0;
    double sum = 0;
    for (int i = size; --i >= 0; ) {
        sum += a;
        a++;
        b++;
    }
    emptyLoop.stop().display();
    System.out.println("empty sum=" + sum);
    cern.colt.Timer timer = new cern.colt.Timer().start();
    a = 0;
    b = 0;
    sum = 0;
    for (int i = size; --i >= 0; ) {
        sum += Math.abs(Math.sin(a) + Math.pow(Math.cos(b), 2));
        // sum += a + b;
        a++;
        b++;
    }
    timer.stop().display();
    System.out.println("evals / sec = " + size / timer.minus(emptyLoop).seconds());
    System.out.println("sum=" + sum);
    timer.reset().start();
    a = 0;
    b = 0;
    sum = 0;
    for (int i = size; --i >= 0; ) {
        sum += f.apply(a, b);
        a++;
        b++;
    }
    timer.stop().display();
    System.out.println("evals / sec = " + size / timer.minus(emptyLoop).seconds());
    System.out.println("sum=" + sum);
    timer.reset().start();
    a = 0;
    b = 0;
    sum = 0;
    for (int i = size; --i >= 0; ) {
        sum += g.apply(a, b);
        a++;
        b++;
    }
    timer.stop().display();
    System.out.println("evals / sec = " + size / timer.minus(emptyLoop).seconds());
    System.out.println("sum=" + sum);
}
Also used : DoubleDoubleFunction(cern.colt.function.DoubleDoubleFunction)

Aggregations

DoubleDoubleFunction (cern.colt.function.DoubleDoubleFunction)3 DoubleFunction (cern.colt.function.DoubleFunction)2 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)1 DoubleMatrix3D (cern.colt.matrix.DoubleMatrix3D)1