backSubstitution static method

List<double> backSubstitution(
  1. List<List<double>> source,
  2. List<double> vector
)

Back substitution is an iterative process that solves equation matrices in the form Ux = b, where U is an upper triangular matrix.

In this case, source represents U and vector represents b.

Implementation

static List<double> backSubstitution(
  List<List<double>> source,
  List<double> vector,
) {
  final size = vector.length;
  final solutions = List<double>.generate(size, (_) => 0, growable: false);

  for (var i = size - 1; i >= 0; --i) {
    solutions[i] = vector[i];
    for (var j = i + 1; j < size; ++j) {
      solutions[i] = solutions[i] - source[i][j] * solutions[j];
    }
    solutions[i] = solutions[i] / source[i][i];
  }

  return solutions;
}