Exemplo n.º 1
0
        /// <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);
            }
        }
Exemplo n.º 2
0
        /// <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));
        }
Exemplo n.º 3
0
        /// <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));
        }
Exemplo n.º 4
0
        /// <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);
            }
        }
Exemplo n.º 5
0
        /// <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);
        }
Exemplo n.º 6
0
        /// <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);
        }