Search in sources :

Example 91 with Matrix

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

the class MatrixImplementationsTest method testMinusMatrix.

/**
 */
@Test
public void testMinusMatrix() {
    consumeSampleMatrix((m, desc) -> {
        if (ignore(m.getClass()))
            return;
        fillMatrix(m);
        Matrix minus = m.minus(m);
        for (int i = 0; i < m.rowSize(); i++) for (int j = 0; j < m.columnSize(); j++) assertEquals("Unexpected value for " + desc + " at (" + i + "," + j + ")", 0.0, minus.get(i, j), 0d);
    });
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 92 with Matrix

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

the class MatrixImplementationsTest method testCopy.

/**
 */
@Test
public void testCopy() {
    consumeSampleMatrix((m, desc) -> {
        Matrix cp = m.copy();
        assertTrue("Incorrect copy for empty matrix " + desc, cp.equals(m));
        if (!readOnly(m))
            fillMatrix(m);
        cp = m.copy();
        assertTrue("Incorrect copy for matrix " + desc, cp.equals(m));
    });
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 93 with Matrix

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

the class MatrixImplementationsTest method testTimesMatrix.

/**
 */
@Test
public void testTimesMatrix() {
    consumeSampleMatrix((m, desc) -> {
        if (ignore(m.getClass()))
            return;
        if (m instanceof DenseLocalOffHeapMatrix)
            return;
        double[][] data = fillAndReturn(m);
        double[] arr = fillArray(m.columnSize());
        Matrix mult = m.like(m.columnSize(), 1);
        mult.setColumn(0, arr);
        Matrix times = m.times(mult);
        assertEquals("Unexpected rows for " + desc, times.rowSize(), m.rowSize());
        assertEquals("Unexpected cols for " + desc, times.columnSize(), 1);
        for (int i = 0; i < m.rowSize(); i++) {
            double exp = 0.0;
            for (int j = 0; j < m.columnSize(); j++) exp += arr[j] * data[i][j];
            assertEquals("Unexpected value for " + desc + " at " + i, exp, times.get(i, 0), DEFAULT_DELTA);
        }
        testInvalidCardinality(() -> m.times(new DenseLocalOnHeapMatrix(m.columnSize() + 1, 1)), desc);
    });
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 94 with Matrix

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

the class MatrixImplementationsTest method testTranspose.

/**
 */
@Test
public void testTranspose() {
    consumeSampleMatrix((m, desc) -> {
        if (ignore(m.getClass()))
            return;
        fillMatrix(m);
        Matrix transpose = m.transpose();
        for (int i = 0; i < m.rowSize(); i++) for (int j = 0; j < m.columnSize(); j++) assertEquals("Unexpected value for " + desc + " at (" + i + "," + j + ")", m.get(i, j), transpose.get(j, i), 0d);
    });
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 95 with Matrix

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

the class MatrixImplementationsTest method testLike.

/**
 */
@Test
public void testLike() {
    consumeSampleMatrix((m, desc) -> {
        Class<? extends Matrix> cls = likeMatrixType(m);
        if (cls != null) {
            Matrix like = m.like(m.rowSize(), m.columnSize());
            assertEquals("Wrong \"like\" matrix for " + desc + "; Unexpected rows.", like.rowSize(), m.rowSize());
            assertEquals("Wrong \"like\" matrix for " + desc + "; Unexpected columns.", like.columnSize(), m.columnSize());
            assertEquals("Wrong \"like\" matrix for " + desc + "; Unexpected class: " + like.getClass().toString(), cls, like.getClass());
            return;
        }
        boolean expECaught = false;
        try {
            m.like(1, 1);
        } catch (UnsupportedOperationException uoe) {
            expECaught = true;
        }
        assertTrue("Expected exception was not caught for " + desc, expECaught);
    });
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

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