コード例 #1
0
ファイル: Polynomial.cs プロジェクト: coderasm/Polynomials
        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 });
        }
コード例 #2
0
        public static void ProductTest()
        {
            Console.WriteLine("########## PART 2 ############");
            int[] coefficientOne = { 1, 1, 1 };
            var   polyOne        = new Polynomial(coefficientOne);

            int[] coefficientTwo = { 1, 1, 0 };
            var   polyTwo        = new Polynomial(coefficientTwo);

            int[] coefficientThree = { 3, 2, 0, 4 };
            var   polyThree        = new Polynomial(coefficientThree);

            int[] coefficientFour = { 0, 3, 2, 0 };
            var   polyFour        = new Polynomial(coefficientFour);

            Console.WriteLine("Polynomial 1: " + polyOne.ToString());
            Console.WriteLine("Polynomial 2: " + polyTwo.ToString());
            Console.WriteLine("Product: " + polyOne.Product(polyTwo).ToString());
            Console.WriteLine("Polynomial 3: " + polyThree.ToString());
            Console.WriteLine("Polynomial 4: " + polyFour.ToString());
            Console.WriteLine("Product: " + polyThree.Product(polyFour).ToString());
            Console.WriteLine();
        }
コード例 #3
0
        public static void DivisionTest()
        {
            Console.WriteLine("########## PART 3 ############");
            var coeffientsOne   = new int[] { 1, 1, 0, 1 };
            var polyOne         = new Polynomial(coeffientsOne);
            var coeffientsTwo   = new int[] { 1, 1 };
            var polyTwo         = new Polynomial(coeffientsTwo);
            var coeffientsThree = new int[] { 1, 1, 0, 0, 0, 1 };
            var polyThree       = new Polynomial(coeffientsThree);
            var coeffientsFour  = new int[] { 1, 1, 1 };
            var polyFour        = new Polynomial(coeffientsFour);
            var coeffientsFive  = new int[] { 1, 1, 1, 0, 0, 1, 0, 1 };
            var polyFive        = new Polynomial(coeffientsFive);
            var coeffientsSix   = new int[] { 1, 0, 1, 0, 1 };
            var polySix         = new Polynomial(coeffientsSix);

            var divisionResultOne = polyOne.Divide(polyTwo);
            var remainderOne      = divisionResultOne[1].GreatestPower() >= 0 ? divisionResultOne[1].ToString() : "0";

            Console.WriteLine("(" + polyOne.ToString() + ") / (" + polyTwo.ToString() + ") = " + divisionResultOne[0].ToString() + " Remainder: " + remainderOne);
            Console.WriteLine("(" + polyTwo.ToString() + ") * (" + divisionResultOne[0] + ") + " + divisionResultOne[1] + " = " + polyTwo.Product(divisionResultOne[0]).Sum(divisionResultOne[1]));
            var divisionResultTwo = polyThree.Divide(polyFour);
            var remainderTwo      = divisionResultTwo[1].GreatestPower() >= 0 ? divisionResultTwo[1].ToString() : "0";

            Console.WriteLine("(" + polyThree.ToString() + ") / (" + polyFour.ToString() + ") = " + divisionResultTwo[0].ToString() + " Remainder: " + remainderTwo);
            Console.WriteLine("(" + polyFour.ToString() + ") * (" + divisionResultTwo[0] + ") + " + divisionResultTwo[1] + " = " + polyFour.Product(divisionResultTwo[0]).Sum(divisionResultTwo[1]));
            var divisionResultThree = polyFive.Divide(polySix);
            var remainderThree      = divisionResultThree[1].GreatestPower() >= 0 ? divisionResultThree[1].ToString() : "0";

            Console.WriteLine("(" + polyFive.ToString() + ") / (" + polySix.ToString() + ") = " + divisionResultThree[0].ToString() + " Remainder: " + remainderThree);
            Console.WriteLine("(" + polySix.ToString() + ") * (" + divisionResultThree[0] + ") + " + divisionResultThree[1] + " = " + polySix.Product(divisionResultThree[0]).Sum(divisionResultThree[1]));
            Console.WriteLine();
        }