use of cern.colt.function.DoubleFunction 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())));
}
use of cern.colt.function.DoubleFunction 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));
}
Aggregations