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 MatrixImplementationsTest method testPlus.
/**
*/
@Test
public void testPlus() {
consumeSampleMatrix((m, desc) -> {
if (readOnly(m))
return;
double[][] data = fillAndReturn(m);
double plusVal = Math.random();
Matrix plus = m.plus(plusVal);
for (int i = 0; i < m.rowSize(); i++) for (int j = 0; j < m.columnSize(); j++) assertEquals("Unexpected value for " + desc + " at (" + i + "," + j + ")", data[i][j] + plusVal, plus.get(i, j), 0d);
});
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class MatrixImplementationsTest method testTimes.
/**
*/
@Test
public void testTimes() {
consumeSampleMatrix((m, desc) -> {
if (readOnly(m))
return;
double[][] data = fillAndReturn(m);
double timeVal = Math.random();
Matrix times = m.times(timeVal);
for (int i = 0; i < m.rowSize(); i++) for (int j = 0; j < m.columnSize(); j++) assertEquals("Unexpected value for " + desc + " at (" + i + "," + j + ")", data[i][j] * timeVal, times.get(i, j), 0d);
});
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class MatrixViewConstructorTest method invalidArgsTest.
/**
*/
@Test
public void invalidArgsTest() {
Matrix m = new DenseLocalOnHeapMatrix(1, 1);
DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new MatrixView((Matrix) null, 0, 0, 1, 1), "Null parent matrix.");
DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new MatrixView(m, -1, 0, 1, 1), "Invalid row offset.");
DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new MatrixView(m, 0, -1, 1, 1), "Invalid col offset.");
DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new MatrixView(m, 0, 0, 0, 1), "Invalid rows.");
DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new MatrixView(m, 0, 0, 1, 0), "Invalid cols.");
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class SparseDistributedMatrixTest method testMatrixTimes.
/**
*/
public void testMatrixTimes() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
int size = MATRIX_SIZE;
SparseDistributedMatrix cacheMatrix1 = new SparseDistributedMatrix(size, size, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
SparseDistributedMatrix cacheMatrix2 = new SparseDistributedMatrix(size, size, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
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, i * i, res.get(i, j), PRECISION);
else
assertEquals(UNEXPECTED_VAL, 0, res.get(i, j), PRECISION);
}
Aggregations