operator + method
The addition of two polynomials is performed by adding the corresponding coefficients. The degrees of the two polynomials don't need to be the same so you can sum a Cubic with a Linear for example.
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);
}