Ejemplo n.º 1
0
        public void GetPolynomialDerivativeTest_IntegerPolynomialAsCoefficients()
        {
            var polynomialText           = "(y^2+y+1)*x^3-2*x^2*y+x*(y^5-3)+4";
            var polynomialDerivativeText = "3*(y^2+y+1)*x^2-4*y*x+y^5-3";
            var variableName             = "x";
            var coeffsVariableName       = "y";

            // Os domínios responsáveis pelas operações sobre os inteiros.
            var integerDomain    = new IntegerDomain();
            var longDomain       = new LongDomain();
            var bigIntegerDomain = new BigIntegerDomain();

            // Os leitore sde inteiros
            var integerParser    = new IntegerParser <string>();
            var longParser       = new LongParser <string>();
            var bigIntegerParser = new BigIntegerParser <string>();

            // Definição das conversões.
            var integerConversion    = new ElementToElementConversion <int>();
            var longConversion       = new LongToIntegerConversion();
            var bigIntegerConversion = new BigIntegerToIntegerConversion();

            var integerPolConvertion = new UnivarPolynomNormalFormToIntegerConversion <int>(
                coeffsVariableName,
                integerConversion,
                integerDomain);
            var longPolConvertion = new UnivarPolynomNormalFormToIntegerConversion <long>(
                coeffsVariableName,
                longConversion,
                longDomain);
            var bigIntegerPolConvertion = new UnivarPolynomNormalFormToIntegerConversion <BigInteger>(
                coeffsVariableName,
                bigIntegerConversion,
                bigIntegerDomain);

            // Definição dos anéis polinomiais.
            var integerPolynomialRing    = new UnivarPolynomRing <int>(coeffsVariableName, integerDomain);
            var longPolynomialRing       = new UnivarPolynomRing <long>(coeffsVariableName, longDomain);
            var bigIntegerPolynomialRing = new UnivarPolynomRing <BigInteger>(coeffsVariableName, bigIntegerDomain);

            // Definição dos leitores polinomiais.
            var integerPolynomialParser = new UnivarPolNormalFormParser <int>(
                coeffsVariableName,
                integerConversion,
                integerParser,
                integerDomain);
            var longPolynomialParser = new UnivarPolNormalFormParser <long>(
                coeffsVariableName,
                longConversion,
                longParser,
                longDomain);
            var bigIntegerPolynomialParser = new UnivarPolNormalFormParser <BigInteger>(
                coeffsVariableName,
                bigIntegerConversion,
                bigIntegerParser,
                bigIntegerDomain);

            // Definição dos testes.
            var integerPolynomial = TestsHelper.ReadUnivarPolynomial <UnivariatePolynomialNormalForm <int> >(
                polynomialText,
                integerPolynomialRing,
                integerPolynomialParser,
                integerPolConvertion,
                variableName);
            var integerExpectedPol = TestsHelper.ReadUnivarPolynomial <UnivariatePolynomialNormalForm <int> >(
                polynomialDerivativeText,
                integerPolynomialRing,
                integerPolynomialParser,
                integerPolConvertion,
                variableName);
            var integerActualPlynomial = integerPolynomial.GetPolynomialDerivative(integerPolynomialRing);

            Assert.AreEqual(integerExpectedPol, integerActualPlynomial);

            var longPolynomial = TestsHelper.ReadUnivarPolynomial <UnivariatePolynomialNormalForm <long> >(
                polynomialText,
                longPolynomialRing,
                longPolynomialParser,
                longPolConvertion,
                variableName);
            var longExpectedPol = TestsHelper.ReadUnivarPolynomial <UnivariatePolynomialNormalForm <long> >(
                polynomialDerivativeText,
                longPolynomialRing,
                longPolynomialParser,
                longPolConvertion,
                variableName);
            var longActualPlynomial = longPolynomial.GetPolynomialDerivative(longPolynomialRing);

            Assert.AreEqual(longExpectedPol, longActualPlynomial);

            var bigIntegerPolynomial = TestsHelper.ReadUnivarPolynomial <UnivariatePolynomialNormalForm <BigInteger> >(
                polynomialText,
                bigIntegerPolynomialRing,
                bigIntegerPolynomialParser,
                bigIntegerPolConvertion,
                variableName);
            var bigIntegerExpectedPol = TestsHelper.ReadUnivarPolynomial <UnivariatePolynomialNormalForm <BigInteger> >(
                polynomialDerivativeText,
                bigIntegerPolynomialRing,
                bigIntegerPolynomialParser,
                bigIntegerPolConvertion,
                variableName);
            var bigIntegerActualPlynomial = bigIntegerPolynomial.GetPolynomialDerivative(bigIntegerPolynomialRing);

            Assert.AreEqual(bigIntegerExpectedPol, bigIntegerExpectedPol);
        }
Ejemplo n.º 2
0
        public void PowerTest_IntegerPolynomial()
        {
            var integerDomain    = new IntegerDomain();
            var longDomain       = new LongDomain();
            var bigIntegerDomain = new BigIntegerDomain();

            var variableName        = "x";
            var intPolDomain        = new UnivarPolynomRing <int>(variableName, integerDomain);
            var longPolDomain       = new UnivarPolynomRing <long>(variableName, longDomain);
            var bigIntegerPolDomain = new UnivarPolynomRing <BigInteger>(variableName, bigIntegerDomain);

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

            var integerConversion             = new ElementToElementConversion <int>();
            var longToIntegerConversion       = new LongToIntegerConversion();
            var bigIntegerToIntegerConversion = new BigIntegerToIntegerConversion();

            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] {
                "x^3-2*x^2+3*x-1", "2*x^2+4*x+4", "x+1"
            };
            var expectedPolinomialsTexts = new string[3] {
                "x^6-4*x^5+10*x^4-14*x^3+13*x^2-6*x+1",
                "8*x^6+48*x^5+144*x^4+256*x^3+288*x^2+192*x+64",
                "x^4+4*x^3+6*x^2+4*x+1"
            };

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

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

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

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

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

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

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

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

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