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;
}
});
}
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");
});
}
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();
}
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();
}
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();
}
Aggregations