// Function solves Ax = b for A as a lower triangular matrix public static SuperMatrix SubsForth(SuperMatrix A, SuperMatrix b) { if (A.L == null) A.MakeLU(); int n = A.rows; SuperMatrix x = new SuperMatrix(n, 1); for (int i = 0; i < n; i++) { x[i, 0] = b[i, 0]; for (int j = 0; j < i; j++) x[i, 0] -= A[i, j] * x[j, 0]; x[i, 0] = x[i, 0] / A[i, i]; } return x; }
// Function solves Ax = b for A as an upper triangular matrix public static SuperMatrix SubsBack(SuperMatrix A, SuperMatrix b) { if (A.L == null) A.MakeLU(); int n = A.rows; SuperMatrix x = new SuperMatrix(n, 1); for (int i = n - 1; i > -1; i--) { x[i, 0] = b[i, 0]; for (int j = n - 1; j > i; j--) x[i, 0] -= A[i, j] * x[j, 0]; x[i, 0] = x[i, 0] / A[i, i]; } return x; }