示例#1
0
        public static void AddDiffPrintTest()
        {
            Console.WriteLine("########## PART 6 ############");
            Console.WriteLine();
            int[] coefficientsOne = { 4, -5, 1, 0 };
            int[] coefficientsTwo = { 1, 1, 0, 1 };
            var   polyOne         = new Polynomial(coefficientsOne);
            var   polyTwo         = new Polynomial(coefficientsTwo);

            Console.Write("Polynomial 1: ");
            polyOne.ToString();
            Console.Write("Polynomial 2: ");
            polyTwo.ToString();
            Console.Write("Sum: ");
            polyOne.Sum(polyTwo).ToString();
            Console.Write("Difference: ");
            polyOne.Difference(polyTwo).ToString();

            coefficientsOne = new int[] { 4, -5, 1 };
            coefficientsTwo = new int[] { 4, -5, -1 };
            polyOne         = new Polynomial(coefficientsOne);
            polyTwo         = new Polynomial(coefficientsTwo);
            Console.Write("Polynomial 1: ");
            polyOne.ToString();
            Console.Write("Polynomial 2: ");
            polyTwo.ToString();
            Console.Write("Sum: ");
            polyOne.Sum(polyTwo).ToString();
            Console.Write("Difference: ");
            polyOne.Difference(polyTwo).ToString();
        }
示例#2
0
        public Polynomial[] Divide(Polynomial polynomial)
        {
            var newLength             = coefficients.Length;
            var quotient              = new Polynomial(new int[newLength]);
            var dividend              = this;
            var divisorGreatestPower  = polynomial.GreatestPower();
            var dividendGreatestPower = dividend.GreatestPower();

            //only divide if divisable
            while (dividendGreatestPower >= divisorGreatestPower)
            {
                var powerNeeded       = dividendGreatestPower - divisorGreatestPower;
                var coefficientNeeded = dividend.coefficients[dividendGreatestPower] / polynomial.coefficients[divisorGreatestPower];
                //correct sign
                coefficientNeeded = dividend.coefficients[dividendGreatestPower] > 0 ? Math.Abs(coefficientNeeded) : -1 * Math.Abs(coefficientNeeded);
                //build new polynomial
                var quotientCoefficient = new int[newLength];
                quotientCoefficient[powerNeeded] = coefficientNeeded;
                var partialQuotient = new Polynomial(quotientCoefficient);
                //add onto the quotient
                quotient = quotient.Sum(partialQuotient);
                //multiply new quotient into divisor and subtract from current
                //dividend for new dividend
                dividend = dividend.Difference(polynomial.Product(partialQuotient));
                //calculate top power of new dividend.
                dividendGreatestPower = dividend.GreatestPower();
            }
            return(new Polynomial[] { quotient, dividend });
        }