Search in sources :

Example 1 with SparseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix 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;
    SparseMatrix a = (SparseMatrix) new SparseMatrix(2, 2).assign(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } });
    DenseVector x = new DenseVector(new double[] { 1.0, 2.0 });
    double beta = 2.0;
    DenseVector y = new DenseVector(new double[] { 3.0, 4.0 });
    DenseVector exp = (DenseVector) y.times(beta).plus(a.times(x).times(alpha));
    Blas.gemv(alpha, a, x, beta, y);
    Assert.assertEquals(exp, y);
}
Also used : SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Example 2 with SparseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix in project ignite by apache.

the class BlasTest method testGemvDenseSparseDense.

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

Example 3 with SparseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix in project ignite by apache.

the class SparseMatrixConstructorTest method basicTest.

/**
 */
@Test
public void basicTest() {
    assertEquals("Expected number of rows.", 1, new SparseMatrix(1, 2).rowSize());
    assertEquals("Expected number of cols, int parameters.", 1, new SparseMatrix(2, 1).columnSize());
    SparseMatrix m = new SparseMatrix(1, 1);
    // noinspection EqualsWithItself
    assertTrue("Matrix is expected to be equal to self.", m.equals(m));
    assertFalse("Matrix is expected to be not equal to null.", m.equals(null));
}
Also used : SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix) Test(org.junit.Test)

Example 4 with SparseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix in project ignite by apache.

the class BlasTest method testGemmSparseDenseDense.

/**
 * Tests 'gemm' operation for sparse matrix A, dense matrix B and dense matrix C.
 */
@Test
public void testGemmSparseDenseDense() {
    // C := alpha * A * B + beta * C
    double alpha = 1.0;
    SparseMatrix a = (SparseMatrix) new SparseMatrix(2, 2).assign(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } });
    DenseMatrix b = new DenseMatrix(new double[][] { { 1.0, 0.3 }, { 0.0, 1.0 } });
    double beta = 0.0;
    DenseMatrix c = new DenseMatrix(new double[][] { { 1.0, 2.0 }, { 2.0, 3.0 } });
    // .times(alpha).plus(c.times(beta));
    Matrix exp = a.times(b);
    Blas.gemm(alpha, a, b, beta, c);
    Assert.assertTrue(Arrays.equals(exp.getStorage().data(), c.getStorage().data()));
}
Also used : SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix) Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Example 5 with SparseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix in project ignite by apache.

the class SparseMatrixConstructorTest method invalidArgsTest.

/**
 */
@Test
public void invalidArgsTest() {
    DenseMatrixConstructorTest.verifyAssertionError(() -> new SparseMatrix(0, 1), "invalid row parameter");
    DenseMatrixConstructorTest.verifyAssertionError(() -> new SparseMatrix(1, 0), "invalid col parameter");
}
Also used : SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix) Test(org.junit.Test)

Aggregations

SparseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix)5 Test (org.junit.Test)5 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)2 Matrix (org.apache.ignite.ml.math.primitives.matrix.Matrix)1 DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)1 SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)1