Search in sources :

Example 16 with DoubleMatrix2D

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

the class TestMatrix2D method doubleTest25.

/**
 */
public static void doubleTest25(int size) {
    System.out.println("\n\n");
    System.out.println("initializing...");
    boolean dense = true;
    DoubleMatrix2D A;
    DoubleFactory2D factory;
    if (dense)
        factory = Factory2D.dense;
    else
        factory = Factory2D.sparse;
    double value = 0.5;
    A = factory.make(size, size, value);
    Property.generateNonSingular(A);
    cern.colt.Timer timer = new cern.colt.Timer().start();
    System.out.println(A);
    System.out.println(Algebra.ZERO.inverse(A));
    timer.stop().display();
    System.out.println("done.");
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DoubleFactory2D(cern.colt.matrix.DoubleFactory2D)

Example 17 with DoubleMatrix2D

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

the class TestMatrix2D method doubleTest21.

/**
 */
public static void doubleTest21() {
    System.out.println("\n\n");
    DoubleMatrix2D A;
    int k;
    int uk;
    int lk;
    double[][] values1 = { { 1 / 3, 2 / 3, Math.PI, 0 }, { 3, 9, 0, 0 }, { 0, 2, 7, 0 }, { 0, 0, 3, 9 } };
    A = Factory2D.make(values1);
    System.out.println(A);
    System.out.println(new cern.colt.matrix.doublealgo.Formatter(null).toString(A));
// System.out.println("\n\n"+LinearAlgebra.toVerboseString(A));
// System.out.println(new LUDecomposition(A));
// System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 18 with DoubleMatrix2D

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

the class TestMatrix2D method doubleTest20.

/**
 */
public static void doubleTest20() {
    System.out.println("\n\n");
    DoubleMatrix2D A;
    int k;
    int uk;
    int lk;
    double[][] values1 = { { 0, 1, 0, 0 }, { 3, 0, 2, 0 }, { 0, 2, 0, 3 }, { 0, 0, 1, 0 } };
    A = Factory2D.make(values1);
    System.out.println("\n\n" + LinearAlgebra.toVerboseString(A));
    // System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
    double[][] values2 = { { 1.0000000000000167, -0.3623577544766736, -0.3623577544766736 }, { 0, 0.9320390859672374, -0.3377315902755755 }, { 0, 0, 0.8686968577706282 }, { 0, 0, 0 }, { 0, 0, 0 } };
    A = Factory2D.make(values2);
    System.out.println("\n\n" + LinearAlgebra.toVerboseString(A));
    // System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
    double[][] values3 = { { 611, 196, -192, 407, -8, -52, -49, 29 }, { 196, 899, 113, -192, -71, -43, -8, -44 }, { -192, 113, 899, 196, 61, 49, 8, 52 }, { 407, -192, 196, 611, 8, 44, 59, -23 }, { -8, -71, 61, 8, 411, -599, 208, 208 }, { -52, -43, 49, 44, -599, 411, 208, 208 }, { -49, -8, 8, 59, 208, 208, 99, -911 }, { 29, -44, 52, -23, 208, 208, -911, 99 } };
    A = Factory2D.make(values3);
    System.out.println("\n\n" + LinearAlgebra.toVerboseString(A));
    // System.out.println("\n\nbandwidth="+k+" "+cern.colt.matrixpattern.Converting.toHTML(A.toString()));
    // Exact eigenvalues from Westlake (1968), p.150 (ei'vectors given too):
    double a = Math.sqrt(10405);
    double b = Math.sqrt(26);
    double[] e = { -10 * a, 0, 510 - 100 * b, 1000, 1000, 510 + 100 * b, 1020, 10 * a };
    System.out.println(Factory1D.dense.make(e));
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 19 with DoubleMatrix2D

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

the class TestMatrix2D method doubleTest8.

/**
 */
public static void doubleTest8() {
    int rows = 2;
    // make a 4*5 matrix
    int columns = 3;
    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);
    DoubleMatrix2D view1 = master.viewDice();
    System.out.println("view1=" + view1);
    DoubleMatrix2D view2 = view1.viewDice();
    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 20 with DoubleMatrix2D

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

the class Algebra method pow.

/**
 * Linear algebraic matrix power; <tt>B = A<sup>k</sup> <==> B = A*A*...*A</tt>.
 * <ul>
 * <li><tt>p &gt;= 1: B = A*A*...*A</tt>.</li>
 * <li><tt>p == 0: B = identity matrix</tt>.</li>
 * <li><tt>p &lt;  0: B = pow(inverse(A),-p)</tt>.</li>
 * </ul>
 * Implementation: Based on logarithms of 2, memory usage minimized.
 * @param A the source matrix; must be square; stays unaffected by this operation.
 * @param p the exponent, can be any number.
 * @return <tt>B</tt>, a newly constructed result matrix; storage-independent of <tt>A</tt>.
 *
 * @throws IllegalArgumentException if <tt>!property().isSquare(A)</tt>.
 */
public DoubleMatrix2D pow(DoubleMatrix2D A, int p) {
    // matrix multiplication based on log2 method: A*A*....*A is slow, ((A * A)^2)^2 * ... is faster
    // allocates two auxiliary matrices as work space
    // for parallel matrix mult; if not initialized defaults to sequential blas
    Blas blas = SmpBlas.smpBlas;
    Property.DEFAULT.checkSquare(A);
    if (p < 0) {
        A = inverse(A);
        p = -p;
    }
    if (p == 0)
        return DoubleFactory2D.dense.identity(A.rows());
    // temporary
    DoubleMatrix2D T = A.like();
    // safes one auxiliary matrix allocation
    if (p == 1)
        return T.assign(A);
    if (p == 2) {
        // mult(A,A); // safes one auxiliary matrix allocation
        blas.dgemm(false, false, 1, A, A, 0, T);
        return T;
    }
    // index of highest bit in state "true"
    int k = cern.colt.bitvector.QuickBitVector.mostSignificantBit(p);
    /*
	this is the naive version:
	DoubleMatrix2D B = A.copy();
	for (int i=0; i<p-1; i++) {
		B = mult(B,A);
	}
	return B;
	*/
    // here comes the optimized version:
    // cern.colt.Timer timer = new cern.colt.Timer().start();
    int i = 0;
    while (i <= k && (p & (1 << i)) == 0) {
        // while (bit i of p == false)
        // A = mult(A,A); would allocate a lot of temporary memory
        // A.zMult(A,T);
        blas.dgemm(false, false, 1, A, A, 0, T);
        // swap A with T
        DoubleMatrix2D swap = A;
        // swap A with T
        A = T;
        // swap A with T
        T = swap;
        i++;
    }
    DoubleMatrix2D B = A.copy();
    i++;
    for (; i <= k; i++) {
        // A = mult(A,A); would allocate a lot of temporary memory
        // A.zMult(A,T);
        blas.dgemm(false, false, 1, A, A, 0, T);
        // swap A with T
        DoubleMatrix2D swap = A;
        // swap A with T
        A = T;
        // swap A with T
        T = swap;
        if ((p & (1 << i)) != 0) {
            // if (bit i of p == true)
            // B = mult(B,A); would allocate a lot of temporary memory
            // B.zMult(A,T);
            blas.dgemm(false, false, 1, B, A, 0, T);
            // swap B with T
            swap = B;
            // swap B with T
            B = T;
            // swap B with T
            T = swap;
        }
    }
    // timer.stop().display();
    return B;
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

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