use of org.apache.ignite.ml.math.Matrix 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;
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class PivotedMatrixViewConstructorTest method basicTest.
/** */
@Test
public void basicTest() {
Matrix m = new DenseLocalOnHeapMatrix(2, 2);
int[] pivot = new int[] { 0, 1 };
PivotedMatrixView view = new PivotedMatrixView(m, pivot);
assertEquals("Rows in view.", m.rowSize(), view.rowSize());
assertEquals("Cols in view.", m.columnSize(), view.columnSize());
assertTrue("Row pivot array in view.", Arrays.equals(pivot, view.rowPivot()));
assertTrue("Col pivot array in view.", Arrays.equals(pivot, view.columnPivot()));
Assert.assertEquals("Base matrix in view.", m, view.getBaseMatrix());
assertEquals("Row pivot value in view.", 0, view.rowPivot(0));
assertEquals("Col pivot value in view.", 0, view.columnPivot(0));
assertEquals("Row unpivot value in view.", 0, view.rowUnpivot(0));
assertEquals("Col unpivot value in view.", 0, view.columnUnpivot(0));
Matrix swap = view.swap(1, 1);
for (int row = 0; row < view.rowSize(); row++) for (int col = 0; col < view.columnSize(); col++) assertEquals("Unexpected swap value set at (" + row + "," + col + ").", view.get(row, col), swap.get(row, col), 0d);
//noinspection EqualsWithItself
assertTrue("View is expected to be equal to self.", view.equals(view));
//noinspection ObjectEqualsNull
assertFalse("View is expected to be not equal to null.", view.equals(null));
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class PivotedMatrixViewConstructorTest method pivotTest.
/** */
private void pivotTest(Matrix parent, int[] pivot) {
for (int row = 0; row < parent.rowSize(); row++) for (int col = 0; col < parent.columnSize(); col++) parent.set(row, col, row * parent.columnSize() + col + 1);
Matrix view = new PivotedMatrixView(parent, pivot);
int rows = parent.rowSize();
int cols = parent.columnSize();
assertEquals("Rows in view.", rows, view.rowSize());
assertEquals("Cols in view.", cols, view.columnSize());
for (int row = 0; row < rows; row++) for (int col = 0; col < cols; col++) assertEquals("Unexpected value at " + row + "x" + col, parent.get(pivot[row], pivot[col]), view.get(row, col), 0d);
int min = rows < cols ? rows : cols;
for (int idx = 0; idx < min; idx++) view.set(idx, idx, 0d);
for (int idx = 0; idx < min; idx++) assertEquals("Unexpected value set at " + idx, 0d, parent.get(pivot[idx], pivot[idx]), 0d);
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class MatrixImplementationsTest method testPlus.
/** */
@Test
public void testPlus() {
consumeSampleMatrix((m, desc) -> {
if (readOnly(m))
return;
double[][] data = fillAndReturn(m);
double plusVal = Math.random();
Matrix plus = m.plus(plusVal);
for (int i = 0; i < m.rowSize(); i++) for (int j = 0; j < m.columnSize(); j++) assertEquals("Unexpected value for " + desc + " at (" + i + "," + j + ")", data[i][j] + plusVal, plus.get(i, j), 0d);
});
}
use of org.apache.ignite.ml.math.Matrix in project ignite by apache.
the class MatrixImplementationFixtures method consumeSampleMatrix.
/** */
void consumeSampleMatrix(BiConsumer<Matrix, String> consumer) {
for (Supplier<Iterable<Matrix>> fixtureSupplier : suppliers) {
final Iterable<Matrix> fixture = fixtureSupplier.get();
for (Matrix matrix : fixture) {
consumer.accept(matrix, fixture.toString());
matrix.destroy();
}
}
}
Aggregations