/// <summary> /// Divides the current polynomial by a list of divisor polynomials given by the argument. /// Based on the algorithm in section 2.3 of CLO book. /// </summary> /// <param name="divisorList">Comma saperated list with all the divisors.</param> /// <returns>The quotient and remainder in the form of a two element dictinoary.</returns> public List <Polynomial> DivideBy(params Polynomial[] divisorList) { Polynomial remainder = new Polynomial(); Polynomial p = this; List <Polynomial> quotients = new List <Polynomial>(); int s = divisorList.Length; foreach (Polynomial p1 in divisorList) { quotients.Add(new Polynomial()); } while (!p.IsZero) { int i = 1; bool divisionoccurred = false; while (i <= s && !divisionoccurred) { if (p.GetLeadingTerm().IsDividedBy(divisorList[i - 1].GetLeadingTerm())) { Monomial cancellingMonomial = p.GetLeadingTerm().DivideBy(divisorList[i - 1].GetLeadingTerm()); double cancellingCoefficient = p.monomialData[p.GetLeadingTerm()] / divisorList[i - 1].monomialData[divisorList[i - 1].GetLeadingTerm()]; quotients[i - 1].Add(new Polynomial(cancellingMonomial, cancellingCoefficient)); Polynomial cancellingPolynomial = new Polynomial(divisorList[i - 1]); cancellingPolynomial.MultiplyMonomial(cancellingMonomial, -cancellingCoefficient); p.Add(cancellingPolynomial); divisionoccurred = true; } else { i++; } } if (!divisionoccurred) { remainder.Add(p.GetLeadingTermAsPolynomial()); p.Add(p.GetLeadingTermAsPolynomial(false)); // A false addition is basically a subtraction. TODO: This is ugly! the Add method should have the flag to indicate subtraction. } } quotients.Add(remainder); return(quotients); }
/// <summary> /// Gets the S-polynomial as defined in section 2.6 of CLO /// </summary> /// <param name="other">The other polynomial with respect to which the S-polynomial is to be calculated.s</param> /// <returns>The S-polynomial of the combination - this and other.</returns> public Polynomial GetSPolynomial(Polynomial other) { Monomial lcm = this.GetLeadingTerm().LCM(other.GetLeadingTerm()); Polynomial thisTerm = new Polynomial(this); thisTerm.MultiplyMonomial(lcm.DivideBy(this.GetLeadingTerm())); thisTerm.MultiplyScalar(1 / this.GetLeadingCoefficient()); Polynomial otherTerm = new Polynomial(other); otherTerm.MultiplyMonomial(lcm.DivideBy(other.GetLeadingTerm())); otherTerm.MultiplyScalar(1 / other.GetLeadingCoefficient()); thisTerm.Add(otherTerm, -1); return(thisTerm); }