public Monomial Multiply(Monomial monomial) { return(new Monomial(this.coefficient * monomial.coefficient, this.exponent + monomial.exponent)); }
// This method assures that the list of terms is // always simplified (cannot exist multiple terms // with same degree) and sorted from largest to // smaller degree. public void Append(Monomial monomial) { // The monomial to add must be different than zero. if (monomial.Coefficient != 0) { // If the actual polynomial is zero, internal list // only contains one monomial and this monomial will // be replaced with the one received. if (this.terms.Count == 1 && this.terms[0].Coefficient == 0) { this.terms[0] = monomial; } else { bool appended = false; int i = 0; while (!appended && i < this.terms.Count) { // If the exponent of the monomial to append is greater // than the exponent of the monomial [i] analyzed, it // means that monomial to append does not exists in the // polynomial, so it is inserted in the currente position // to maintain the order of the list. if (monomial.Exponent > this.terms[i].Exponent) { this.terms.Insert(i, monomial); appended = true; } // If the exponent of the monomial to append is equal to // the exponent of the monomial [i] analyzed, perform // addition between monomials and replaces the current // monomial; in the case the sum of the monomials is zero, // removes the current monomial from the list. else if (monomial.Exponent == this.terms[i].Exponent) { if (this.terms[i].Coefficient + monomial.Coefficient != 0) { this.terms[i] = this.terms[i].Add(monomial); } else { this.terms.RemoveAt(i); } appended = true; } // Go to the next monomial in the internal list. else { i++; } } // If monomial has not been appended at this point, append it // at the end of the list. if (!appended) { this.terms.Add(monomial); } // If the list becomes empty, it means that the polynomial is // zero, so append a monomial equal to zero. if (this.terms.Count == 0) { this.terms.Add(new Monomial()); } // If the list has only one monomial and its coefficient is // zero, it means the that polynomial is zero, so replace // with a monomial equal to zero (this is only for consistency // of monomial representation). else if (this.terms.Count == 1 && this.terms[0].Coefficient == 0) { this.terms[0] = new Monomial(); } } } }
public static void TestMultiVariatePolynomialDivision() { Monomial m1 = new Monomial(new int[] { 3, 0, 0 }); Monomial m2 = new Monomial(new int[] { 2, 1, 0 }); Monomial m3 = new Monomial(new int[] { 2, 0, 1 }); Monomial m4 = new Monomial(new int[] { 1, 0, 0 }); Polynomial dividend = new Polynomial(m1); dividend.AddMonomial(m2, -1); dividend.AddMonomial(m3, -1); dividend.AddMonomial(m4); dividend = new Polynomial(new Monomial(new int[] { 1, 0, 0 })); dividend.AddMonomial(new Monomial(new int[] { 0, 0, 1 }), -1); Polynomial divisor1 = new Polynomial(new Monomial(new int[] { 2, 1, 0 })); // (y^2 - 1) divisor1.AddMonomial(new Monomial(new int[] { 0, 0, 1 }), -1); Polynomial divisor2 = new Polynomial(new Monomial(new int[] { 1, 1, 0 })); // (xy - 1) divisor2.AddMonomial(new Monomial(new int[] { 0, 0, 0 }), -1); List <Polynomial> quotients = dividend.DivideBy(divisor1, divisor2); foreach (Monomial m in quotients.LastOrDefault().monomialData.Keys) { System.Console.WriteLine(string.Join(",", m.powers) + " coefficient: " + quotients.LastOrDefault().monomialData[m].ToString()); } }