Search in sources :

Example 36 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class BlasTest method testGemvDenseDenseDense.

/**
 * Tests 'gemv' operation for dense matrix A, dense vector x and dense vector y.
 */
@Test
public void testGemvDenseDenseDense() {
    // y := alpha * A * x + beta * y
    double alpha = 3.0;
    DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } }, 2);
    DenseLocalOnHeapVector x = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
    double beta = 2.0;
    DenseLocalOnHeapVector y = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
    DenseLocalOnHeapVector exp = (DenseLocalOnHeapVector) y.times(beta).plus(a.times(x).times(alpha));
    Blas.gemv(alpha, a, x, beta, y);
    Assert.assertEquals(exp, y);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 37 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class BlasTest method testDot.

/**
 * Test 'dot' operation.
 */
@Test
public void testDot() {
    DenseLocalOnHeapVector v1 = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
    DenseLocalOnHeapVector v2 = new DenseLocalOnHeapVector(new double[] { 2.0, 2.0 });
    Assert.assertEquals(Blas.dot(v1, v2), v1.dot(v2), 0.0);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 38 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class DiagonalMatrixTest method testSetGet.

/**
 */
@Test
public void testSetGet() {
    verifyDiagonal(testMatrix);
    final int size = MathTestConstants.STORAGE_SIZE;
    for (Matrix m : new Matrix[] { new DenseLocalOnHeapMatrix(size + 1, size), new DenseLocalOnHeapMatrix(size, size + 1) }) {
        fillMatrix(m);
        verifyDiagonal(new DiagonalMatrix(m));
    }
    final double[] data = new double[size];
    for (int i = 0; i < size; i++) data[i] = 1 + i;
    final Matrix m = new DiagonalMatrix(new DenseLocalOnHeapVector(data));
    assertEquals("Rows in matrix constructed from vector", size, m.rowSize());
    assertEquals("Cols in matrix constructed from vector", size, m.columnSize());
    for (int i = 0; i < size; i++) assertEquals(UNEXPECTED_VALUE + " at vector index " + i, data[i], m.get(i, i), 0d);
    verifyDiagonal(m);
    final Matrix m1 = new DiagonalMatrix(data);
    assertEquals("Rows in matrix constructed from array", size, m1.rowSize());
    assertEquals("Cols in matrix constructed from array", size, m1.columnSize());
    for (int i = 0; i < size; i++) assertEquals(UNEXPECTED_VALUE + " at array index " + i, data[i], m1.get(i, i), 0d);
    verifyDiagonal(m1);
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest) Test(org.junit.Test)

Example 39 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class MatrixImplementationsTest method testTimesVector.

/**
 */
@Test
public void testTimesVector() {
    consumeSampleMatrix((m, desc) -> {
        if (ignore(m.getClass()))
            return;
        if (m instanceof DenseLocalOffHeapMatrix)
            // TODO: IGNITE-5535, waiting offheap support.
            return;
        double[][] data = fillAndReturn(m);
        double[] arr = fillArray(m.columnSize());
        Vector times = m.times(new DenseLocalOnHeapVector(arr));
        assertEquals("Unexpected vector size for " + desc, times.size(), m.rowSize());
        for (int i = 0; i < m.rowSize(); i++) {
            double exp = 0.0;
            for (int j = 0; j < m.columnSize(); j++) exp += arr[j] * data[i][j];
            assertEquals("Unexpected value for " + desc + " at " + i, times.get(i), exp, DEFAULT_DELTA);
        }
        testInvalidCardinality(() -> m.times(new DenseLocalOnHeapVector(m.columnSize() + 1)), desc);
    });
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) RandomVector(org.apache.ignite.ml.math.impls.vector.RandomVector) DenseLocalOffHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector) Vector(org.apache.ignite.ml.math.Vector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 40 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class BlasTest method testGemvSparseDenseDense.

/**
 * Tests 'gemv' operation for dense matrix A, dense vector x and dense vector y.
 */
@Test
public void testGemvSparseDenseDense() {
    // y := alpha * A * x + beta * y
    double alpha = 3.0;
    SparseLocalOnHeapMatrix a = (SparseLocalOnHeapMatrix) new SparseLocalOnHeapMatrix(2, 2).assign(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } });
    DenseLocalOnHeapVector x = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
    double beta = 2.0;
    DenseLocalOnHeapVector y = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
    DenseLocalOnHeapVector exp = (DenseLocalOnHeapVector) y.times(beta).plus(a.times(x).times(alpha));
    Blas.gemv(alpha, a, x, beta, y);
    Assert.assertEquals(exp, y);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SparseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix) Test(org.junit.Test)

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