use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class VectorNormTest method getDistanceSquaredTest.
/** */
@Test
public void getDistanceSquaredTest() {
consumeSampleVectors((v, desc) -> {
// IMPL NOTE this initialises vector
new VectorImplementationsTest.ElementsChecker(v, desc);
final int size = v.size();
final Vector vOnHeap = new DenseLocalOnHeapVector(size);
final Vector vOffHeap = new DenseLocalOffHeapVector(size);
invertValues(v, vOnHeap);
invertValues(v, vOffHeap);
for (int idx = 0; idx < size; idx++) {
final double exp = v.get(idx);
final int idxMirror = size - 1 - idx;
assertTrue("On heap vector difference at " + desc + ", idx " + idx, exp - vOnHeap.get(idxMirror) == 0);
assertTrue("Off heap vector difference at " + desc + ", idx " + idx, exp - vOffHeap.get(idxMirror) == 0);
}
// IMPL NOTE this won't mutate vOnHeap
final double exp = vOnHeap.minus(v).getLengthSquared();
final VectorImplementationsTest.Metric metric = new VectorImplementationsTest.Metric(exp, v.getDistanceSquared(vOnHeap));
assertTrue("On heap vector not close enough at " + desc + ", " + metric, metric.closeEnough());
final VectorImplementationsTest.Metric metric1 = new VectorImplementationsTest.Metric(exp, v.getDistanceSquared(vOffHeap));
assertTrue("Off heap vector not close enough at " + desc + ", " + metric1, metric1.closeEnough());
});
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class DelegatingVectorConstructorTest method basicTest.
/** */
@Test
public void basicTest() {
final Vector parent = new DenseLocalOnHeapVector(new double[] { 0, 1 });
final Vector delegate = new DelegatingVector(parent);
final int size = parent.size();
assertEquals("Delegate size differs from expected.", size, delegate.size());
for (int idx = 0; idx < size; idx++) assertDelegate(parent, delegate, idx);
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class RandomVectorConstructorTest method basicTest.
/** */
@Test
public void basicTest() {
final int basicSize = 3;
Vector v1 = new RandomVector(basicSize);
//noinspection EqualsWithItself
assertTrue("Expect vector to be equal to self", v1.equals(v1));
//noinspection ObjectEqualsNull
assertFalse("Expect vector to be not equal to null", v1.equals(null));
assertEquals("Size differs from expected", basicSize, v1.size());
verifyValues(v1);
Vector v2 = new RandomVector(basicSize, true);
assertEquals("Size differs from expected", basicSize, v2.size());
verifyValues(v2);
Vector v3 = new RandomVector(basicSize, false);
assertEquals("Size differs from expected", basicSize, v3.size());
verifyValues(v3);
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class SingleElementVectorConstructorTest method basicTest.
/** */
private void basicTest(int size, int idx) {
final Double expVal = (double) (size - idx);
Vector v = new SingleElementVector(size, idx, expVal);
assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, expVal.equals(v.get(idx)));
final double delta = 1.0;
v.set(idx, expVal - delta);
assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, expVal.equals(v.get(idx) + delta));
final Double zero = 0.0;
for (int i = 0; i < size; i++) {
if (i == idx)
continue;
assertTrue("Expect zero at index " + i + " for size " + size, zero.equals(v.get(i)));
boolean eCaught = false;
try {
v.set(i, 1.0);
} catch (UnsupportedOperationException uoe) {
eCaught = true;
}
assertTrue("Expect " + java.lang.UnsupportedOperationException.class.getSimpleName() + " at index " + i + " for size " + size, eCaught);
}
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class EigenDecomposition method orthes.
/** */
private Matrix orthes(Matrix matrix) {
// Working storage for nonsymmetric algorithm.
Vector ort = likeVector(matrix);
Matrix hessenBerg = like(matrix).assign(matrix);
// This is derived from the Algol procedures orthes and ortran,
// by Martin and Wilkinson, Handbook for Auto. Comp.,
// Vol.ii-Linear Algebra, and the corresponding
// Fortran subroutines in EISPACK.
int low = 0;
int high = n - 1;
for (int m = low + 1; m <= high - 1; m++) {
// Scale column.
Vector hCol = hessenBerg.viewColumn(m - 1).viewPart(m, high - m + 1);
double scale = hCol.kNorm(1);
if (scale != 0.0) {
// Compute Householder transformation.
ort.viewPart(m, high - m + 1).map(hCol, Functions.plusMult(1 / scale));
double h = ort.viewPart(m, high - m + 1).getLengthSquared();
double g = Math.sqrt(h);
if (ort.getX(m) > 0)
g = -g;
h -= ort.getX(m) * g;
ort.setX(m, ort.getX(m) - g);
// Apply Householder similarity transformation
// H = (I-u*u'/h)*H*(I-u*u')/h)
Vector ortPiece = ort.viewPart(m, high - m + 1);
for (int j = m; j < n; j++) {
double f = ortPiece.dot(hessenBerg.viewColumn(j).viewPart(m, high - m + 1)) / h;
hessenBerg.viewColumn(j).viewPart(m, high - m + 1).map(ortPiece, Functions.plusMult(-f));
}
for (int i = 0; i <= high; i++) {
double f = ortPiece.dot(hessenBerg.viewRow(i).viewPart(m, high - m + 1)) / h;
hessenBerg.viewRow(i).viewPart(m, high - m + 1).map(ortPiece, Functions.plusMult(-f));
}
ort.setX(m, scale * ort.getX(m));
hessenBerg.setX(m, m - 1, scale * g);
}
}
// Accumulate transformations (Algol's ortran).
v.assign(0);
v.viewDiagonal().assign(1);
for (int m = high - 1; m >= low + 1; m--) {
if (hessenBerg.getX(m, m - 1) != 0.0) {
ort.viewPart(m + 1, high - m).assign(hessenBerg.viewColumn(m - 1).viewPart(m + 1, high - m));
for (int j = m; j <= high; j++) {
double g = ort.viewPart(m, high - m + 1).dot(v.viewColumn(j).viewPart(m, high - m + 1));
// Double division avoids possible underflow
g = g / ort.getX(m) / hessenBerg.getX(m, m - 1);
v.viewColumn(j).viewPart(m, high - m + 1).map(ort.viewPart(m, high - m + 1), Functions.plusMult(g));
}
}
}
return hessenBerg;
}
Aggregations