solveCubic function

List<double> solveCubic(
  1. double a,
  2. double b,
  3. double c,
  4. double d,
)

Solves cubic equation ax³ + bx² + cx + d == 0.

Depending on the coefficients, either 1 or 3 real solutions may be returned. In degenerate cases, when some of the roots of the equation coincide, we return all such roots without deduplication.

If coefficient a is equal to zero, then we solve the equation as a quadratic one (see solveQuadratic).

Implementation

List<double> solveCubic(double a, double b, double c, double d) {
  if (a == 0) {
    return solveQuadratic(b, c, d);
  }
  if (b == 0) {
    return _solveDepressedCubic(c / a, d / a);
  } else {
    final s = b / (3 * a);
    final p = c / a - 3 * s * s;
    final q = d / a - (p + s * s) * s;
    return _solveDepressedCubic(p, q).map((t) => t - s).toList();
  }
}