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 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 PivotedMatrixViewConstructorTest method pivotTest.
/**
*/
private void pivotTest(Matrix parent, int[] pivot) {
for (int row = 0; row < parent.rowSize(); row++) for (int col = 0; col < parent.columnSize(); col++) parent.set(row, col, row * parent.columnSize() + col + 1);
Matrix view = new PivotedMatrixView(parent, pivot);
int rows = parent.rowSize();
int cols = parent.columnSize();
assertEquals("Rows in view.", rows, view.rowSize());
assertEquals("Cols in view.", cols, view.columnSize());
for (int row = 0; row < rows; row++) for (int col = 0; col < cols; col++) assertEquals("Unexpected value at " + row + "x" + col, parent.get(pivot[row], pivot[col]), view.get(row, col), 0d);
int min = rows < cols ? rows : cols;
for (int idx = 0; idx < min; idx++) view.set(idx, idx, 0d);
for (int idx = 0; idx < min; idx++) assertEquals("Unexpected value set at " + idx, 0d, parent.get(pivot[idx], pivot[idx]), 0d);
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class PivotedMatrixViewConstructorTest method basicTest.
/**
*/
@Test
public void basicTest() {
Matrix m = new DenseLocalOnHeapMatrix(2, 2);
int[] pivot = new int[] { 0, 1 };
PivotedMatrixView view = new PivotedMatrixView(m, pivot);
assertEquals("Rows in view.", m.rowSize(), view.rowSize());
assertEquals("Cols in view.", m.columnSize(), view.columnSize());
assertTrue("Row pivot array in view.", Arrays.equals(pivot, view.rowPivot()));
assertTrue("Col pivot array in view.", Arrays.equals(pivot, view.columnPivot()));
Assert.assertEquals("Base matrix in view.", m, view.getBaseMatrix());
assertEquals("Row pivot value in view.", 0, view.rowPivot(0));
assertEquals("Col pivot value in view.", 0, view.columnPivot(0));
assertEquals("Row unpivot value in view.", 0, view.rowUnpivot(0));
assertEquals("Col unpivot value in view.", 0, view.columnUnpivot(0));
Matrix swap = view.swap(1, 1);
for (int row = 0; row < view.rowSize(); row++) for (int col = 0; col < view.columnSize(); col++) assertEquals("Unexpected swap value set at (" + row + "," + col + ").", view.get(row, col), swap.get(row, col), 0d);
// noinspection EqualsWithItself
assertTrue("View is expected to be equal to self.", view.equals(view));
// noinspection ObjectEqualsNull
assertFalse("View is expected to be not equal to null.", view.equals(null));
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class SparseDistributedBlockMatrixTest method testCopy.
/**
*/
public void testCopy() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
cacheMatrix = new SparseBlockDistributedMatrix(rows, cols);
cacheMatrix.set(rows - 1, cols - 1, 1);
Matrix newMatrix = cacheMatrix.copy();
assert newMatrix.columnSize() == cols;
assert newMatrix.rowSize() == rows;
assert newMatrix.get(rows - 1, cols - 1) == 1;
}
Aggregations