use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector 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, DenseMatrix 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 DenseVector)
syr(alpha, x, a);
else if (x instanceof SparseVector)
syr(alpha, x, a);
else
throw new IllegalArgumentException("Operation 'syr' does not support vector [class=" + x.getClass().getName() + "].");
}
use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector 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);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.
the class BlasTest method testGemvSparseSparseDense.
/**
* Tests 'gemv' operation for sparse matrix A, sparse vector x and dense vector y.
*/
@Test
public void testGemvSparseSparseDense() {
// y := alpha * A * x + beta * y
double alpha = 3.0;
DenseMatrix a = new DenseMatrix(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } }, 2);
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);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.
the class BlasTest method testScalSparse.
/**
* Test 'scal' operation for sparse matrix.
*/
@Test
public void testScalSparse() {
double[] data = new double[] { 1.0, 1.0 };
double alpha = 2.0;
SparseVector v = sparseFromArray(data);
Vector exp = sparseFromArray(data).times(alpha);
Blas.scal(alpha, v);
Assert.assertEquals(v, exp);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.
the class Deltas method getStateVector.
/**
* @param mdl Model.
* @return vector of model weights with intercept.
*/
private Vector getStateVector(SVMLinearClassificationModel mdl) {
double intercept = mdl.intercept();
Vector weights = mdl.weights();
int stateVectorSize = weights.size() + 1;
Vector res = weights.isDense() ? new DenseVector(stateVectorSize) : new SparseVector(stateVectorSize);
res.set(0, intercept);
weights.nonZeroes().forEach(ith -> res.set(ith.index(), ith.get()));
return res;
}
Aggregations