Search in sources :

Example 6 with SparseLocalVector

use of org.apache.ignite.ml.math.impls.vector.SparseLocalVector 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;
    SparseLocalVector v = sparseFromArray(new double[] { 1.0, 2.0 });
    DenseLocalOnHeapVector u = new DenseLocalOnHeapVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
    DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 3.0, 0.0 }, { 13.0, 20.0 } }, StorageConstants.COLUMN_STORAGE_MODE);
    DenseLocalOnHeapMatrix exp = (DenseLocalOnHeapMatrix) new DenseLocalOnHeapMatrix(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);
    DenseLocalOnHeapMatrix mu = fromVector(u, a.rowSize(), StorageConstants.COLUMN_STORAGE_MODE, (i, j) -> i >= j);
    Assert.assertEquals(exp, 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 7 with SparseLocalVector

use of org.apache.ignite.ml.math.impls.vector.SparseLocalVector 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 8 with SparseLocalVector

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

the class Blas method gemv.

/**
 * y := alpha * A * x + beta * y.
 *
 * @param alpha Alpha.
 * @param a Matrix a.
 * @param x Vector x.
 * @param beta Beta.
 * @param y Vector y.
 */
public static void gemv(double alpha, Matrix a, Vector x, double beta, Vector y) {
    checkCardinality(a, x);
    if (a.rowSize() != y.size())
        throw new CardinalityException(a.columnSize(), y.size());
    checkMatrixType(a, "gemv");
    checkVectorType(x, "gemv");
    checkVectorType(y, "gemv");
    if (alpha == 0.0 && beta == 1.0)
        return;
    if (alpha == 0.0) {
        scal(y, beta);
        return;
    }
    double[] fA = a.getStorage().data();
    double[] fX = x.getStorage().data();
    double[] fY = y.getStorage().data();
    nativeBlas.dgemv("N", a.rowSize(), a.columnSize(), alpha, fA, a.rowSize(), fX, 1, beta, fY, 1);
    if (y instanceof SparseLocalVector)
        y.assign(fY);
}
Also used : CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector)

Example 9 with SparseLocalVector

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

the class Blas method syr.

/**
 * A := alpha * x * x^T + A.
 *
 * @param alpha a real scalar that will be multiplied to x * x^T^.
 * @param x the vector x that contains the n elements.
 * @param a the symmetric matrix A. Size of n x n.
 */
void syr(Double alpha, Vector x, DenseLocalOnHeapMatrix a) {
    int mA = a.rowSize();
    int nA = a.columnSize();
    if (mA != nA)
        throw new NonSquareMatrixException(mA, nA);
    if (mA != x.size())
        throw new CardinalityException(x.size(), mA);
    // TODO: IGNITE-5535, Process DenseLocalOffHeapVector
    if (x instanceof DenseLocalOnHeapVector)
        syr(alpha, x, a);
    else if (x instanceof SparseLocalVector)
        syr(alpha, x, a);
    else
        throw new IllegalArgumentException("Operation 'syr' does not support vector [class=" + x.getClass().getName() + "].");
}
Also used : NonSquareMatrixException(org.apache.ignite.ml.math.exceptions.NonSquareMatrixException) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)

Aggregations

SparseLocalVector (org.apache.ignite.ml.math.impls.vector.SparseLocalVector)9 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)7 Test (org.junit.Test)6 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)3 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)2 Vector (org.apache.ignite.ml.math.Vector)1 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)1 NonSquareMatrixException (org.apache.ignite.ml.math.exceptions.NonSquareMatrixException)1 SparseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix)1