in modules/ml-ext/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/LUDecomposition.java [84:176]
public LUDecomposition(Matrix matrix, double singularityThreshold) {
assert matrix != null;
int rows = matrix.rowSize();
int cols = matrix.columnSize();
if (rows != cols)
throw new CardinalityException(rows, cols);
this.matrix = matrix;
lu = copy(matrix);
pivot = likeVector(matrix);
for (int i = 0; i < pivot.size(); i++)
pivot.setX(i, i);
even = true;
singular = false;
cachedL = null;
cachedU = null;
cachedP = null;
for (int col = 0; col < cols; col++) {
//upper
for (int row = 0; row < col; row++) {
Vector luRow = lu.viewRow(row);
double sum = luRow.get(col);
for (int i = 0; i < row; i++)
sum -= luRow.getX(i) * lu.getX(i, col);
luRow.setX(col, sum);
}
// permutation row
int max = col;
double largest = Double.NEGATIVE_INFINITY;
// lower
for (int row = col; row < rows; row++) {
Vector luRow = lu.viewRow(row);
double sum = luRow.getX(col);
for (int i = 0; i < col; i++)
sum -= luRow.getX(i) * lu.getX(i, col);
luRow.setX(col, sum);
if (Math.abs(sum) > largest) {
largest = Math.abs(sum);
max = row;
}
}
// Singularity check
if (Math.abs(lu.getX(max, col)) < singularityThreshold) {
singular = true;
return;
}
// Pivot if necessary
if (max != col) {
double tmp;
Vector luMax = lu.viewRow(max);
Vector luCol = lu.viewRow(col);
for (int i = 0; i < cols; i++) {
tmp = luMax.getX(i);
luMax.setX(i, luCol.getX(i));
luCol.setX(i, tmp);
}
int temp = (int)pivot.getX(max);
pivot.setX(max, pivot.getX(col));
pivot.setX(col, temp);
even = !even;
}
// Divide the lower elements by the "winning" diagonal elt.
final double luDiag = lu.getX(col, col);
for (int row = col + 1; row < cols; row++) {
double val = lu.getX(row, col) / luDiag;
lu.setX(row, col, val);
}
}
}