Search in sources :

Example 1 with SparseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix in project ignite by apache.

the class SparseMatrixExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Sparse 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 SparseLocalOnHeapMatrix(data.length, data[0].length);
    m.assign(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 SparseLocalOnHeapMatrix(data1.length, data1[0].length);
    Matrix m2 = new SparseLocalOnHeapMatrix(data2.length, data2[0].length);
    m1.assign(data1);
    m2.assign(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>>> Sparse matrix API usage example completed.");
}
Also used : SparseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix) Matrix(org.apache.ignite.ml.math.Matrix) SparseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix)

Aggregations

Matrix (org.apache.ignite.ml.math.Matrix)1 SparseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix)1