Search in sources :

Example 31 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class TestMatrix2D method doubleTest23.

/**
 */
public static void doubleTest23(int runs, int size, double nonZeroFraction, boolean dense) {
    System.out.println("\n\n");
    System.out.println("initializing...");
    DoubleMatrix2D A, LU, I, Inv;
    DoubleMatrix1D b, solved;
    double mean = 5.0;
    double stdDev = 3.0;
    cern.jet.random.Normal random = new cern.jet.random.Normal(mean, stdDev, new cern.jet.random.engine.MersenneTwister());
    System.out.println("sampling...");
    double value = 2;
    if (dense)
        A = Factory2D.dense.sample(size, size, value, nonZeroFraction);
    else
        A = Factory2D.sparse.sample(size, size, value, nonZeroFraction);
    b = A.like1D(size).assign(1);
    // A.assign(random);
    // A.assign(F.rint); // round
    System.out.println("generating invertible matrix...");
    Property.generateNonSingular(A);
    // I = Factory2D.identity(size);
    LU = A.like();
    solved = b.like();
    // Inv = Factory2D.make(size,size);
    LUDecompositionQuick lu = new LUDecompositionQuick();
    System.out.println("benchmarking assignment...");
    cern.colt.Timer timer = new cern.colt.Timer().start();
    LU.assign(A);
    solved.assign(b);
    timer.stop().display();
    LU.assign(A);
    lu.decompose(LU);
    System.out.println("benchmarking LU...");
    timer.reset().start();
    for (int i = runs; --i >= 0; ) {
        solved.assign(b);
        // Inv.assign(I);
        // lu.decompose(LU);
        lu.solve(solved);
    // lu.solve(Inv);
    }
    timer.stop().display();
    // System.out.println("A="+A);
    // System.out.println("LU="+LU);
    // System.out.println("U="+lu.getU());
    // System.out.println("L="+lu.getL());
    System.out.println("done.");
}
Also used : LUDecompositionQuick(cern.colt.matrix.linalg.LUDecompositionQuick) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D)

Example 32 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class TestMatrix2D method doubleTest7.

/**
 */
public static void doubleTest7() {
    int rows = 4;
    // make a 4*5 matrix
    int columns = 5;
    DoubleMatrix2D master = Factory2D.ascending(rows, columns);
    // master.assign(1); // set all cells to 1
    System.out.println("\n" + master);
    // master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
    // System.out.println("\n"+master);
    int[] rowIndexes = { 0, 1, 3, 0 };
    int[] columnIndexes = { 0, 2 };
    DoubleMatrix2D view1 = master.viewSelection(rowIndexes, columnIndexes);
    System.out.println("view1=" + view1);
    DoubleMatrix2D view2 = view1.viewPart(0, 0, 2, 2);
    System.out.println("view2=" + view2);
    view2.assign(-1);
    System.out.println("master replaced" + master);
    System.out.println("flip1 replaced" + view1);
    System.out.println("flip2 replaced" + view2);
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 33 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class TestMatrix2D method doubleTest27.

/**
 */
public static void doubleTest27() {
    System.out.println("\n\n");
    System.out.println("initializing...");
    int rows = 51;
    int columns = 10;
    double[][] trainingSet = new double[columns][rows];
    for (int i = columns; --i >= 0; ) trainingSet[i][i] = 2.0;
    int patternIndex = 0;
    int unitIndex = 0;
    DoubleMatrix2D patternMatrix = null;
    DoubleMatrix2D transposeMatrix = null;
    DoubleMatrix2D QMatrix = null;
    DoubleMatrix2D inverseQMatrix = null;
    DoubleMatrix2D pseudoInverseMatrix = null;
    DoubleMatrix2D weightMatrix = null;
    // form a matrix with the columns as training vectors
    patternMatrix = DoubleFactory2D.dense.make(rows, columns);
    // copy the patterns into the matrix
    for (patternIndex = 0; patternIndex < columns; patternIndex++) {
        for (unitIndex = 0; unitIndex < rows; unitIndex++) {
            patternMatrix.setQuick(unitIndex, patternIndex, trainingSet[patternIndex][unitIndex]);
        }
    }
    transposeMatrix = Algebra.DEFAULT.transpose(patternMatrix);
    QMatrix = Algebra.DEFAULT.mult(transposeMatrix, patternMatrix);
    inverseQMatrix = Algebra.DEFAULT.inverse(QMatrix);
    pseudoInverseMatrix = Algebra.DEFAULT.mult(inverseQMatrix, transposeMatrix);
    weightMatrix = Algebra.DEFAULT.mult(patternMatrix, pseudoInverseMatrix);
    System.out.println("done.");
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 34 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class TestMatrix2D method testLU.

/**
 */
public static void testLU() {
    double[][] vals = { { -0.074683, 0.321248, -0.014656, 0.286586, 0 }, { -0.344852, -0.16278, 0.173711, 0.00064, 0 }, { -0.181924, -0.092926, 0.184153, 0.177966, 1 }, { -0.166829, -0.10321, 0.582301, 0.142583, 0 }, { 0, -0.112952, -0.04932, -0.700157, 0 }, { 0, 0, 0, 0, 0 } };
    // see values below...
    DoubleMatrix2D H = new DenseDoubleMatrix2D(vals);
    System.out.println("\nHplus=" + H.viewDice().zMult(H, null));
    DoubleMatrix2D Hplus = Algebra.DEFAULT.inverse(H.viewDice().zMult(H, null)).zMult(H.viewDice(), null);
    Hplus.assign(cern.jet.math.Functions.round(1.0E-10));
    System.out.println("\nHplus=" + Hplus);
/*
DoubleMatrix2D HtH = new DenseDoubleMatrix2D( 5, 5 );
DoubleMatrix2D Hplus = new DenseDoubleMatrix2D( 5, 6 );
LUDecompositionQuick LUD = new LUDecompositionQuick();
		//H.zMult( H, HtH, 1, 0, true, false );
		//DoubleMatrix2D res = Algebra.DEFAULT.inverse(HtH).zMult(H,null,1,0,false,true);
		LUD.decompose( HtH );
		// first fill Hplus with the transpose of H...
		for (int i = 0; i < 6; i++ ) {
			for ( int j = 0; j < 5; j++ ) {
				Hplus.set( j, i, H.get( i, j ) );
			}
		}
		LUD.solve( Hplus );

		DoubleMatrix2D perm = Algebra.DEFAULT.permute(Hplus, null,LUD.getPivot());
		DoubleMatrix2D inv = Algebra.DEFAULT.inverse(HtH);//.zMult(H,null,1,0,false,true);
		*/
// in matlab...
// Hplus = inv(H' * H) * H'
// System.out.println("\nLU="+LUD);
// System.out.println("\nHplus="+Hplus);
// System.out.println("\nperm="+perm);
// System.out.println("\ninv="+inv);
// System.out.println("\nres="+res);
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 35 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class SmpBlas method dgemv.

public void dgemv(final boolean transposeA, final double alpha, DoubleMatrix2D A, final DoubleMatrix1D x, final double beta, DoubleMatrix1D y) {
    /*
	split A, as follows:
	
			x x
			x
			x
	A
	xxx     x y
	xxx     x
	---     -
	xxx     x
	xxx     x
	---     -
	xxx     x

	*/
    if (transposeA) {
        dgemv(false, alpha, A.viewDice(), x, beta, y);
        return;
    }
    int m = A.rows();
    int n = A.columns();
    long flops = 2L * m * n;
    // each thread should process at least 30000 flops
    int noOfTasks = (int) Math.min(flops / 30000, this.maxThreads);
    int width = A.rows();
    noOfTasks = Math.min(width, noOfTasks);
    if (noOfTasks < 2) {
        // parallelization doesn't pay off (too much start up overhead)
        seqBlas.dgemv(transposeA, alpha, A, x, beta, y);
        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;
        // last span may be a bit larger
        if (i == noOfTasks - 1)
            span = width - span * i;
        // split A along rows into blocks
        final DoubleMatrix2D AA = A.viewPart(offset, 0, span, n);
        final DoubleMatrix1D yy = y.viewPart(offset, span);
        subTasks[i] = new FJTask() {

            public void run() {
                seqBlas.dgemv(transposeA, alpha, AA, x, beta, yy);
            // 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) {
    }
}
Also used : FJTask(EDU.oswego.cs.dl.util.concurrent.FJTask) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D)

Aggregations

DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)137 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)39 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)37 Algebra (cern.colt.matrix.linalg.Algebra)16 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)13 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)13 Node (edu.cmu.tetrad.graph.Node)11 Graph (edu.cmu.tetrad.graph.Graph)8 Test (org.junit.Test)6 DoubleMatrixReader (ubic.basecode.io.reader.DoubleMatrixReader)6 StringMatrixReader (ubic.basecode.io.reader.StringMatrixReader)6 DataSet (edu.cmu.tetrad.data.DataSet)5 DoubleArrayList (cern.colt.list.DoubleArrayList)4 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)4 DenseDoubleMatrix (ubic.basecode.dataStructure.matrix.DenseDoubleMatrix)4 AbstractFormatter (cern.colt.matrix.impl.AbstractFormatter)3 RobustEigenDecomposition (dr.math.matrixAlgebra.RobustEigenDecomposition)3 Endpoint (edu.cmu.tetrad.graph.Endpoint)3 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)3 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)3