operator - method

Algebraic operator -(
  1. Algebraic other
)

The difference of two polynomials is performed by subtracting the corresponding coefficients.

The degrees of the two polynomials don't need to be the same. For example, you could subtract a Quadratic and a Quartic.

Implementation

Algebraic operator -(Algebraic other) {
  final maxDegree = max<int>(coefficients.length, other.coefficients.length);
  final newCoefficients = <Complex>[];

  // The difference of two polynomials is the difference of the coefficients
  // with the same degree
  for (var degree = maxDegree; degree >= 0; --degree) {
    final thisCoefficient = coefficient(degree) ?? const Complex.zero();
    final otherCoefficient =
        other.coefficient(degree) ?? const Complex.zero();
    final diff = thisCoefficient - otherCoefficient;

    if (!diff.isZero) {
      newCoefficients.add(diff);
    }
  }

  // Returning a new instance
  return Algebraic.from(newCoefficients);
}