C#: Implementing polynomials with the Tuple class

ddanbe 0 Tallied Votes 445 Views Share

Don’t know if this is ever been done before, or even if it is a good way to do it.
I’ll just await your comments if any.
What I mostly found on the web is that if you want to define a polynomial you need to have an array containing the coefficients. The place of the coefficient in the array determines the power of the unknown, you have to include a 0(zero) in the array for the powers that are not in your polynomial.
So I tried to do it using a list of tuples, with one element containing the coefficient and the other the power or exponent. That way I did not have to include zero coefficients in my list. Even the order of the Tuples in the list would not matter. Further, I wrapped the rather generic Tuple class in a TMonomial class. Polynomials are in fact just a bunch of monomials. It was also an excellent opportunity to use the operator overload feature. If I did too many of them or if I missed a few, please let me know.
As code examples: my console test program and the code of my polynomial division, which implements the division algorithm quite readable I think.
Also included a zip file with the whole project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Polynomes
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Polynomials using tuples: *****");

            TMonomial[] trms = new TMonomial[] { new TMonomial(1, 2), new TMonomial(-10, 0), new TMonomial(-9, 1) };
            TPolynomial PolyT1 = new TPolynomial(trms);

            TMonomial[] trms2 = new TMonomial[] { new TMonomial(1, 2), new TMonomial(1, 0) };
            TPolynomial PolyT2 = new TPolynomial(trms2);

            Console.WriteLine("P1 = " + PolyT1.ToString());
            Console.WriteLine("P2 = " + PolyT2.ToString());
            Console.WriteLine();
            TPolynomial PolyT3 = PolyT1 + PolyT2;
            Console.WriteLine("P1 + P2 = " + PolyT3.ToString());
            Console.WriteLine();
            Console.WriteLine("P1 for value -1.0 = " + PolyT1.Evaluate(-1.0).ToString());
            Console.WriteLine();
            PolyT3 = 12.34 * PolyT1;
            Console.WriteLine("12.34 * P1 = " + PolyT3.ToString());
            Console.WriteLine();
            Console.WriteLine("The division P1 / P2 gives:");
            var R = PolyT1 / PolyT2;
            Console.WriteLine("Quotient = {0}", R.Item1.ToString());
            Console.WriteLine("Rest = {0}", R.Item2.ToString());

            Console.ReadKey();
        }
    }
}

//*********************************************************************************************************

public static Tuple<TPolynomial, TPolynomial> operator /(TPolynomial dividend, TPolynomial divisor)
        {
            if (dividend.Degree < divisor.Degree)
            {
                throw new Exception("Cannot divide " + dividend.ToString() + " by " + divisor.ToString());
            }
            TPolynomial Quotient = new TPolynomial();
            TPolynomial Rest = new TPolynomial();
            TMonomial term;
            dividend.Sort(); //from higest term to lowest
            divisor.Sort();
            Rest = dividend;
            while (Rest.Degree >= divisor.Degree)
            {
                term = Rest.Terms[0] / divisor.Terms[0]; //divide highest terms
                Quotient.Terms.Add(term);              //build up quotient
                Rest -= term * divisor;                //build up possible rest
            }
            Tuple<TPolynomial, TPolynomial> tuple =
                new Tuple<TPolynomial, TPolynomial>(Quotient, Rest);
            return tuple;
        }