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);
            }
        }
Example #2
0
        /// <summary>
        /// Permite efectuar a leitura de um polinómio a partir de texto.
        /// </summary>
        /// <remarks>
        /// Se a leitura não for bem sucedida, é lançada uma excep~ção.
        /// </remarks>
        /// <param name="polynomial">O texto.</param>
        /// <returns>O polinómio.</returns>
        public UnivariatePolynomialNormalForm <BigInteger> Read(string polynomial)
        {
            var integerDomain   = new BigIntegerDomain();
            var integerParser   = new BigIntegerParser <string>();
            var conversion      = new BigIntegerToIntegerConversion();
            var polInputReader  = new StringReader(polynomial);
            var polSymbolReader = new StringSymbolReader(polInputReader, false);
            var polParser       = new UnivariatePolynomialReader <BigInteger, CharSymbolReader <string> >(
                "x",
                integerParser,
                integerDomain);

            var result = default(UnivariatePolynomialNormalForm <BigInteger>);

            if (polParser.TryParsePolynomial(polSymbolReader, conversion, out result))
            {
                // O polinómio foi lido com sucesso.
                return(result);
            }
            else
            {
                // Não é possível ler o polinómio.
                throw new Exception("Can't read integer polynomial.");
            }
        }
        public void RunTest_BigIntegerNumbersRhoAlg()
        {
            var bigIntegerNumber = new BigIntegerDomain();
            var integerNumber    = new IntegerDomain();
            var integerParser    = new BigIntegerParser <string>();
            var conversion       = new BigIntegerToIntegerConversion();
            var variableName     = "x";
            var testPols         = new List <UnivariatePolynomialNormalForm <BigInteger> >();

            testPols.Add(TestsHelper.ReadUnivarPolynomial("x^123+1", bigIntegerNumber, integerParser, conversion, variableName));
            testPols.Add(TestsHelper.ReadUnivarPolynomial("x^452+1537*x+1", bigIntegerNumber, integerParser, conversion, variableName));

            var rhoAlgorithm = new PollardRhoAlgorithm <BigInteger>(
                testPols,
                new ModularBigIntFieldFactory(),
                bigIntegerNumber);
            var factorizationTarget = new DecompositionFactorizationAlgorithm <BigInteger, int>(
                rhoAlgorithm,
                1,
                integerNumber,
                bigIntegerNumber);
            var value    = BigInteger.Parse("1000000000001");
            var expected = new Dictionary <BigInteger, int>();

            expected.Add(137, 1);
            expected.Add(73, 1);
            expected.Add(BigInteger.Parse("99990001"), 1);
            var actual = factorizationTarget.Run(value);

            CollectionAssert.AreEquivalent(expected, actual);
        }
        public void RunTest_BigIntegerPolynomial()
        {
            var    integerDomain      = new BigIntegerDomain();
            var    fractionField      = new FractionField <BigInteger>(integerDomain);
            var    integerParser      = new BigIntegerParser <string>();
            var    conversion         = new BigIntegerToIntegerConversion();
            var    fractionConversion = new OuterElementFractionConversion <int, BigInteger>(conversion, integerDomain);
            string variableName       = "x";
            var    univarPolDomain    = new UnivarPolynomEuclideanDomain <Fraction <BigInteger> >(
                variableName,
                fractionField);

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

            var secondValue = TestsHelper.ReadFractionalCoeffsUnivarPol <BigInteger, BigIntegerDomain>(
                "(x-1/2)^3*(x+1/3)^2",
                integerDomain,
                integerParser,
                fractionConversion,
                variableName);

            var gcd = TestsHelper.ReadFractionalCoeffsUnivarPol <BigInteger, BigIntegerDomain>(
                "(x-1/2)^2*(x+1/3)^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);
        }
 public IntegerBigIntFractionConversion(
     IIntegerNumber <BigInteger> integerNumber,
     BigIntegerToIntegerConversion bigIntegerToIntegerConversion)
 {
     if (integerNumber == null)
     {
         throw new ArgumentNullException("integerNumber");
     }
     else if (bigIntegerToIntegerConversion == null)
     {
         throw new ArgumentNullException("bigIntegerToIntegerConversion");
     }
     else
     {
         this.integerNumber = integerNumber;
         this.bigIntegerToIntegerConversion = bigIntegerToIntegerConversion;
     }
 }
Example #6
0
        public void RunTest_BigIntegerMatrix()
        {
            // A leitura é realizada por colunas.
            var matrixText    = "[[100000,1001,20005], [32534,4245341,56134513451], [21346136,1134613,1136135613]]";
            var integerDomain = new BigIntegerDomain();
            var variableName  = "x";
            var integerParser = new BigIntegerParser <string>();
            var conversion    = new BigIntegerToIntegerConversion();
            var matrix        = TestsHelper.ReadMatrix <BigInteger>(
                3,
                3,
                matrixText,
                (i, j) => new ArraySquareMathMatrix <BigInteger>(i),
                integerParser);
            var fastDivFreeCharacPolAlg = new FastDivisionFreeCharPolynomCalculator <BigInteger>(variableName, integerDomain);
            var expected = TestsHelper.ReadUnivarPolynomial("1*x^3+-1140480954*x^2-58754054577367644*x+4689162494877443109176", integerDomain, integerParser, conversion, variableName);
            var actual   = fastDivFreeCharacPolAlg.Run(matrix as ISquareMathMatrix <BigInteger>);

            Assert.AreEqual(expected, actual);
        }
Example #7
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);
        }
Example #8
0
        public void GetPolynomialDerivativeTest_SimpleInteger()
        {
            // Representação dos polinómios.
            var polynomText       = "x^1000-2*x^550+1000*x^10+50";
            var polDerivativeText = "1000*x^999-1100*x^549+10000*x^9";

            var variableName = "x";

            // Estabelece os domínios.
            var integerDomain    = new IntegerDomain();
            var longDomain       = new LongDomain();
            var bigIntegerDomain = new BigIntegerDomain();

            // Estabelece os conversores.
            var integerToIntegerConv           = new ElementToElementConversion <int>();
            var integerToLongConv              = new LongToIntegerConversion();
            var integerToBigIntegerConvsersion = new BigIntegerToIntegerConversion();

            // Estabelece os leitores individuais.
            var integerParser    = new IntegerParser <string>();
            var longParser       = new LongParser <string>();
            var bigIntegerParser = new BigIntegerParser <string>();

            // Estabelece os polinómios.
            var integerPolynomial = TestsHelper.ReadUnivarPolynomial(
                polynomText,
                integerDomain,
                integerParser,
                integerToIntegerConv,
                variableName);
            var integerExpectedPolynomial = TestsHelper.ReadUnivarPolynomial(
                polDerivativeText,
                integerDomain,
                integerParser,
                integerToIntegerConv,
                variableName);
            var integerActualDerivative = integerPolynomial.GetPolynomialDerivative(integerDomain);

            // Verifica se os polinómios são válidos.
            Assert.AreEqual(integerExpectedPolynomial, integerActualDerivative);

            // Estabelece os polinómios.
            var longPolynomial = TestsHelper.ReadUnivarPolynomial(
                polynomText,
                longDomain,
                longParser,
                integerToLongConv,
                variableName);
            var longExpectedPolynomial = TestsHelper.ReadUnivarPolynomial(
                polDerivativeText,
                longDomain,
                longParser,
                integerToLongConv,
                variableName);
            var longActualDerivative = longPolynomial.GetPolynomialDerivative(longDomain);

            // Verifica se os polinómios são válidos.
            Assert.AreEqual(longExpectedPolynomial, longActualDerivative);

            // Estabelece os polinómios.
            var bigIntegerPolynomial = TestsHelper.ReadUnivarPolynomial(
                polynomText,
                bigIntegerDomain,
                bigIntegerParser,
                integerToBigIntegerConvsersion,
                variableName);
            var bigIntegerExpectedPolynomial = TestsHelper.ReadUnivarPolynomial(
                polDerivativeText,
                bigIntegerDomain,
                bigIntegerParser,
                integerToBigIntegerConvsersion,
                variableName);
            var bigIntegerActualDerivative = bigIntegerPolynomial.GetPolynomialDerivative(bigIntegerDomain);

            // Verifica se os polinómios são válidos.
            Assert.AreEqual(bigIntegerExpectedPolynomial, bigIntegerActualDerivative);
        }
Example #9
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);
        }
Example #10
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);
            }
        }