Search in sources :

Example 81 with DenseLocalOnHeapVector

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);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 82 with DenseLocalOnHeapVector

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);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) Test(org.junit.Test)

Example 83 with DenseLocalOnHeapVector

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);
}
Also used : Path(java.nio.file.Path) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 84 with DenseLocalOnHeapVector

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;
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 85 with DenseLocalOnHeapVector

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();
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Aggregations

DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)98 Vector (org.apache.ignite.ml.math.Vector)49 Test (org.junit.Test)44 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)26 Random (java.util.Random)18 HashMap (java.util.HashMap)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 Matrix (org.apache.ignite.ml.math.Matrix)12 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)11 IgniteCache (org.apache.ignite.IgniteCache)8 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)8 Arrays (java.util.Arrays)7 Collections (java.util.Collections)6 List (java.util.List)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6 Ignite (org.apache.ignite.Ignite)6 IgniteUtils (org.apache.ignite.internal.util.IgniteUtils)6 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)6