public void RunTest()
        {
            var polynomialText = "((2*x+1)*(x-4))^2*(x+3)^3";

            // Os objectos responsáveis pelas operações sobre os coeficientes
            var bigIntegerDomain          = new BigIntegerDomain();
            var bigIntegerParser          = new BigIntegerParser <string>();
            var bigIntToIntegerConversion = new BigIntegerToIntegerConversion();
            var bigIntFractionConversion  = new OuterElementFractionConversion <int, BigInteger>(
                bigIntToIntegerConversion,
                bigIntegerDomain);

            var polynomial = TestsHelper.ReadFractionalCoeffsUnivarPol <BigInteger, BigIntegerDomain>(
                polynomialText,
                bigIntegerDomain,
                bigIntegerParser,
                bigIntFractionConversion,
                "x");

            var squareFreeFactorizationAlg = new SquareFreeFractionFactorizationAlg <BigInteger>(
                bigIntegerDomain);
            var result = squareFreeFactorizationAlg.Run(polynomial);

            // O teste passa se a expansão da factorização ser igual ao polinómio original.
            Assert.IsTrue(result.Factors.Count > 0, "At least two factors are expected.");
            var factorsEnum = result.Factors.GetEnumerator();

            if (factorsEnum.MoveNext())
            {
                var polynomialDomain = new UnivarPolynomPseudoDomain <BigInteger>(
                    "x",
                    bigIntegerDomain);
                var productPol = MathFunctions.Power(
                    factorsEnum.Current.Value,
                    factorsEnum.Current.Key,
                    polynomialDomain);
                while (factorsEnum.MoveNext())
                {
                    var temporary = MathFunctions.Power(
                        factorsEnum.Current.Value,
                        factorsEnum.Current.Key,
                        polynomialDomain);
                    productPol = polynomialDomain.Multiply(
                        productPol,
                        temporary);
                }

                var fractionField = new FractionField <BigInteger>(bigIntegerDomain);
                var expectedPol   = new UnivariatePolynomialNormalForm <Fraction <BigInteger> >("x");
                foreach (var term in productPol)
                {
                    expectedPol = expectedPol.Add(
                        result.IndependentCoeff.Multiply(term.Value, bigIntegerDomain),
                        term.Key,
                        fractionField);
                }

                Assert.AreEqual(expectedPol, polynomial);
            }
        }
Esempio n. 2
0
        public void GetQuotientAndRemainderTest()
        {
            var dividend = "    x^3-1/3*x^2+ - -x/5-1/2";
            var divisor  = "x^2-x/2+1";

            // Os objectos responsáveis pelas operações sobre os coeficientes.
            var integerDomain = new IntegerDomain();
            var integerParser = new IntegerParser <string>();
            var conversion    = new ElementFractionConversion <int>(integerDomain);
            var fractionField = new FractionField <int>(integerDomain);

            // A leitura dos polinómios.
            var dividendPol = TestsHelper.ReadFractionalCoeffsUnivarPol <int, IntegerDomain>(
                dividend,
                integerDomain,
                integerParser,
                conversion,
                "x");
            var divisorPol = TestsHelper.ReadFractionalCoeffsUnivarPol <int, IntegerDomain>(
                divisor,
                integerDomain,
                integerParser,
                conversion,
                "x");
            var polynomialDomain = new UnivarPolynomEuclideanDomain <Fraction <int> >(
                "x",
                fractionField);
            var result   = polynomialDomain.GetQuotientAndRemainder(dividendPol, divisorPol);
            var expected = divisorPol.Multiply(result.Quotient, fractionField);

            expected = expected.Add(result.Remainder, fractionField);

            Assert.AreEqual(expected, dividendPol);
        }
Esempio n. 3
0
        public void RunTest_IntegerPolynomial()
        {
            var    integerDomain      = new IntegerDomain();
            var    fractionField      = new FractionField <int>(integerDomain);
            var    integerParser      = new IntegerParser <string>();
            var    conversion         = new ElementToElementConversion <int>();
            var    fractionConversion = new ElementFractionConversion <int>(integerDomain);
            string variableName       = "x";
            var    univarPolDomain    = new UnivarPolynomEuclideanDomain <Fraction <int> >(
                variableName,
                fractionField);

            var lagAlg     = new LagrangeAlgorithm <UnivariatePolynomialNormalForm <Fraction <int> > >(univarPolDomain);
            var firstValue = TestsHelper.ReadFractionalCoeffsUnivarPol <int, IntegerDomain>(
                "(x-1/2)*(x+1/3)",
                integerDomain,
                integerParser,
                fractionConversion,
                variableName);

            var secondValue = TestsHelper.ReadFractionalCoeffsUnivarPol <int, IntegerDomain>(
                "(x-1/2)*(x-1)",
                integerDomain,
                integerParser,
                fractionConversion,
                variableName);

            var gcd = TestsHelper.ReadFractionalCoeffsUnivarPol <int, IntegerDomain>(
                "x-1/2",
                integerDomain,
                integerParser,
                fractionConversion,
                variableName);
            var result = lagAlg.Run(firstValue, secondValue);

            var mainGcdCoeff = result.GreatestCommonDivisor.GetLeadingCoefficient(fractionField);
            var monicGcd     = result.GreatestCommonDivisor.Multiply(
                fractionField.MultiplicativeInverse(mainGcdCoeff),
                fractionField);

            Assert.AreEqual(gcd, monicGcd);

            var firstTermExpression  = univarPolDomain.Multiply(result.FirstFactor, result.FirstItem);
            var secondTermExpression = univarPolDomain.Multiply(result.SecondFactor, result.SecondItem);
            var actualExpression     = univarPolDomain.Add(firstTermExpression, secondTermExpression);

            Assert.AreEqual(result.GreatestCommonDivisor, actualExpression);

            actualExpression = univarPolDomain.Multiply(result.GreatestCommonDivisor, result.FirstCofactor);
            Assert.AreEqual(result.FirstItem, actualExpression);

            actualExpression = univarPolDomain.Multiply(result.GreatestCommonDivisor, result.SecondCofactor);
            Assert.AreEqual(result.SecondItem, actualExpression);
        }
Esempio n. 4
0
        public void GetPolynomialDerivativeTest_IntegerFraction()
        {
            var polynomialText           = "1/2*x^5+3/4*x^4-2/7*x^3+5/3*x^2+1/5*x+9";
            var polynomialDerivativeText = "5/2*x^4+3*x^3-6/7*x^2+10/3*x+1/5";
            var variableName             = "x";

            var integerDomain    = new IntegerDomain();
            var longDomain       = new LongDomain();
            var bigIntegerDomain = new BigIntegerDomain();

            var integerParser    = new IntegerParser <string>();
            var longParser       = new LongParser <string>();
            var bigIntegerParser = new BigIntegerParser <string>();

            var longConversion       = new LongToIntegerConversion();
            var bigIntegerConversion = new BigIntegerToIntegerConversion();

            var integerFractionConversion    = new ElementFractionConversion <int>(integerDomain);
            var longfractionConversion       = new OuterElementFractionConversion <int, long>(longConversion, longDomain);
            var bigIntegerfractionConversion = new OuterElementFractionConversion <int, BigInteger>(bigIntegerConversion, bigIntegerDomain);

            var integerFractionField    = new FractionField <int>(integerDomain);
            var longFractionField       = new FractionField <long>(longDomain);
            var bigIntegerFractionField = new FractionField <BigInteger>(bigIntegerDomain);

            // Coeficientes inteiros
            var integerPolynomial = TestsHelper.ReadFractionalCoeffsUnivarPol <int, IntegerDomain>(
                polynomialText,
                integerDomain,
                integerParser,
                integerFractionConversion,
                variableName);

            var integerPolynomialDerivative = TestsHelper.ReadFractionalCoeffsUnivarPol <int, IntegerDomain>(
                polynomialDerivativeText,
                integerDomain,
                integerParser,
                integerFractionConversion,
                variableName);
            var integerActualPolDerivative = integerPolynomial.GetPolynomialDerivative(integerFractionField);

            Assert.AreEqual(integerPolynomialDerivative, integerActualPolDerivative);

            // Coeficientes longos
            var longPolynomial = TestsHelper.ReadFractionalCoeffsUnivarPol <long, LongDomain>(
                polynomialText,
                longDomain,
                longParser,
                longfractionConversion,
                variableName);

            var longPolynomialDerivative = TestsHelper.ReadFractionalCoeffsUnivarPol <long, LongDomain>(
                polynomialDerivativeText,
                longDomain,
                longParser,
                longfractionConversion,
                variableName);
            var longActualPolDerivative = longPolynomial.GetPolynomialDerivative(longFractionField);

            Assert.AreEqual(longPolynomialDerivative, longActualPolDerivative);

            var bigIntegerPolynomial = TestsHelper.ReadFractionalCoeffsUnivarPol <BigInteger, BigIntegerDomain>(
                polynomialText,
                bigIntegerDomain,
                bigIntegerParser,
                bigIntegerfractionConversion,
                variableName);

            var bigIntegerPolynomialDerivative = TestsHelper.ReadFractionalCoeffsUnivarPol <BigInteger, BigIntegerDomain>(
                polynomialDerivativeText,
                bigIntegerDomain,
                bigIntegerParser,
                bigIntegerfractionConversion,
                variableName);
            var bigIntegerActualPolDerivative = bigIntegerPolynomial.GetPolynomialDerivative(bigIntegerFractionField);

            Assert.AreEqual(bigIntegerPolynomialDerivative, bigIntegerActualPolDerivative);
        }
Esempio n. 5
0
        public void PowerTest_FractionPolynomial()
        {
            var integerDomain    = new IntegerDomain();
            var longDomain       = new LongDomain();
            var bigIntegerDomain = new BigIntegerDomain();

            var variableName           = "x";
            var integerPolynomialField = new UnivarPolynomEuclideanDomain <Fraction <int> >(
                variableName,
                new FractionField <int>(integerDomain));
            var longPolynomialField = new UnivarPolynomEuclideanDomain <Fraction <long> >(
                variableName,
                new FractionField <long>(longDomain));
            var bigIntegerPolynomialField = new UnivarPolynomEuclideanDomain <Fraction <BigInteger> >(
                variableName,
                new FractionField <BigInteger>(bigIntegerDomain));

            // Leitores
            var integerParser    = new IntegerParser <string>();
            var longParser       = new LongParser <string>();
            var bigIntegerParser = new BigIntegerParser <string>();

            var integerConversion             = new OuterElementFractionConversion <int, int>(new ElementToElementConversion <int>(), integerDomain);
            var longToIntegerConversion       = new OuterElementFractionConversion <int, long>(new LongToIntegerConversion(), longDomain);
            var bigIntegerToIntegerConversion = new OuterElementFractionConversion <int, BigInteger>(new BigIntegerToIntegerConversion(), bigIntegerDomain);

            var intPowers = new int[3] {
                2, 3, 4
            };
            var longPowers = new long[3] {
                2, 3, 4
            };
            var bigIntPowers = new BigInteger[3] {
                2, 3, 4
            };

            var polynomialsTexts = new string[3] {
                "1/3*x^3-2/3*x^2+3/2*x-1/2", "2*x^2+4/3*x+4/9", "7/5*x+1"
            };
            var expectedPolinomialsTexts = new string[3] {
                "1/9*x^6-4/9*x^5+13/9*x^4-7/3*x^3+35/12*x^2-3/2*x+1/4",
                "8*x^6+16*x^5+16*x^4+256/27*x^3+32/9*x^2+64/81*x+64/729",
                "2401/625*x^4+1372/125*x^3+294/25*x^2+28/5*x+1"
            };

            // Coeficientes inteiros.
            for (int i = 0; i < 3; ++i)
            {
                var polynomialValue = TestsHelper.ReadFractionalCoeffsUnivarPol(
                    polynomialsTexts[i],
                    integerDomain,
                    integerParser,
                    integerConversion,
                    variableName);

                var expectedPolynomial = TestsHelper.ReadFractionalCoeffsUnivarPol(
                    expectedPolinomialsTexts[i],
                    integerDomain,
                    integerParser,
                    integerConversion,
                    variableName);

                var actualPolynomial = MathFunctions.Power(polynomialValue, intPowers[i], integerPolynomialField);
                Assert.AreEqual(expectedPolynomial, actualPolynomial);
            }

            // Coeficientes longos.
            for (int i = 0; i < 3; ++i)
            {
                var polynomialValue = TestsHelper.ReadFractionalCoeffsUnivarPol(
                    polynomialsTexts[i],
                    longDomain,
                    longParser,
                    longToIntegerConversion,
                    variableName);

                var expectedPolynomial = TestsHelper.ReadFractionalCoeffsUnivarPol(
                    expectedPolinomialsTexts[i],
                    longDomain,
                    longParser,
                    longToIntegerConversion,
                    variableName);

                var actualPolynomial = MathFunctions.Power(polynomialValue, intPowers[i], longPolynomialField);
                Assert.AreEqual(expectedPolynomial, actualPolynomial);
            }

            // Coeficientes correspondentes a inteiros de precisão arbitrária.
            for (int i = 0; i < 3; ++i)
            {
                var polynomialValue = TestsHelper.ReadFractionalCoeffsUnivarPol(
                    polynomialsTexts[i],
                    bigIntegerDomain,
                    bigIntegerParser,
                    bigIntegerToIntegerConversion,
                    variableName);

                var expectedPolynomial = TestsHelper.ReadFractionalCoeffsUnivarPol(
                    expectedPolinomialsTexts[i],
                    bigIntegerDomain,
                    bigIntegerParser,
                    bigIntegerToIntegerConversion,
                    variableName);

                var actualPolynomial = MathFunctions.Power(polynomialValue, intPowers[i], bigIntegerPolynomialField);
                Assert.AreEqual(expectedPolynomial, actualPolynomial);
            }
        }