operator + method

Algebraic operator +(
  1. Algebraic other
)

The sum of two polynomials is performed by adding the corresponding coefficients.

The degrees of the two polynomials don't need to be the same. For example, you could sum a Cubic with a Linear.

Implementation

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

  // The sum of two polynomials is the sum 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 sum = thisCoefficient + otherCoefficient;

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

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