Search in sources :

Example 66 with Matrix

use of org.apache.ignite.ml.math.Matrix in project ignite by apache.

the class QRDecompositionExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println(">>> QR decomposition example started.");
    Matrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } });
    System.out.println("\n>>> Input matrix:");
    Tracer.showAscii(m);
    QRDecomposition dec = new QRDecomposition(m);
    System.out.println("\n>>> Value for full rank in decomposition: [" + dec.hasFullRank() + "].");
    Matrix q = dec.getQ();
    Matrix r = dec.getR();
    System.out.println("\n>>> Orthogonal matrix Q:");
    Tracer.showAscii(q);
    System.out.println("\n>>> Upper triangular matrix R:");
    Tracer.showAscii(r);
    Matrix qSafeCp = safeCopy(q);
    Matrix identity = qSafeCp.times(qSafeCp.transpose());
    System.out.println("\n>>> Identity matrix obtained from Q:");
    Tracer.showAscii(identity);
    Matrix recomposed = qSafeCp.times(r);
    System.out.println("\n>>> Recomposed input matrix:");
    Tracer.showAscii(recomposed);
    Matrix sol = dec.solve(new DenseLocalOnHeapMatrix(3, 10));
    System.out.println("\n>>> Solved matrix:");
    Tracer.showAscii(sol);
    dec.destroy();
    System.out.println("\n>>> QR decomposition example completed.");
}
Also used : QRDecomposition(org.apache.ignite.ml.math.decompositions.QRDecomposition) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 67 with Matrix

use of org.apache.ignite.ml.math.Matrix in project ignite by apache.

the class MatrixExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Basic Matrix API usage example started.");
    System.out.println("\n>>> Creating a matrix to be transposed.");
    double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
    Matrix m = new DenseLocalOnHeapMatrix(data);
    Matrix transposed = m.transpose();
    System.out.println(">>> Matrix: ");
    MatrixExampleUtil.print(m);
    System.out.println(">>> Transposed matrix: ");
    MatrixExampleUtil.print(transposed);
    MatrixExampleUtil.verifyTransposition(m, transposed);
    System.out.println("\n>>> Creating matrices to be multiplied.");
    double[][] data1 = new double[][] { { 1, 2 }, { 3, 4 } };
    double[][] data2 = new double[][] { { 5, 6 }, { 7, 8 } };
    Matrix m1 = new DenseLocalOnHeapMatrix(data1);
    Matrix m2 = new DenseLocalOnHeapMatrix(data2);
    Matrix mult = m1.times(m2);
    System.out.println(">>> First matrix: ");
    MatrixExampleUtil.print(m1);
    System.out.println(">>> Second matrix: ");
    MatrixExampleUtil.print(m2);
    System.out.println(">>> Matrix product: ");
    MatrixExampleUtil.print(mult);
    System.out.println("\n>>> Calculating matrices determinants.");
    double det1 = m1.determinant();
    double det2 = m2.determinant();
    double detMult = mult.determinant();
    boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d;
    System.out.println(">>> First matrix determinant: [" + det1 + "].");
    System.out.println(">>> Second matrix determinant: [" + det2 + "].");
    System.out.println(">>> Matrix product determinant: [" + detMult + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "].");
    System.out.println("Determinant of product matrix [" + detMult + "] should be equal to product of determinants [" + (det1 * det2) + "].");
    System.out.println("\n>>> Basic Matrix API usage example completed.");
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 68 with Matrix

use of org.apache.ignite.ml.math.Matrix in project ignite by apache.

the class CholeskyDecompositionExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println(">>> Cholesky decomposition example started.");
    // Let's compute a Cholesky decomposition of Hermitian matrix m:
    // m = l l^{*}, where
    // l is a lower triangular matrix
    // l^{*} is its conjugate transpose
    DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } });
    System.out.println("\n>>> Matrix m for decomposition: ");
    Tracer.showAscii(m);
    // This decomposition is useful when dealing with systems of linear equations of the form
    // m x = b where m is a Hermitian matrix.
    // For such systems Cholesky decomposition provides
    // more effective method of solving compared to LU decomposition.
    // Suppose we want to solve system
    // m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs
    // as a matrix of the form
    // (b1, b2, ..., bm)
    // to the method Cholesky::solve which returns solutions in the form
    // (sol1, sol2, ..., solm)
    CholeskyDecomposition dec = new CholeskyDecomposition(m);
    System.out.println("\n>>> Made decomposition m = l * l^{*}.");
    System.out.println(">>> Matrix l is ");
    Tracer.showAscii(dec.getL());
    System.out.println(">>> Matrix l^{*} is ");
    Tracer.showAscii(dec.getLT());
    Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } }).transpose();
    System.out.println("\n>>> Solving systems of linear equations of the form m x = b for various bs represented by columns of matrix");
    Tracer.showAscii(bs);
    Matrix sol = dec.solve(bs);
    System.out.println("\n>>> List of solutions: ");
    for (int i = 0; i < sol.columnSize(); i++) Tracer.showAscii(sol.viewColumn(i));
    System.out.println("\n>>> Cholesky decomposition example completed.");
}
Also used : CholeskyDecomposition(org.apache.ignite.ml.math.decompositions.CholeskyDecomposition) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 69 with Matrix

use of org.apache.ignite.ml.math.Matrix in project ignite by apache.

the class IgniteSingularValueDecompositionBenchmark method runSingularValueDecomposition.

/**
 * Based on SingularValueDecompositionTest#basicTest.
 */
private void runSingularValueDecomposition() {
    Matrix m = new DenseLocalOnHeapMatrix(new DataChanger.Scale().mutate(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } }));
    SingularValueDecomposition dec = new SingularValueDecomposition(m);
    Matrix s = dec.getS();
    Matrix u = dec.getU();
    Matrix v = dec.getV();
    u.times(s).times(v.transpose());
    dec.destroy();
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) SingularValueDecomposition(org.apache.ignite.ml.math.decompositions.SingularValueDecomposition)

Example 70 with Matrix

use of org.apache.ignite.ml.math.Matrix in project ignite by apache.

the class IgniteAbstractMatrixMulBenchmark method test.

/**
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    final double scale = DataChanger.next();
    // Create IgniteThread, we may want to work with SparseDistributedMatrix inside IgniteThread
    // because we create ignite cache internally.
    IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), this.getClass().getSimpleName(), new Runnable() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void run() {
            Matrix m1, m2, m3, m4, m5, m6;
            Matrix m7 = times(m1 = createAndFill(dataSquare, scale), m2 = createAndFill(dataSquare, scale));
            Matrix m8 = times(m3 = createAndFill(dataRect1, scale), m4 = createAndFill(dataRect2, scale));
            Matrix m9 = times(m5 = createAndFill(dataRect2, scale), m6 = createAndFill(dataRect1, scale));
            m1.destroy();
            m2.destroy();
            m3.destroy();
            m4.destroy();
            m5.destroy();
            m6.destroy();
            m7.destroy();
            m8.destroy();
            m9.destroy();
        }
    });
    igniteThread.start();
    igniteThread.join();
    return true;
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) IgniteThread(org.apache.ignite.thread.IgniteThread)

Aggregations

Matrix (org.apache.ignite.ml.math.Matrix)131 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)51 Test (org.junit.Test)48 Vector (org.apache.ignite.ml.math.Vector)30 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)18 ExternalizeTest (org.apache.ignite.ml.math.ExternalizeTest)17 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)10 Random (java.util.Random)6 DenseLocalOffHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOffHeapMatrix)6 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)6 SparseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix)6 Ignite (org.apache.ignite.Ignite)5 RandomMatrix (org.apache.ignite.ml.math.impls.matrix.RandomMatrix)5 FunctionVector (org.apache.ignite.ml.math.impls.vector.FunctionVector)5 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)4 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)4 IgniteThread (org.apache.ignite.thread.IgniteThread)4 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)3 LUDecomposition (org.apache.ignite.ml.math.decompositions.LUDecomposition)3 QRDecomposition (org.apache.ignite.ml.math.decompositions.QRDecomposition)3