use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class BlasTest method testScalDense.
/**
* Test 'scal' operation for dense matrix.
*/
@Test
public void testScalDense() {
double[] data = new double[] { 1.0, 1.0 };
double alpha = 2.0;
DenseLocalOnHeapVector v = new DenseLocalOnHeapVector(data);
Vector exp = new DenseLocalOnHeapVector(data, true).times(alpha);
Blas.scal(alpha, v);
Assert.assertEquals(v, exp);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class BlasTest method testSprSparseDense2.
/**
* Test 'spr' operation for sparse vector v (sparse in representation, sparse in fact) and dense matrix A.
*/
@Test
public void testSprSparseDense2() {
double alpha = 3.0;
SparseLocalVector v = new SparseLocalVector(2, StorageConstants.RANDOM_ACCESS_MODE);
v.set(0, 1);
DenseLocalOnHeapVector u = new DenseLocalOnHeapVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
// m is alpha * v * v^t
DenseLocalOnHeapMatrix m = (DenseLocalOnHeapMatrix) new DenseLocalOnHeapMatrix(new double[][] { { 1.0, 0.0 }, { 0.0, 0.0 } }, StorageConstants.COLUMN_STORAGE_MODE).times(alpha);
DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 3.0, 0.0 }, { 13.0, 20.0 } }, StorageConstants.COLUMN_STORAGE_MODE);
// m := alpha * v * v.t + A
Blas.spr(alpha, v, u);
DenseLocalOnHeapMatrix mu = fromVector(u, a.rowSize(), StorageConstants.COLUMN_STORAGE_MODE, (i, j) -> i >= j);
Assert.assertEquals(m.plus(a), mu);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class TracerTest method testWriteVectorToCSVFile.
/**
*/
@Test
public void testWriteVectorToCSVFile() throws IOException {
DenseLocalOnHeapVector vector = new DenseLocalOnHeapVector(MathTestConstants.STORAGE_SIZE);
for (int i = 0; i < vector.size(); i++) vector.set(i, Math.random());
Path file = createTempFile("vector", ".csv");
Tracer.saveAsCsv(vector, DEFAULT_FORMAT, file.toString());
System.out.println("Vector exported: " + file.getFileName());
List<String> strings = Files.readAllLines(file);
Optional<String> reduce = strings.stream().reduce((s1, s2) -> s1 + s2);
String[] csvVals = reduce.orElse("").split(",");
for (int i = 0; i < vector.size(); i++) {
Double csvVal = Double.valueOf(csvVals[i]);
assertEquals("Unexpected value.", csvVal, vector.get(i), DEFAULT_DELTA);
}
Files.deleteIfExists(file);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class TracerTest method makeRandomVector.
/**
* @param size Vector size.
*/
private Vector makeRandomVector(int size) {
DenseLocalOnHeapVector vec = new DenseLocalOnHeapVector(size);
vec.assign((idx) -> Math.random());
return vec;
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class CholeskyDecompositionTest method basicTest.
/**
*/
private void basicTest(Matrix m) {
// This decomposition is useful when dealing with systems of linear equations of the form
// m x = b where m is a Hermitian matrix.
// For such systems Cholesky decomposition provides
// more effective method of solving compared to LU decomposition.
// Suppose we want to solve system
// m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs
// as a matrix of the form
// (b1, b2, ..., bm)
// to the method Cholesky::solve which returns solutions in the form
// (sol1, sol2, ..., solm)
CholeskyDecomposition dec = new CholeskyDecomposition(m);
assertEquals("Unexpected value for decomposition determinant.", 4d, dec.getDeterminant(), 0d);
Matrix l = dec.getL();
Matrix lt = dec.getLT();
assertNotNull("Matrix l is expected to be not null.", l);
assertNotNull("Matrix lt is expected to be not null.", lt);
for (int row = 0; row < l.rowSize(); row++) for (int col = 0; col < l.columnSize(); col++) assertEquals("Unexpected value transposed matrix at (" + row + "," + col + ").", l.get(row, col), lt.get(col, row), 0d);
Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } }).transpose();
Matrix sol = dec.solve(bs);
assertNotNull("Solution matrix is expected to be not null.", sol);
assertEquals("Solution rows are not as expected.", bs.rowSize(), sol.rowSize());
assertEquals("Solution columns are not as expected.", bs.columnSize(), sol.columnSize());
for (int i = 0; i < sol.columnSize(); i++) assertNotNull("Solution matrix column is expected to be not null at index " + i, sol.viewColumn(i));
Vector b = new DenseLocalOnHeapVector(new double[] { 4.0, -6.0, 7.0 });
Vector solVec = dec.solve(b);
for (int idx = 0; idx < b.size(); idx++) assertEquals("Unexpected value solution vector at " + idx, b.get(idx), solVec.get(idx), 0d);
dec.destroy();
}
Aggregations