cern.colt.function.DoubleDoubleFunction Java Examples

The following examples show how to use cern.colt.function.DoubleDoubleFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Functions.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #2
Source File: Functions.java    From jAudioGIT with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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));
}
 
Example #3
Source File: Functions.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Constructs a function that returns <tt>a + b*constant</tt>.
 * <tt>a</tt> and <tt>b</tt> are variables, <tt>constant</tt> is fixed.
 */
public static DoubleDoubleFunction plusMult(double constant) {
	return new PlusMult(constant); 
	/*
	return new DoubleDoubleFunction() {
		public final double apply(double a, double b) { return a + b*constant; }
	};
	*/
}
 
Example #4
Source File: Functions.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a function that returns <tt>a + b*constant</tt>.
 * <tt>a</tt> and <tt>b</tt> are variables, <tt>constant</tt> is fixed.
 */
public static DoubleDoubleFunction plusMult(double constant) {
	return new PlusMult(constant); 
	/*
	return new DoubleDoubleFunction() {
		public final double apply(double a, double b) { return a + b*constant; }
	};
	*/
}
 
Example #5
Source File: Functions.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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)); }
		//public final double apply(double x, double y) { return x+y; }
	};
	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);
		
}
 
Example #6
Source File: Functions.java    From jAudioGIT with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * 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)); }
		//public final double apply(double x, double y) { return x+y; }
	};
	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);
		
}
 
Example #7
Source File: ControlFlowGraph.java    From CFGScanDroid with GNU General Public License v2.0 4 votes vote down vote up
public void normalize() {
	// System.out.println(adjacencyMatrix);
	int adjacencyMatrixSize = adjacencyMatrix.columns();
	
	if(adjacencyMatrixSize < 1)
		return;

	DoubleMatrix2D selector = new SparseDoubleMatrix2D(1, adjacencyMatrixSize);
	selector.set(0, 0, 1.0);
	// System.out.println(selector);
	// get vertices reachable from V0
	int cardinality = selector.cardinality();
	int lastCardinality = 0;

	DoubleDoubleFunction merge = new DoubleDoubleFunction() {
		public double apply(double a, double b) { 
			return (a > 0 || b > 0) ? 1 : 0; 
		}
	};

	while(cardinality != lastCardinality) {
		lastCardinality = cardinality;
		// selector = Algebra.DEFAULT.mult(selector, adjacencyMatrix);
		selector.assign(Algebra.DEFAULT.mult(selector, adjacencyMatrix), merge);
		// System.out.println(selector);
		cardinality = selector.cardinality();
	}

	// System.out.println(selector);

	if(cardinality == adjacencyMatrixSize) {
		return;
	}

	// IntArrayList nonZeros = new IntArrayList();
	// IntArrayList unusedInt = new IntArrayList();
	// DoubleArrayList unusedDouble = new DoubleArrayList();
	// selector.getNonZeros(unusedInt, nonZeros, unusedDouble);
	// SparseDoubleMatrix2D reduced = new SparseDoubleMatrix2D(adjacencyMatrix.viewSelection(nonZeros.elements(), nonZeros.elements()).toArray());

	SparseDoubleMatrix2D reduced = new SparseDoubleMatrix2D(cardinality, cardinality);
	int iCurrentVertex = 0;
	for(int i=0; i<adjacencyMatrixSize; ++i) {
		if(selector.get(0, i) != 0.0) {
			int jCurrentVertex = 0;
			for(int j=0; j<adjacencyMatrixSize; ++j) {
				if(selector.get(0, j) != 0.0){
					reduced.set(iCurrentVertex, jCurrentVertex, adjacencyMatrix.get(i, j));
					++jCurrentVertex;
				}
			}
			++iCurrentVertex;
		}
	}
	// System.out.println(reduced);
	// System.out.println("=======");
	adjacencyMatrix = reduced;
	vertexCount = adjacencyMatrix.columns();
	edgeCount = adjacencyMatrix.cardinality();
}
 
Example #8
Source File: CFGSig.java    From CFGScanDroid with GNU General Public License v2.0 4 votes vote down vote up
public void normalize() {
	// System.out.println(adjacencyMatrix);
	int adjacencyMatrixSize = adjacencyMatrix.columns();

	if(adjacencyMatrixSize < 1)
		return;

	DoubleMatrix2D selector = new SparseDoubleMatrix2D(1, adjacencyMatrixSize);
	selector.set(0, 0, 1.0);
	// System.out.println(selector);
	// get vertices reachable from V0
	int cardinality = selector.cardinality();
	int lastCardinality = 0;

	DoubleDoubleFunction merge = new DoubleDoubleFunction() {
		public double apply(double a, double b) { 
			return (a > 0 || b > 0) ? 1 : 0; 
		}
	};

	while(cardinality != lastCardinality) {
		lastCardinality = cardinality;
		// selector = Algebra.DEFAULT.mult(selector, adjacencyMatrix);
		selector.assign(Algebra.DEFAULT.mult(selector, adjacencyMatrix), merge);
		// System.out.println(selector);
		cardinality = selector.cardinality();
	}

	// System.out.println(selector);

	if(cardinality == adjacencyMatrixSize) {
		return;
	}

	// IntArrayList nonZeros = new IntArrayList();
	// IntArrayList unusedInt = new IntArrayList();
	// DoubleArrayList unusedDouble = new DoubleArrayList();
	// selector.getNonZeros(unusedInt, nonZeros, unusedDouble);
	// SparseDoubleMatrix2D reduced = new SparseDoubleMatrix2D(adjacencyMatrix.viewSelection(nonZeros.elements(), nonZeros.elements()).toArray());

	SparseDoubleMatrix2D reduced = new SparseDoubleMatrix2D(cardinality, cardinality);
	int iCurrentVertex = 0;
	for(int i=0; i<adjacencyMatrixSize; ++i) {
		if(selector.get(0, i) != 0.0) {
			int jCurrentVertex = 0;
			for(int j=0; j<adjacencyMatrixSize; ++j) {
				if(selector.get(0, j) != 0.0){
					reduced.set(iCurrentVertex, jCurrentVertex, adjacencyMatrix.get(i, j));
					++jCurrentVertex;
				}
			}
			++iCurrentVertex;
		}
	}
	// System.out.println(reduced);
	// System.out.println("=======");
	adjacencyMatrix = reduced;
	vertexCount = adjacencyMatrix.columns();
	edgeCount = adjacencyMatrix.cardinality();
}
 
Example #9
Source File: Functions.java    From jAudioGIT with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs a function that returns <tt>function.apply(b,a)</tt>, i.e. applies the function with the first operand as second operand and the second operand as first operand.
 * 
 * @param function a function taking operands in the form <tt>function.apply(a,b)</tt>.
 * @return the binary function <tt>function(b,a)</tt>.
 */
public static DoubleDoubleFunction swapArgs(final DoubleDoubleFunction function) {
	return new DoubleDoubleFunction() {
		public final double apply(double a, double b) { return function.apply(b,a); }
	};
}
 
Example #10
Source File: Functions.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs a unary function from a binary function with the first operand (argument) fixed to the given constant <tt>c</tt>.
 * The second operand is variable (free).
 * 
 * @param function a binary function taking operands in the form <tt>function.apply(c,var)</tt>.
 * @return the unary function <tt>function(c,var)</tt>.
 */
public static DoubleFunction bindArg1(final DoubleDoubleFunction function, final double c) {
	return new DoubleFunction() {
		public final double apply(double var) { return function.apply(c,var); }
	};
}
 
Example #11
Source File: Functions.java    From jAudioGIT with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs a function that returns <tt>a - b*constant</tt>.
 * <tt>a</tt> and <tt>b</tt> are variables, <tt>constant</tt> is fixed.
 */
public static DoubleDoubleFunction minusMult(final double constant) {
	return plusMult(-constant);
}
 
Example #12
Source File: Functions.java    From jAudioGIT with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs the function <tt>g( h(a,b) )</tt>.
 * 
 * @param g a unary function.
 * @param h a binary function.
 * @return the unary function <tt>g( h(a,b) )</tt>.
 */
public static DoubleDoubleFunction chain(final DoubleFunction g, final DoubleDoubleFunction h) {
	return new DoubleDoubleFunction() {
		public final double apply(double a, double b) { return g.apply(h.apply(a,b)); }
	};
}
 
Example #13
Source File: Functions.java    From jAudioGIT with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs the function <tt>f( g(a), h(b) )</tt>.
 * 
 * @param f a binary function.
 * @param g a unary function.
 * @param h a unary function.
 * @return the binary function <tt>f( g(a), h(b) )</tt>.
 */
public static DoubleDoubleFunction chain(final DoubleDoubleFunction f, final DoubleFunction g, final DoubleFunction h) {
	return new DoubleDoubleFunction() {
		public final double apply(double a, double b) { return f.apply(g.apply(a), h.apply(b)); }
	};
}
 
Example #14
Source File: Functions.java    From jAudioGIT with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs a unary function from a binary function with the second operand (argument) fixed to the given constant <tt>c</tt>.
 * The first operand is variable (free).
 * 
 * @param function a binary function taking operands in the form <tt>function.apply(var,c)</tt>.
 * @return the unary function <tt>function(var,c)</tt>.
 */
public static DoubleFunction bindArg2(final DoubleDoubleFunction function, final double c) {
	return new DoubleFunction() {
		public final double apply(double var) { return function.apply(var,c); }
	};
}
 
Example #15
Source File: Functions.java    From jAudioGIT with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs a unary function from a binary function with the first operand (argument) fixed to the given constant <tt>c</tt>.
 * The second operand is variable (free).
 * 
 * @param function a binary function taking operands in the form <tt>function.apply(c,var)</tt>.
 * @return the unary function <tt>function(c,var)</tt>.
 */
public static DoubleFunction bindArg1(final DoubleDoubleFunction function, final double c) {
	return new DoubleFunction() {
		public final double apply(double var) { return function.apply(c,var); }
	};
}
 
Example #16
Source File: Functions.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs a function that returns <tt>function.apply(b,a)</tt>, i.e. applies the function with the first operand as second operand and the second operand as first operand.
 * 
 * @param function a function taking operands in the form <tt>function.apply(a,b)</tt>.
 * @return the binary function <tt>function(b,a)</tt>.
 */
public static DoubleDoubleFunction swapArgs(final DoubleDoubleFunction function) {
	return new DoubleDoubleFunction() {
		public final double apply(double a, double b) { return function.apply(b,a); }
	};
}
 
Example #17
Source File: Functions.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs a function that returns <tt>a - b*constant</tt>.
 * <tt>a</tt> and <tt>b</tt> are variables, <tt>constant</tt> is fixed.
 */
public static DoubleDoubleFunction minusMult(final double constant) {
	return plusMult(-constant);
}
 
Example #18
Source File: Functions.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs the function <tt>g( h(a,b) )</tt>.
 * 
 * @param g a unary function.
 * @param h a binary function.
 * @return the unary function <tt>g( h(a,b) )</tt>.
 */
public static DoubleDoubleFunction chain(final DoubleFunction g, final DoubleDoubleFunction h) {
	return new DoubleDoubleFunction() {
		public final double apply(double a, double b) { return g.apply(h.apply(a,b)); }
	};
}
 
Example #19
Source File: Functions.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs the function <tt>f( g(a), h(b) )</tt>.
 * 
 * @param f a binary function.
 * @param g a unary function.
 * @param h a unary function.
 * @return the binary function <tt>f( g(a), h(b) )</tt>.
 */
public static DoubleDoubleFunction chain(final DoubleDoubleFunction f, final DoubleFunction g, final DoubleFunction h) {
	return new DoubleDoubleFunction() {
		public final double apply(double a, double b) { return f.apply(g.apply(a), h.apply(b)); }
	};
}
 
Example #20
Source File: Functions.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs a unary function from a binary function with the second operand (argument) fixed to the given constant <tt>c</tt>.
 * The first operand is variable (free).
 * 
 * @param function a binary function taking operands in the form <tt>function.apply(var,c)</tt>.
 * @return the unary function <tt>function(var,c)</tt>.
 */
public static DoubleFunction bindArg2(final DoubleDoubleFunction function, final double c) {
	return new DoubleFunction() {
		public final double apply(double var) { return function.apply(var,c); }
	};
}