solve method

Array2d solve(
  1. Array2d B
)

Solve A*X = B

  • B A Matrix with as many rows as A and any number of columns. so that LUX = B(piv,:)
  • FormatException Matrix row dimensions must agree or Matrix is singular.

Implementation

Array2d solve(Array2d B) {
  if (B.row != _m) {
    throw FormatException('Matrix row dimensions must agree.');
  }
  if (!isNonsingular()) {
    throw FormatException('Matrix is singular.');
  }

  // Copy right hand side with pivoting
  var nx = B.column;
  var X = matrixSubFromArray(B, _piv, 0, nx - 1);

  // Solve L*Y = B(piv,:)
  for (var k = 0; k < _n; k++) {
    for (var i = k + 1; i < _n; i++) {
      for (var j = 0; j < nx; j++) {
        X[i][j] -= X[k][j] * _LUMatrix[i][k];
      }
    }
  }
  // Solve U*X = Y;
  for (var k = _n - 1; k >= 0; k--) {
    for (var j = 0; j < nx; j++) {
      X[k][j] /= _LUMatrix[k][k];
    }
    for (var i = 0; i < k; i++) {
      for (var j = 0; j < nx; j++) {
        X[i][j] -= X[k][j] * _LUMatrix[i][k];
      }
    }
  }
  return X;
}