Search in sources :

Example 16 with Matrix

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

the class MatrixImplementationsTest method testDeterminant.

/** */
@Test
public void testDeterminant() {
    consumeSampleMatrix((m, desc) -> {
        if (m.rowSize() != m.columnSize())
            return;
        if (ignore(m.getClass()))
            return;
        double[][] doubles = fillIntAndReturn(m);
        if (m.rowSize() == 1) {
            assertEquals("Unexpected value " + desc, m.determinant(), doubles[0][0], 0d);
            return;
        }
        if (m.rowSize() == 2) {
            double det = doubles[0][0] * doubles[1][1] - doubles[0][1] * doubles[1][0];
            assertEquals("Unexpected value " + desc, m.determinant(), det, 0d);
            return;
        }
        if (m.rowSize() > 512)
            // IMPL NOTE if row size >= 30000 it takes unacceptably long for normal test run.
            return;
        Matrix diagMtx = m.like(m.rowSize(), m.columnSize());
        diagMtx.assign(0);
        for (int i = 0; i < m.rowSize(); i++) diagMtx.set(i, i, m.get(i, i));
        double det = 1;
        for (int i = 0; i < diagMtx.rowSize(); i++) det *= diagMtx.get(i, i);
        try {
            assertEquals("Unexpected value " + desc, det, diagMtx.determinant(), DEFAULT_DELTA);
        } catch (Exception e) {
            System.out.println(desc);
            throw e;
        }
    });
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) IndexException(org.apache.ignite.ml.math.exceptions.IndexException) ColumnIndexException(org.apache.ignite.ml.math.exceptions.ColumnIndexException) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException) RowIndexException(org.apache.ignite.ml.math.exceptions.RowIndexException) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 17 with Matrix

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

the class MatrixImplementationsTest method testSwapRows.

/** */
@Test
public void testSwapRows() {
    consumeSampleMatrix((m, desc) -> {
        if (readOnly(m))
            return;
        double[][] doubles = fillAndReturn(m);
        final int swap_i = m.rowSize() == 1 ? 0 : 1;
        final int swap_j = 0;
        Matrix swap = m.swapRows(swap_i, swap_j);
        for (int col = 0; col < m.columnSize(); col++) {
            assertEquals("Unexpected value for " + desc + " at col " + col + ", swap_i " + swap_i, swap.get(swap_i, col), doubles[swap_j][col], 0d);
            assertEquals("Unexpected value for " + desc + " at col " + col + ", swap_j " + swap_j, swap.get(swap_j, col), doubles[swap_i][col], 0d);
        }
        testInvalidRowIndex(() -> m.swapRows(-1, 0), desc + " negative first swap index");
        testInvalidRowIndex(() -> m.swapRows(0, -1), desc + " negative second swap index");
        testInvalidRowIndex(() -> m.swapRows(m.rowSize(), 0), desc + " too large first swap index");
        testInvalidRowIndex(() -> m.swapRows(0, m.rowSize()), desc + " too large second swap index");
    });
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 18 with Matrix

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

the class SingularValueDecompositionTest method basicTest.

/** */
private void basicTest(Matrix m) {
    SingularValueDecomposition dec = new SingularValueDecomposition(m);
    assertEquals("Unexpected value for singular values size.", 3, dec.getSingularValues().length);
    Matrix s = dec.getS();
    Matrix u = dec.getU();
    Matrix v = dec.getV();
    Matrix covariance = dec.getCovariance(0.5);
    assertNotNull("Matrix s is expected to be not null.", s);
    assertNotNull("Matrix u is expected to be not null.", u);
    assertNotNull("Matrix v is expected to be not null.", v);
    assertNotNull("Covariance matrix is expected to be not null.", covariance);
    assertTrue("Decomposition cond is expected to be positive.", dec.cond() > 0);
    assertTrue("Decomposition norm2 is expected to be positive.", dec.norm2() > 0);
    assertEquals("Decomposition rank differs from expected.", 3, dec.rank());
    assertEquals("Decomposition singular values size differs from expected.", 3, dec.getSingularValues().length);
    Matrix recomposed = (u.times(s).times(v.transpose()));
    for (int row = 0; row < m.rowSize(); row++) for (int col = 0; col < m.columnSize(); col++) assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", m.get(row, col), recomposed.get(row, col), 0.001);
    for (int row = 0; row < covariance.rowSize(); row++) for (int col = row + 1; col < covariance.columnSize(); col++) assertEquals("Unexpected covariance matrix value at (" + row + "," + col + ").", covariance.get(row, col), covariance.get(col, row), 0.001);
    dec.destroy();
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 19 with Matrix

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

the class SingularValueDecompositionTest method rowsLessThanColumnsTest.

/** */
@Test
public void rowsLessThanColumnsTest() {
    DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d } });
    SingularValueDecomposition dec = new SingularValueDecomposition(m);
    assertEquals("Unexpected value for singular values size.", 2, dec.getSingularValues().length);
    Matrix s = dec.getS();
    Matrix u = dec.getU();
    Matrix v = dec.getV();
    Matrix covariance = dec.getCovariance(0.5);
    assertNotNull("Matrix s is expected to be not null.", s);
    assertNotNull("Matrix u is expected to be not null.", u);
    assertNotNull("Matrix v is expected to be not null.", v);
    assertNotNull("Covariance matrix is expected to be not null.", covariance);
    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) Test(org.junit.Test)

Example 20 with Matrix

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

the class LUDecompositionTest method getU.

/** */
@Test
public void getU() throws Exception {
    Matrix luDecompositionU = new LUDecomposition(testMatrix).getU();
    assertEquals("Unexpected row size.", testU.rowSize(), luDecompositionU.rowSize());
    assertEquals("Unexpected column size.", testU.columnSize(), luDecompositionU.columnSize());
    for (int i = 0; i < testU.rowSize(); i++) for (int j = 0; j < testU.columnSize(); j++) assertEquals("Unexpected value at (" + i + "," + j + ").", testU.getX(i, j), luDecompositionU.getX(i, j), 0.0000001d);
    luDecompositionU.destroy();
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Aggregations

Matrix (org.apache.ignite.ml.math.Matrix)78 Test (org.junit.Test)38 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)25 ExternalizeTest (org.apache.ignite.ml.math.ExternalizeTest)17 Vector (org.apache.ignite.ml.math.Vector)7 DenseLocalOffHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOffHeapMatrix)6 SparseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix)6 RandomMatrix (org.apache.ignite.ml.math.impls.matrix.RandomMatrix)5 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)4 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)3 UnsupportedOperationException (org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)3 LUDecomposition (org.apache.ignite.ml.math.decompositions.LUDecomposition)2 PivotedMatrixView (org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView)2 CholeskyDecomposition (org.apache.ignite.ml.math.decompositions.CholeskyDecomposition)1 ColumnIndexException (org.apache.ignite.ml.math.exceptions.ColumnIndexException)1 IndexException (org.apache.ignite.ml.math.exceptions.IndexException)1 RowIndexException (org.apache.ignite.ml.math.exceptions.RowIndexException)1 AbstractMatrix (org.apache.ignite.ml.math.impls.matrix.AbstractMatrix)1 MatrixView (org.apache.ignite.ml.math.impls.matrix.MatrixView)1 MatrixDelegateStorage (org.apache.ignite.ml.math.impls.storage.matrix.MatrixDelegateStorage)1