/// <summary> /// Sort Method: Sort Items of Collection in ASC or DESC Order. /// </summary> /// <param name="order"> /// SortOrder values : ASC or DESC /// </param> public void Sort(SortType order) { TermCollection result = new TermCollection(); if (order == SortType.Asc) { while (this.Length > 0) { Term minTerm = this[0]; foreach (Term t in this.List) { if (t.Power < minTerm.Power) { minTerm = t; } } result.Add(minTerm); this.Remove(minTerm); } } else { while (this.Length > 0) { Term maxTerm = this[0]; foreach (Term t in this.List) { if (t.Power > maxTerm.Power) { maxTerm = t; } } result.Add(maxTerm); this.Remove(maxTerm); } } this.Clear(); foreach (Term t in result) { this.Add(t); } }
/// <summary> /// Multiple Operation: For each term in the First Poly We Multiple it in the Each Term of Second Poly /// </summary> /// <param name="p1">Polynomial one</param> /// <param name="p2">Polynomial two</param> /// <returns>Multiplied polynomial expression</returns> public static Poly operator *(Poly p1, Poly p2) { TermCollection result = new TermCollection(); foreach (Term t1 in p1.Terms) { foreach (Term t2 in p2.Terms) { result.Add(new Term(t1.Power + t2.Power, t1.Coefficient * t2.Coefficient)); } } return(new Poly(result)); }
/// <summary> /// Division operator /// </summary> /// <param name="p1">Polynomial one</param> /// <param name="p2">Polynomial two</param> /// <returns>Divided polynomial</returns> public static Poly operator /(Poly p1, Poly p2) { p1.Terms.Sort(TermCollection.SortType.Des); p2.Terms.Sort(TermCollection.SortType.Des); TermCollection resultTerms = new TermCollection(); if (p1.Terms[0].Power < p2.Terms[0].Power) { throw new Exception("Invalid Division: P1.MaxPower is Lower than P2.MaxPower"); } while (p1.Terms[0].Power > p2.Terms[0].Power) { Term nextResult = new Term( p1.Terms[0].Power - p2.Terms[0].Power, p1.Terms[0].Coefficient / p2.Terms[0].Coefficient); resultTerms.Add(nextResult); Poly tempPoly = nextResult; Poly newPoly = tempPoly * p2; p1 = p1 - newPoly; } return(new Poly(resultTerms)); }
/// <summary> /// Division operator /// </summary> /// <param name="p1">Polynomial one</param> /// <param name="p2">Polynomial two</param> /// <returns>Divided polynomial</returns> public static Poly operator /(Poly p1, Poly p2) { p1.Terms.Sort(TermCollection.SortType.Des); p2.Terms.Sort(TermCollection.SortType.Des); TermCollection resultTerms = new TermCollection(); if (p1.Terms[0].Power < p2.Terms[0].Power) { throw new Exception("Invalid Division: P1.MaxPower is Lower than P2.MaxPower"); } while (p1.Terms[0].Power > p2.Terms[0].Power) { Term nextResult = new Term( p1.Terms[0].Power - p2.Terms[0].Power, p1.Terms[0].Coefficient / p2.Terms[0].Coefficient); resultTerms.Add(nextResult); Poly tempPoly = nextResult; Poly newPoly = tempPoly * p2; p1 = p1 - newPoly; } return new Poly(resultTerms); }
/// <summary> /// Multiple Operation: For each term in the First Poly We Multiple it in the Each Term of Second Poly /// </summary> /// <param name="p1">Polynomial one</param> /// <param name="p2">Polynomial two</param> /// <returns>Multiplied polynomial expression</returns> public static Poly operator *(Poly p1, Poly p2) { TermCollection result = new TermCollection(); foreach (Term t1 in p1.Terms) { foreach (Term t2 in p2.Terms) { result.Add(new Term(t1.Power + t2.Power, t1.Coefficient * t2.Coefficient)); } } return new Poly(result); }