mulPolynomial method

Polynomial<T> mulPolynomial(
  1. Polynomial<T> other, {
  2. DataType<T>? dataType,
  3. PolynomialFormat? format,
  4. bool? fftMultiply,
})

Multiplies this Polynomial with a Polynomial.

Implementation

Polynomial<T> mulPolynomial(
  Polynomial<T> other, {
  DataType<T>? dataType,
  PolynomialFormat? format,
  bool? fftMultiply,
}) {
  if (degree < 0 || other.degree < 0) {
    // One of the polynomials has zero coefficients.
    return createPolynomial<T>(this, 0, dataType, format);
  }
  final result =
      createPolynomial<T>(this, degree + other.degree, dataType, format);
  final add = result.dataType.field.add, mul = result.dataType.field.mul;
  if (degree == 0) {
    // First polynomial is constant.
    final factor = getUnchecked(0);
    for (var i = other.degree; i >= 0; i--) {
      result.setUnchecked(i, mul(factor, other.getUnchecked(i)));
    }
  } else if (other.degree == 0) {
    // Second polynomial is constant.
    final factor = other.getUnchecked(0);
    for (var i = degree; i >= 0; i--) {
      result.setUnchecked(i, mul(getUnchecked(i), factor));
    }
  } else if (fftMultiply == true ||
      (fftMultiply != false && degree * other.degree > 1600)) {
    // Perform fourier multiplication when this is a large polynomial, or
    // when the user desires to use it. Experimentally FFT multiplication
    // starts to become more performant if the multiplied degrees exceed 1600.
    _fftMulPolynomial(result, this, other);
  } else {
    // Churn through full multiplication.
    for (var a = degree; a >= 0; a--) {
      for (var b = other.degree; b >= 0; b--) {
        result.setUnchecked(
            a + b,
            add(result.getUnchecked(a + b),
                mul(getUnchecked(a), other.getUnchecked(b))));
      }
    }
  }
  return result;
}