solutions method

  1. @override
List<Complex> solutions()
override

Calculates the roots (the solutions) of the equation.

Implementation

@override
List<Complex> solutions() {
  // List that will contain the solutions
  final roots = <Complex>[];

  // The algorithm requires the coefficients to start from the one with the
  // lowest degree.
  final polyP = coefficients.reversed.toList();
  var polyQ = polyP;

  // Finding all the roots of the polynomial
  while (polyQ.length > 2) {
    var z = initialGuess;

    // Laguerre's method
    z = _laguerre(polyQ, z);
    z = _laguerre(polyP, z);
    polyQ = _horner(polyQ, z).polynomial;

    // Adding the new root to the results list
    roots.add(z);
  }

  // Safety check to avoid 'RangeError'
  if (polyQ.length >= 2) {
    roots.add(-polyQ[0] / polyQ[1]);
  }

  return roots;
}