use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class VectorCustomStorageExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
System.out.println();
System.out.println(">>> Vector custom storage API usage example started.");
System.out.println("\n>>> Creating perpendicular vectors.");
double[] data1 = new double[] { 1, 0, 3, 0, 5, 0 };
double[] data2 = new double[] { 0, 2, 0, 4, 0, 6 };
Vector v1 = new VectorCustomStorage(data1);
Vector v2 = new VectorCustomStorage(data2);
System.out.println(">>> First vector: " + Arrays.toString(data1));
System.out.println(">>> Second vector: " + Arrays.toString(data2));
double dotProduct = v1.dot(v2);
boolean dotProductIsAsExp = dotProduct == 0;
System.out.println("\n>>> Dot product of vectors: [" + dotProduct + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
Vector hypotenuse = v1.plus(v2);
System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
double lenSquared1 = v1.getLengthSquared();
double lenSquared2 = v2.getLengthSquared();
double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse + "], equals sum of squared lengths of two original vectors as expected: [" + lenSquaredHypotenuseIsAsExp + "].");
System.out.println("\n>>> Vector custom storage API usage example completed.");
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class MatrixImplementationsTest method testFoldCol.
/** */
@Test
public void testFoldCol() {
consumeSampleMatrix((m, desc) -> {
if (ignore(m.getClass()))
return;
fillMatrix(m);
Vector foldCols = m.foldColumns(Vector::sum);
for (int j = 0; j < m.columnSize(); j++) {
Double locSum = 0d;
for (int i = 0; i < m.rowSize(); i++) locSum += m.get(i, j);
assertEquals("Unexpected value for " + desc + " at " + j, foldCols.get(j), locSum, 0d);
}
});
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class MatrixImplementationsTest method testFoldRow.
/** */
@Test
public void testFoldRow() {
consumeSampleMatrix((m, desc) -> {
if (ignore(m.getClass()))
return;
fillMatrix(m);
Vector foldRows = m.foldRows(Vector::sum);
for (int i = 0; i < m.rowSize(); i++) {
Double locSum = 0d;
for (int j = 0; j < m.columnSize(); j++) locSum += m.get(i, j);
assertEquals("Unexpected value for " + desc + " at " + i, foldRows.get(i), locSum, 0d);
}
});
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class LUDecompositionTest method solveVec.
/** */
@Test
public void solveVec() throws Exception {
Vector sol = new LUDecomposition(new PivotedMatrixView(testMatrix)).solve(new DenseLocalOnHeapVector(testMatrix.rowSize()));
assertEquals("Wrong solution vector size.", testMatrix.rowSize(), sol.size());
for (int i = 0; i < sol.size(); i++) assertEquals("Unexpected value at index " + i, 0d, sol.getX(i), 0.0000001d);
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class EigenDecompositionTest method assertDiagonalConsistsOfEigenvalues.
/** */
private void assertDiagonalConsistsOfEigenvalues(DenseLocalOnHeapMatrix m, Matrix d, Matrix v) {
int n = m.columnSize();
for (int i = 0; i < n; i++) {
Vector eigenVector = v.viewColumn(i);
double eigenVal = d.getX(i, i);
assertVectorsAreEqual(m.times(eigenVector), eigenVector.times(eigenVal));
}
}
Aggregations