solve method

  1. @override
List<double> solve()
override

Solves the Ax = b equation and returns the x vector.

Implementation

@override
List<double> solve() {
  // Exit conditions for the method.
  var k = 0;
  var diff = precision + 1;

  // Support lists.
  final size = knownValues.length;
  final solutions = List<double>.from(x0);

  // Jacobi
  while ((diff >= precision) && (k < maxSteps)) {
    final oldSolutions = List<double>.from(solutions);

    for (var i = 0; i < size; ++i) {
      // Initial value of the solution
      solutions[i] = knownValues[i];

      for (var j = 0; j < size; ++j) {
        // Skip the diagonal
        if (i == j) {
          continue;
        }

        solutions[i] = solutions[i] - matrix(i, j) * oldSolutions[j];
      }

      // New "refined" value of the solution
      solutions[i] = solutions[i] / matrix(i, i);
    }

    ++k;
    diff = _euclideanNorm(oldSolutions, solutions);
  }

  return solutions;
}