use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix 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()));
}
use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix 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;
SparseVector v = new SparseVector(2);
v.set(0, 1);
DenseVector u = new DenseVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
// m is alpha * v * v^t
DenseMatrix m = (DenseMatrix) new DenseMatrix(new double[][] { { 1.0, 0.0 }, { 0.0, 0.0 } }, StorageConstants.COLUMN_STORAGE_MODE).times(alpha);
DenseMatrix a = new DenseMatrix(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);
DenseMatrix 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.primitives.matrix.impl.DenseMatrix in project ignite by apache.
the class BlasTest method testSprSparseDense1.
/**
* Test 'spr' operation for sparse vector v (sparse in representation, dense in fact) and dense matrix A.
*/
@Test
public void testSprSparseDense1() {
double alpha = 3.0;
SparseVector v = sparseFromArray(new double[] { 1.0, 2.0 });
DenseVector u = new DenseVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
DenseMatrix a = new DenseMatrix(new double[][] { { 3.0, 0.0 }, { 13.0, 20.0 } }, StorageConstants.COLUMN_STORAGE_MODE);
DenseMatrix exp = (DenseMatrix) new DenseMatrix(new double[][] { { 1.0, 0.0 }, { 2.0, 4.0 } }, StorageConstants.COLUMN_STORAGE_MODE).times(alpha).plus(a);
// m := alpha * v * v.t + A
Blas.spr(alpha, v, u);
DenseMatrix mu = fromVector(u, a.rowSize(), StorageConstants.COLUMN_STORAGE_MODE, (i, j) -> i >= j);
Assert.assertEquals(exp, mu);
}
use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix in project ignite by apache.
the class LUDecompositionTest method setUp.
/**
*/
@Before
public void setUp() {
double[][] rawMatrix = new double[][] { { 2.0d, 1.0d, 1.0d, 0.0d }, { 4.0d, 3.0d, 3.0d, 1.0d }, { 8.0d, 7.0d, 9.0d, 5.0d }, { 6.0d, 7.0d, 9.0d, 8.0d } };
double[][] rawL = { { 1.0d, 0.0d, 0.0d, 0.0d }, { 3.0d / 4.0d, 1.0d, 0.0d, 0.0d }, { 1.0d / 2.0d, -2.0d / 7.0d, 1.0d, 0.0d }, { 1.0d / 4.0d, -3.0d / 7.0d, 1.0d / 3.0d, 1.0d } };
double[][] rawU = { { 8.0d, 7.0d, 9.0d, 5.0d }, { 0.0d, 7.0d / 4.0d, 9.0d / 4.0d, 17.0d / 4.0d }, { 0.0d, 0.0d, -6.0d / 7.0d, -2.0d / 7.0d }, { 0.0d, 0.0d, 0.0d, 2.0d / 3.0d } };
double[][] rawP = new double[][] { { 0, 0, 1.0d, 0 }, { 0, 0, 0, 1.0d }, { 0, 1.0d, 0, 0 }, { 1.0d, 0, 0, 0 } };
rawPivot = new int[] { 3, 4, 2, 1 };
testMatrix = new DenseMatrix(rawMatrix);
testL = new DenseMatrix(rawL);
testU = new DenseMatrix(rawU);
testP = new DenseMatrix(rawP);
}
Aggregations