use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class ColtOps method multiplyMatrix.
// For some reason, the parallelcolt version of zMult doesn't
// really take advantage of the available cores. This version does, but
// it seems like it only works for multiplying matrices of the same
// size.
public Matrix multiplyMatrix(Matrix matrix2) {
// return mult(matrix);
// if (matrix2.nRows() != matrix.nRows() || matrix2.nColumns() != matrix.nColumns()())
// return mult(matrix);
DoubleMatrix2D A = getData();
DoubleMatrix2D B = matrix2.getColtMatrix();
int m = A.rows();
int n = A.columns();
int p = B.columns();
// Create views into B
final DoubleMatrix1D[] Brows = new DoubleMatrix1D[n];
for (int i = n; --i >= 0; ) Brows[i] = B.viewRow(i);
// Create a series of 1D vectors
final DoubleMatrix1D[] Crows = new DoubleMatrix1D[m];
for (int i = m; --i >= 0; ) Crows[i] = B.like1D(p);
// Create the thread pools
final ExecutorService[] threadPools = new ExecutorService[nThreads];
for (int pool = 0; pool < threadPools.length; pool++) {
threadPools[pool] = Executors.newFixedThreadPool(1);
}
A.forEachNonZero(new IntIntDoubleFunction() {
public double apply(int row, int column, double value) {
Runnable r = new ThreadedDotProduct(value, Brows[column], Crows[row]);
threadPools[row % nThreads].submit(r);
return value;
}
});
for (int pool = 0; pool < threadPools.length; pool++) {
threadPools[pool].shutdown();
try {
boolean result = threadPools[pool].awaitTermination(7, TimeUnit.DAYS);
} catch (Exception e) {
}
}
// Recreate C
return new ColtMatrix(matrix, create2DMatrix(Crows));
}
use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class ColtOps method addElement.
/**
* add two matrices together
*
* @param addend the matrix to add to our matrix
*/
public void addElement(Matrix addend) {
DoubleMatrix2D data = getData();
data.forEachNonZero(new IntIntDoubleFunction() {
public double apply(int row, int column, double v) {
double addendValue = addend.doubleValue(row, column);
if (!Double.isNaN(addendValue))
return v + addendValue;
else
return v;
}
});
}
use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class ColtOps method powScalar.
/**
* raise all cells in the matrix by a power
*
* @param matrix our matrix
* @param value power to raise to each cell
*/
public void powScalar(double value) {
DoubleMatrix2D data = getData();
data.forEachNonZero(new IntIntDoubleFunction() {
public double apply(int row, int column, double v) {
return Math.pow(v, value);
}
});
}
use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class ColtOps method create2DMatrix.
private DoubleMatrix2D create2DMatrix(DoubleMatrix1D[] rows) {
int columns = (int) rows[0].size();
DoubleMatrix2D C = DoubleFactory2D.sparse.make(rows.length, columns);
for (int row = 0; row < rows.length; row++) {
for (int col = 0; col < columns; col++) {
double value = rows[row].getQuick(col);
if (value != 0.0)
C.setQuick(row, col, value);
}
}
return C;
}
use of cern.colt.matrix.tdouble.DoubleMatrix2D in project clusterMaker2 by RBVI.
the class ColtOps method mult.
public Matrix mult(Matrix b) {
/*
DoubleMatrix2D aMat = data;
DoubleMatrix2D bMat = b.getColtMatrix();
DoubleMatrix2D cMat = DoubleFactory2D.sparse.make(nRows, nColumns);
// System.out.println("aMat ("+aMat.rows()+", "+aMat.columns()+")");
// System.out.println("bMat ("+bMat.rows()+", "+bMat.columns()+")");
// System.out.println("cMat ("+cMat.rows()+", "+cMat.columns()+")");
blas.dgemm(false, false, 1.0, aMat, bMat, 0.0, cMat);
ColtMatrix c = new ColtMatrix(this, cMat);
*/
DoubleMatrix2D cMat;
if (getData().getClass().getName().indexOf("Sparse") >= 0)
cMat = DoubleFactory2D.sparse.make(matrix.nRows(), b.nColumns());
else
cMat = DoubleFactory2D.dense.make(matrix.nRows(), b.nColumns());
getData().zMult(b.getColtMatrix(), cMat);
ColtMatrix c = new ColtMatrix(matrix, cMat);
return c;
}
Aggregations