divide method

List<GenericGFPoly> divide(
  1. GenericGFPoly other
)

Implementation

List<GenericGFPoly> divide(GenericGFPoly other) {
  if (_field != other._field) {
    throw ArgumentError('GenericGFPolys do not have same GenericGF field');
  }
  if (other.isZero) {
    throw ArgumentError('Divide by 0');
  }

  GenericGFPoly quotient = _field.zero;
  GenericGFPoly remainder = this;

  final denominatorLeadingTerm = other.getCoefficient(other.degree);
  final inverseDenominatorLeadingTerm =
      _field.inverse(denominatorLeadingTerm);

  while (remainder.degree >= other.degree && !remainder.isZero) {
    final degreeDifference = remainder.degree - other.degree;
    final scale = _field.multiply(
      remainder.getCoefficient(remainder.degree),
      inverseDenominatorLeadingTerm,
    );
    final term = other.multiplyByMonomial(degreeDifference, scale);
    final iterationQuotient = _field.buildMonomial(degreeDifference, scale);
    quotient = quotient.addOrSubtract(iterationQuotient);
    remainder = remainder.addOrSubtract(term);
  }

  return [quotient, remainder];
}