use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class SparseDistributedBlockMatrixTest method testNonSquareMatrixTimes.
/**
*/
public void testNonSquareMatrixTimes() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
int size = MatrixBlockEntry.MAX_BLOCK_SIZE + 1;
int size2 = MatrixBlockEntry.MAX_BLOCK_SIZE * 2 + 1;
Matrix cacheMatrix1 = new SparseBlockDistributedMatrix(size2, size);
Matrix cacheMatrix2 = new SparseBlockDistributedMatrix(size, size2);
for (int i = 0; i < size; i++) {
cacheMatrix1.setX(i, i, i);
cacheMatrix2.setX(i, i, i);
}
Matrix res = cacheMatrix1.times(cacheMatrix2);
for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) if (i == j)
assertEquals(UNEXPECTED_VAL + " for " + i + ":" + j, i * i, res.get(i, j), PRECISION);
else
assertEquals(UNEXPECTED_VAL + " for " + i + ":" + j, 0, res.get(i, j), PRECISION);
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class EigenDecompositionTest method testNonSquareMatrix.
/**
*/
@Test
public void testNonSquareMatrix() {
EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { { 1.0d, 0.0d, 0.0d }, { 0.0d, 1.0d, 0.0d }, { 0.0d, 0.0d, 2.0d }, { 1.0d, 1.0d, 0.0d } }));
// TODO: IGNITE-5828, find out why decomposition of 3X4 matrix throws row index exception
Matrix d = decomposition.getD();
Matrix v = decomposition.getV();
assertNotNull("Matrix d is expected to be not null.", d);
assertNotNull("Matrix v is expected to be not null.", v);
assertEquals("Unexpected rows in d matrix.", 4, d.rowSize());
assertEquals("Unexpected cols in d matrix.", 4, d.columnSize());
assertEquals("Unexpected rows in v matrix.", 4, v.rowSize());
assertEquals("Unexpected cols in v matrix.", 3, v.columnSize());
assertIsDiagonal(d, true);
decomposition.destroy();
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class EigenDecompositionTest method testSymmetricMatrix.
/**
*/
@Test
public void testSymmetricMatrix() {
EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { { 1.0d, 0.0d, 0.0d, 1.0d }, { 0.0d, 1.0d, 0.0d, 1.0d }, { 0.0d, 0.0d, 2.0d, 0.0d }, { 1.0d, 1.0d, 0.0d, 2.0d } }));
Matrix d = decomposition.getD();
Matrix v = decomposition.getV();
assertNotNull("Matrix d is expected to be not null.", d);
assertNotNull("Matrix v is expected to be not null.", v);
assertEquals("Unexpected rows in d matrix.", 4, d.rowSize());
assertEquals("Unexpected cols in d matrix.", 4, d.columnSize());
assertEquals("Unexpected rows in v matrix.", 4, v.rowSize());
assertEquals("Unexpected cols in v matrix.", 4, v.columnSize());
assertIsDiagonalNonZero(d);
decomposition.destroy();
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class EigenDecompositionTest method test.
/**
*/
private void test(double[][] mRaw, double[] expRealEigenValues) {
DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(mRaw);
EigenDecomposition decomposition = new EigenDecomposition(m);
Matrix d = decomposition.getD();
Matrix v = decomposition.getV();
assertIsDiagonalNonZero(d);
// check that d's diagonal consists of eigenvalues of m.
assertDiagonalConsistsOfEigenvalues(m, d, v);
// m = v d v^{-1} is equivalent to
// m v = v d
assertMatricesAreEqual(m.times(v), v.times(d));
assertEigenvalues(decomposition, expRealEigenValues);
decomposition.destroy();
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class QRDSolverTest method basicTest.
/**
*/
@Test
public void basicTest() {
Matrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } });
QRDecomposition dec = new QRDecomposition(m);
assertTrue("Unexpected value for full rank in decomposition " + dec, dec.hasFullRank());
Matrix q = dec.getQ();
Matrix r = dec.getR();
assertNotNull("Matrix q is expected to be not null.", q);
assertNotNull("Matrix r is expected to be not null.", r);
Matrix qSafeCp = safeCopy(q);
Matrix expIdentity = qSafeCp.times(qSafeCp.transpose());
final double delta = 0.0001;
for (int row = 0; row < expIdentity.rowSize(); row++) for (int col = 0; col < expIdentity.columnSize(); col++) assertEquals("Unexpected identity matrix value at (" + row + "," + col + ").", row == col ? 1d : 0d, expIdentity.get(col, row), delta);
for (int row = 0; row < r.rowSize(); row++) for (int col = 0; col < row - 1; col++) assertEquals("Unexpected upper triangular matrix value at (" + row + "," + col + ").", 0d, r.get(row, col), delta);
Matrix recomposed = qSafeCp.times(r);
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), delta);
Matrix sol = new QRDSolver(q, r).solve(new DenseLocalOnHeapMatrix(3, 10));
assertEquals("Unexpected rows in solution matrix.", 3, sol.rowSize());
assertEquals("Unexpected cols in solution matrix.", 10, sol.columnSize());
for (int row = 0; row < sol.rowSize(); row++) for (int col = 0; col < sol.columnSize(); col++) assertEquals("Unexpected solution matrix value at (" + row + "," + col + ").", 0d, sol.get(row, col), delta);
dec.destroy();
}
Aggregations