use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector 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());
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 SparseVector)
y.assign(fY);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.
the class VectorUtils method of.
/**
* Creates vector based on array of Doubles. If array contains null-elements then
* method returns sparse local on head vector. In other case method returns
* dense local on heap vector.
*
* @param values Values.
*/
public static Vector of(Double[] values) {
A.notNull(values, "values");
Vector answer;
if (Arrays.stream(values).anyMatch(Objects::isNull))
answer = new SparseVector(values.length);
else
answer = new DenseVector(values.length);
for (int i = 0; i < values.length; i++) if (values[i] != null)
answer.set(i, values[i]);
return answer;
}
use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.
the class VectorUtils method of.
/**
* Creates named vector based on map of keys and values.
*
* @param values Values.
* @return Named vector.
*/
public static NamedVector of(Map<String, Double> values) {
SparseVector vector = new SparseVector(values.size());
for (int i = 0; i < values.size(); i++) vector.set(i, Double.NaN);
Map<String, Integer> dict = new HashMap<>();
int idx = 0;
for (Map.Entry<String, Double> e : values.entrySet()) {
dict.put(e.getKey(), idx);
vector.set(idx, e.getValue());
idx++;
}
return new DelegatingNamedVector(vector, dict);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.
the class BlasTest method testAxpySparseArray.
/**
* Test 'axpy' operation for sparse vector and array-based vector.
*/
@Test
public void testAxpySparseArray() {
DenseVector y = new DenseVector(new double[] { 1.0, 2.0 });
double a = 2.0;
SparseVector x = sparseFromArray(new double[] { 1.0, 2.0 });
SparseVector exp = (SparseVector) x.times(a).plus(y);
Blas.axpy(a, x, y);
Assert.assertTrue(elementsEqual(exp, y));
}
use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector 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);
}
Aggregations