in modules/ml-ext/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/LUDecomposition.java [331:382]
public Matrix solve(Matrix b) {
final int m = pivot.size();
if (b.rowSize() != m)
throw new CardinalityException(b.rowSize(), m);
if (singular)
throw new SingularMatrixException();
final int nColB = b.columnSize();
// Apply permutations to b
final double[][] bp = new double[m][nColB];
for (int row = 0; row < m; row++) {
final double[] bpRow = bp[row];
final int pRow = (int)pivot.get(row);
for (int col = 0; col < nColB; col++)
bpRow[col] = b.get(pRow, col);
}
// Solve LY = b
for (int col = 0; col < m; col++) {
final double[] bpCol = bp[col];
for (int i = col + 1; i < m; i++) {
final double[] bpI = bp[i];
final double luICol = lu.get(i, col);
for (int j = 0; j < nColB; j++)
bpI[j] -= bpCol[j] * luICol;
}
}
// Solve UX = Y
for (int col = m - 1; col >= 0; col--) {
final double[] bpCol = bp[col];
final double luDiag = lu.getX(col, col);
for (int j = 0; j < nColB; j++)
bpCol[j] /= luDiag;
for (int i = 0; i < col; i++) {
final double[] bpI = bp[i];
final double luICol = lu.get(i, col);
for (int j = 0; j < nColB; j++)
bpI[j] -= bpCol[j] * luICol;
}
}
return b.like(b.rowSize(), b.columnSize()).assign(bp);
}