use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector 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.DenseVector in project ignite by apache.
the class TracerTest method testWriteVectorToCSVFile.
/**
*/
@Test
public void testWriteVectorToCSVFile() throws IOException {
DenseVector vector = new DenseVector(MathTestConstants.STORAGE_SIZE);
for (int i = 0; i < vector.size(); i++) vector.set(i, Math.random());
Path file = createTempFile("vector", ".csv");
Tracer.saveAsCsv(vector, DEFAULT_FORMAT, file.toString());
System.out.println("Vector exported: " + file.getFileName());
List<String> strings = Files.readAllLines(file);
Optional<String> reduce = strings.stream().reduce((s1, s2) -> s1 + s2);
String[] csvVals = reduce.orElse("").split(",");
for (int i = 0; i < vector.size(); i++) {
Double csvVal = Double.valueOf(csvVals[i]);
assertEquals("Unexpected value.", csvVal, vector.get(i), DEFAULT_DELTA);
}
Files.deleteIfExists(file);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class TracerTest method makeRandomVector.
/**
* @param size Vector size.
*/
private Vector makeRandomVector(int size) {
DenseVector vec = new DenseVector(size);
vec.assign((idx) -> Math.random());
return vec;
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class CosineSimilarityTest method cosineSimilarityDistance.
/**
*/
@Test
public void cosineSimilarityDistance() {
double expRes = 0.9449111825230682d;
DenseVector a = new DenseVector(new double[] { 1, 2, 3 });
double[] b = { 1, 1, 4 };
DistanceMeasure distanceMeasure = new CosineSimilarity();
assertEquals(expRes, distanceMeasure.compute(a, b), PRECISION);
assertEquals(expRes, distanceMeasure.compute(a, new DenseVector(b)), PRECISION);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class BlasTest method testAxpyArrayArray.
/**
* Test 'axpy' operation for two array-based vectors.
*/
@Test
public void testAxpyArrayArray() {
Vector y = new DenseVector(new double[] { 1.0, 2.0 });
double a = 2.0;
Vector x = new DenseVector(new double[] { 1.0, 2.0 });
Vector exp = x.times(a).plus(y);
Blas.axpy(a, x, y);
Assert.assertEquals(y, exp);
}
Aggregations