Java Code Examples for cern.colt.matrix.DoubleMatrix2D#toStringShort()

The following examples show how to use cern.colt.matrix.DoubleMatrix2D#toStringShort() . 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: SeqBlas.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void dsymv(boolean isUpperTriangular, double alpha, DoubleMatrix2D A, DoubleMatrix1D x, double beta, DoubleMatrix1D y) {
	if (isUpperTriangular) A = A.viewDice();
	Property.DEFAULT.checkSquare(A);
	int size = A.rows();
	if (size != x.size() || size!=y.size()) {
		throw new IllegalArgumentException(A.toStringShort() + ", " + x.toStringShort() + ", " + y.toStringShort());
	}
	DoubleMatrix1D tmp = x.like();
	for (int i = 0; i < size; i++) {
		double sum = 0;
		for (int j = 0; j <= i; j++) {
			sum += A.getQuick(i,j) * x.getQuick(j);
		}
		for (int j = i + 1; j < size; j++) {
			sum += A.getQuick(j,i) * x.getQuick(j);
		}
		tmp.setQuick(i, alpha * sum + beta * y.getQuick(i));
	}
	y.assign(tmp);
}
 
Example 2
Source File: SeqBlas.java    From jAudioGIT with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void dsymv(boolean isUpperTriangular, double alpha, DoubleMatrix2D A, DoubleMatrix1D x, double beta, DoubleMatrix1D y) {
	if (isUpperTriangular) A = A.viewDice();
	Property.DEFAULT.checkSquare(A);
	int size = A.rows();
	if (size != x.size() || size!=y.size()) {
		throw new IllegalArgumentException(A.toStringShort() + ", " + x.toStringShort() + ", " + y.toStringShort());
	}
	DoubleMatrix1D tmp = x.like();
	for (int i = 0; i < size; i++) {
		double sum = 0;
		for (int j = 0; j <= i; j++) {
			sum += A.getQuick(i,j) * x.getQuick(j);
		}
		for (int j = i + 1; j < size; j++) {
			sum += A.getQuick(j,i) * x.getQuick(j);
		}
		tmp.setQuick(i, alpha * sum + beta * y.getQuick(i));
	}
	y.assign(tmp);
}
 
Example 3
Source File: SeqBlas.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public void dtrmv(boolean isUpperTriangular, boolean transposeA, boolean isUnitTriangular, DoubleMatrix2D A, DoubleMatrix1D x) {
	if (transposeA) {
		A = A.viewDice();
		isUpperTriangular = !isUpperTriangular;
	}
	
	Property.DEFAULT.checkSquare(A);
	int size = A.rows();
	if (size != x.size()) {
		throw new IllegalArgumentException(A.toStringShort() + ", " + x.toStringShort());
	}
	    
	DoubleMatrix1D b = x.like();
	DoubleMatrix1D y = x.like();
	if (isUnitTriangular) {
		y.assign(1);
	}
	else {
		for (int i = 0; i < size; i++) {
			y.setQuick(i, A.getQuick(i,i));
		}
	}
	
	for (int i = 0; i < size; i++) {
		double sum = 0;
		if (!isUpperTriangular) {
			for (int j = 0; j < i; j++) {
				sum += A.getQuick(i,j) * x.getQuick(j);
			}
			sum += y.getQuick(i) * x.getQuick(i);
		}
		else {
			sum += y.getQuick(i) * x.getQuick(i);
			for (int j = i + 1; j < size; j++) {
				sum += A.getQuick(i,j) * x.getQuick(j);			}
		}
		b.setQuick(i,sum);
	}
	x.assign(b);
}
 
Example 4
Source File: SeqBlas.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void dtrmv(boolean isUpperTriangular, boolean transposeA, boolean isUnitTriangular, DoubleMatrix2D A, DoubleMatrix1D x) {
	if (transposeA) {
		A = A.viewDice();
		isUpperTriangular = !isUpperTriangular;
	}
	
	Property.DEFAULT.checkSquare(A);
	int size = A.rows();
	if (size != x.size()) {
		throw new IllegalArgumentException(A.toStringShort() + ", " + x.toStringShort());
	}
	    
	DoubleMatrix1D b = x.like();
	DoubleMatrix1D y = x.like();
	if (isUnitTriangular) {
		y.assign(1);
	}
	else {
		for (int i = 0; i < size; i++) {
			y.setQuick(i, A.getQuick(i,i));
		}
	}
	
	for (int i = 0; i < size; i++) {
		double sum = 0;
		if (!isUpperTriangular) {
			for (int j = 0; j < i; j++) {
				sum += A.getQuick(i,j) * x.getQuick(j);
			}
			sum += y.getQuick(i) * x.getQuick(i);
		}
		else {
			sum += y.getQuick(i) * x.getQuick(i);
			for (int j = i + 1; j < size; j++) {
				sum += A.getQuick(i,j) * x.getQuick(j);			}
		}
		b.setQuick(i,sum);
	}
	x.assign(b);
}
 
Example 5
Source File: RCDoubleMatrix2D.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public DoubleMatrix2D zMult(DoubleMatrix2D B, DoubleMatrix2D C, final double alpha, double beta, boolean transposeA, boolean transposeB) {
	if (transposeB) B = B.viewDice();
	int m = rows;
	int n = columns;
	if (transposeA) {
		m = columns;
		n = rows;
	}
	int p = B.columns;
	boolean ignore = (C==null);
	if (C==null) C = new DenseDoubleMatrix2D(m,p);

	if (B.rows != n)
		throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+toStringShort()+", "+ (transposeB ? B.viewDice() : B).toStringShort());
	if (C.rows != m || C.columns != p)
		throw new IllegalArgumentException("Incompatibel result matrix: "+toStringShort()+", "+ (transposeB ? B.viewDice() : B).toStringShort()+", "+C.toStringShort());
	if (this == C || B == C)
		throw new IllegalArgumentException("Matrices must not be identical");
	
	if (!ignore) C.assign(cern.jet.math.Functions.mult(beta));

	// cache views	
	final DoubleMatrix1D[] Brows = new DoubleMatrix1D[n];
	for (int i=n; --i>=0; ) Brows[i] = B.viewRow(i);
	final DoubleMatrix1D[] Crows = new DoubleMatrix1D[m];
	for (int i=m; --i>=0; ) Crows[i] = C.viewRow(i);

	final cern.jet.math.PlusMult fun = cern.jet.math.PlusMult.plusMult(0);

	int[] idx = indexes.elements();
	double[] vals = values.elements();
	for (int i=starts.length-1; --i >= 0; ) {
		int low = starts[i];
		for (int k=starts[i+1]; --k >= low; ) {
			int j = idx[k];
			fun.multiplicator = vals[k]*alpha;
			if (!transposeA)
				Crows[i].assign(Brows[j],fun);
			else
				Crows[j].assign(Brows[i],fun);
		}
	}

	return C;
}
 
Example 6
Source File: SmpBlas.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void dgemm(final boolean transposeA, final boolean transposeB, final double alpha, final DoubleMatrix2D A, final DoubleMatrix2D B, final double beta, final DoubleMatrix2D C) {
	/*
	determine how to split and parallelize best into blocks
	if more B.columns than tasks --> split B.columns, as follows:
	
			xx|xx|xxx B
			xx|xx|xxx
			xx|xx|xxx
	A
	xxx     xx|xx|xxx C 
	xxx		xx|xx|xxx
	xxx		xx|xx|xxx
	xxx		xx|xx|xxx
	xxx		xx|xx|xxx

	if less B.columns than tasks --> split A.rows, as follows:
	
			xxxxxxx B
			xxxxxxx
			xxxxxxx
	A
	xxx     xxxxxxx C
	xxx     xxxxxxx
	---     -------
	xxx     xxxxxxx
	xxx     xxxxxxx
	---     -------
	xxx     xxxxxxx

	*/
	if (transposeA) {
		dgemm(false, transposeB, alpha, A.viewDice(), B, beta, C);
		return;
	}
	if (transposeB) {
		dgemm(transposeA, false, alpha, A, B.viewDice(), beta, C);
		return;
	}
	int m = A.rows();
	int n = A.columns();
	int p = B.columns();

	if (B.rows() != n)
		throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+A.toStringShort()+", "+B.toStringShort());
	if (C.rows() != m || C.columns() != p)
		throw new IllegalArgumentException("Incompatibel result matrix: "+A.toStringShort()+", "+B.toStringShort()+", "+C.toStringShort());
	if (A == C || B == C)
		throw new IllegalArgumentException("Matrices must not be identical");

	long flops = 2L*m*n*p;
	int noOfTasks = (int) Math.min(flops / 30000, this.maxThreads); // each thread should process at least 30000 flops
	boolean splitB = (p >= noOfTasks);
	int width = splitB ? p : m;
	noOfTasks = Math.min(width,noOfTasks);
	
	if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
		seqBlas.dgemm(transposeA, transposeB, alpha, A, B, beta, C);
		return;
	}
	
	// set up concurrent tasks
	int span = width/noOfTasks;
	final RecursiveAction[] subTasks = new RecursiveAction[noOfTasks];
	for (int i=0; i<noOfTasks; i++) {
		final int offset = i*span;
		if (i==noOfTasks-1) span = width - span*i; // last span may be a bit larger

		final DoubleMatrix2D AA,BB,CC; 
		if (splitB) { 
			// split B along columns into blocks
			AA = A;
			BB = B.viewPart(0,offset,n,span);
			CC = C.viewPart(0,offset,m,span);
		}
		else { 
			// split A along rows into blocks
			AA = A.viewPart(offset,0,span,n);
			BB = B;
			CC = C.viewPart(offset,0,span,p);
		}
				
		subTasks[i] = new RecursiveAction() { 
			@Override
			protected void compute() { 
				seqBlas.dgemm(transposeA,transposeB,alpha,AA,BB,beta,CC); 
				//System.out.println("Hello "+offset); 
			}
		};
	}
	
	// run tasks and wait for completion
		this.smp.taskGroup.invoke(
			new RecursiveAction() {
 				@Override
				protected void compute() {	
					invokeAll(subTasks);	
				}
			}
		);
}
 
Example 7
Source File: RCDoubleMatrix2D.java    From jAudioGIT with GNU Lesser General Public License v2.1 4 votes vote down vote up
public DoubleMatrix2D zMult(DoubleMatrix2D B, DoubleMatrix2D C, final double alpha, double beta, boolean transposeA, boolean transposeB) {
	if (transposeB) B = B.viewDice();
	int m = rows;
	int n = columns;
	if (transposeA) {
		m = columns;
		n = rows;
	}
	int p = B.columns;
	boolean ignore = (C==null);
	if (C==null) C = new DenseDoubleMatrix2D(m,p);

	if (B.rows != n)
		throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+toStringShort()+", "+ (transposeB ? B.viewDice() : B).toStringShort());
	if (C.rows != m || C.columns != p)
		throw new IllegalArgumentException("Incompatibel result matrix: "+toStringShort()+", "+ (transposeB ? B.viewDice() : B).toStringShort()+", "+C.toStringShort());
	if (this == C || B == C)
		throw new IllegalArgumentException("Matrices must not be identical");
	
	if (!ignore) C.assign(cern.jet.math.Functions.mult(beta));

	// cache views	
	final DoubleMatrix1D[] Brows = new DoubleMatrix1D[n];
	for (int i=n; --i>=0; ) Brows[i] = B.viewRow(i);
	final DoubleMatrix1D[] Crows = new DoubleMatrix1D[m];
	for (int i=m; --i>=0; ) Crows[i] = C.viewRow(i);

	final cern.jet.math.PlusMult fun = cern.jet.math.PlusMult.plusMult(0);

	int[] idx = indexes.elements();
	double[] vals = values.elements();
	for (int i=starts.length-1; --i >= 0; ) {
		int low = starts[i];
		for (int k=starts[i+1]; --k >= low; ) {
			int j = idx[k];
			fun.multiplicator = vals[k]*alpha;
			if (!transposeA)
				Crows[i].assign(Brows[j],fun);
			else
				Crows[j].assign(Brows[i],fun);
		}
	}

	return C;
}
 
Example 8
Source File: SmpBlas.java    From jAudioGIT with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void dgemm(final boolean transposeA, final boolean transposeB, final double alpha, final DoubleMatrix2D A, final DoubleMatrix2D B, final double beta, final DoubleMatrix2D C) {
	/*
	determine how to split and parallelize best into blocks
	if more B.columns than tasks --> split B.columns, as follows:
	
			xx|xx|xxx B
			xx|xx|xxx
			xx|xx|xxx
	A
	xxx     xx|xx|xxx C 
	xxx		xx|xx|xxx
	xxx		xx|xx|xxx
	xxx		xx|xx|xxx
	xxx		xx|xx|xxx

	if less B.columns than tasks --> split A.rows, as follows:
	
			xxxxxxx B
			xxxxxxx
			xxxxxxx
	A
	xxx     xxxxxxx C
	xxx     xxxxxxx
	---     -------
	xxx     xxxxxxx
	xxx     xxxxxxx
	---     -------
	xxx     xxxxxxx

	*/
	if (transposeA) {
		dgemm(false, transposeB, alpha, A.viewDice(), B, beta, C);
		return;
	}
	if (transposeB) {
		dgemm(transposeA, false, alpha, A, B.viewDice(), beta, C);
		return;
	}
	int m = A.rows();
	int n = A.columns();
	int p = B.columns();

	if (B.rows() != n)
		throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+A.toStringShort()+", "+B.toStringShort());
	if (C.rows() != m || C.columns() != p)
		throw new IllegalArgumentException("Incompatibel result matrix: "+A.toStringShort()+", "+B.toStringShort()+", "+C.toStringShort());
	if (A == C || B == C)
		throw new IllegalArgumentException("Matrices must not be identical");

	long flops = 2L*m*n*p;
	int noOfTasks = (int) Math.min(flops / 30000, this.maxThreads); // each thread should process at least 30000 flops
	boolean splitB = (p >= noOfTasks);
	int width = splitB ? p : m;
	noOfTasks = Math.min(width,noOfTasks);
	
	if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
		seqBlas.dgemm(transposeA, transposeB, alpha, A, B, beta, C);
		return;
	}
	
	// set up concurrent tasks
	int span = width/noOfTasks;
	final FJTask[] subTasks = new FJTask[noOfTasks];
	for (int i=0; i<noOfTasks; i++) {
		final int offset = i*span;
		if (i==noOfTasks-1) span = width - span*i; // last span may be a bit larger

		final DoubleMatrix2D AA,BB,CC; 
		if (splitB) { 
			// split B along columns into blocks
			AA = A;
			BB = B.viewPart(0,offset,n,span);
			CC = C.viewPart(0,offset,m,span);
		}
		else { 
			// split A along rows into blocks
			AA = A.viewPart(offset,0,span,n);
			BB = B;
			CC = C.viewPart(offset,0,span,p);
		}
				
		subTasks[i] = new FJTask() { 
			public void run() { 
				seqBlas.dgemm(transposeA,transposeB,alpha,AA,BB,beta,CC); 
				//System.out.println("Hello "+offset); 
			}
		};
	}
	
	// run tasks and wait for completion
	try { 
		this.smp.taskGroup.invoke(
			new FJTask() {
				public void run() {	
					coInvoke(subTasks);	
				}
			}
		);
	} catch (InterruptedException exc) {}
}