solve function Null safety

void solve(
  1. Matrix a,
  2. AMatrix b,
  3. AMatrix x
)

Solve the system of linear equations AX = B. This is a port of la4j's ForwardBackSubstitutionSolver.solve.

Implementation

void solve(Matrix a, AMatrix b, AMatrix x) {
  if (!a.isLU) {
    decomposeLU(a);
  }
  final int n = b.rows;
  if (x.rows != n || x.columns != b.columns || a.rows != n) {
    throw CalculatorError(11);
  }

  for (int rCol = 0; rCol < x.columns; rCol++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (a.getP(i, j)) {
          x.set(i, rCol, b.get(j, rCol));
          break;
        }
      }
    }

    for (int j = 0; j < n; j++) {
      for (int i = j + 1; i < n; i++) {
        x.setF(i, rCol, x.getF(i, rCol) - x.getF(j, rCol) * a.getF(i, j));
      }
    }

    for (int j = n - 1; j >= 0; j--) {
      x.setF(j, rCol, x.getF(j, rCol) / a.getF(j, j));
      for (int i = 0; i < j; i++) {
        x.setF(i, rCol, x.getF(i, rCol) - x.getF(j, rCol) * a.getF(i, j));
      }
    }
  }
  x.visit((r, c) {
    final v = x.get(r, c);
    if (v == Value.fInfinity || v == Value.fNegativeInfinity) {
      throw MatrixOverflow();
    }
  });
}