/// <summary> /// Finds the Greatest Common Divisor between this and another polynomial /// </summary> /// <param name="otherPolynomial">The other polynomial with which the GCD is to be calculated</param> /// <returns>The Greatest Common Divisor</returns> public OneVariablePolynomial GCD(OneVariablePolynomial otherPolynomial) { OneVariablePolynomial h = this; OneVariablePolynomial s = otherPolynomial; OneVariablePolynomial rem = new OneVariablePolynomial(); while (!s.IsZero()) { rem = h.Divide(s)["remainder"]; h = s; s = rem; } return(rem); }
/// <summary> /// Divides the polynomial with the divisor. /// </summary> /// <param name="divisor">The divisor. Denoted by g in the CLO book.</param> /// <returns>The quotient and remainder in the form of a tuple.</returns> public Dictionary <string, OneVariablePolynomial> Divide(OneVariablePolynomial divisor) { OneVariablePolynomial quotient = new OneVariablePolynomial(); OneVariablePolynomial remainder = this; while (!remainder.IsZero() && divisor.LeadingTerm().degree <= remainder.LeadingTerm().degree) { OneVariableMonomial update = remainder.LeadingTerm().Divide(divisor.LeadingTerm()); quotient = quotient.Add(new OneVariablePolynomial(update)); remainder = remainder.Add(divisor.Multiply(update), false); // A false addition is basically a subtraction. } return(new Dictionary <string, OneVariablePolynomial>() { { "quotient", quotient }, { "remainder", remainder } }); }