Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Poly"/> class.
        /// Constructor which creates new instance of poly from existing poly.
        /// Meant for copying polys.
        /// </summary>
        /// <param name="p">
        /// Polynomial instance
        /// </param>
        public Poly(Poly p)
        {
            this.terms = new TermCollection();

            for (int i = 0; i < p.Terms.Length; i++)
            {
                Term t = new Term(p.Terms[i].Power, p.Terms[i].Coefficient);

                this.terms.Add(t);
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Poly"/> class. 
        /// Constructor which creates new instance of poly from existing poly.
        /// Meant for copying polys.
        /// </summary>
        /// <param name="p">
        /// Polynomial instance
        /// </param>
        public Poly(Poly p)
        {
            this.terms = new TermCollection();

            for (int i = 0; i < p.Terms.Length; i++)
            {
                Term t = new Term(p.Terms[i].Power, p.Terms[i].Coefficient);

                this.terms.Add(t);
            }
        }
Example #3
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));
        }
Example #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);
            }
        }
Example #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));
        }
Example #6
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);
            }
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Poly"/> class. 
 /// Constructor which creates a new instance of Poly without any terms.
 /// Meant for terms to be added dynamically.
 /// </summary>
 public Poly()
 {
     this.terms = new TermCollection();
 }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Poly"/> class. 
 /// Constructor which create a new instance of Poly with a predefined TermCollection.
 /// </summary>
 /// <param name="terms">
 /// Polynomial terms
 /// </param>
 public Poly(TermCollection terms)
 {
     this.Terms = terms;
     this.Terms.Sort(TermCollection.SortType.Asc);
 }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Poly"/> class. 
 /// Constructor which Read String and find Terms in it. Create new Term for each
 /// Term String and add it to the Terms Collection. 
 /// </summary>
 /// <param name="polyExpression">
 /// Polynomial expression
 /// </param>
 public Poly(string polyExpression)
 {
     this.terms = new TermCollection();
     this.ReadPolyExpression(polyExpression);
 }
Example #10
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);
        }
Example #11
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);
        }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Poly"/> class.
 /// Constructor which creates a new instance of Poly without any terms.
 /// Meant for terms to be added dynamically.
 /// </summary>
 public Poly()
 {
     this.terms = new TermCollection();
 }
Example #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Poly"/> class.
 /// Constructor which create a new instance of Poly with a predefined TermCollection.
 /// </summary>
 /// <param name="terms">
 /// Polynomial terms
 /// </param>
 public Poly(TermCollection terms)
 {
     this.Terms = terms;
     this.Terms.Sort(TermCollection.SortType.Asc);
 }
Example #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Poly"/> class.
 /// Constructor which Read String and find Terms in it. Create new Term for each
 /// Term String and add it to the Terms Collection.
 /// </summary>
 /// <param name="polyExpression">
 /// Polynomial expression
 /// </param>
 public Poly(string polyExpression)
 {
     this.terms = new TermCollection();
     this.ReadPolyExpression(polyExpression);
 }