/// <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); }