/// <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() { 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); } }
public void AddPowerTest_BigInteger() { var multiplier = new BigIntegerDomain(); var intPowers = new int[] { 2, 3, 4, 1, 20, 13 }; var longPowers = new long[] { 2, 3, 4, 1, 20, 13 }; var bigIntPowers = new BigInteger[] { 2, 3, 4, 1, 20, 13 }; var values = new BigInteger[] { 13, 25, 11, 100, 2, 3 }; var expected = new BigInteger[] { 26, 75, 44, 100, 40, 39 }; // Potências inteiras. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.AddPower(values[i], intPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências longas for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.AddPower(values[i], longPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências com inteiros grandes. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.AddPower(values[i], bigIntPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } }
/// <summary> /// Permite efectuar a leitura de um polinómio com coeficientes fraccionários 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 <Fraction <BigInteger> > Read(string polynomial) { var integerDomain = new BigIntegerDomain(); var fractionField = new FractionField <BigInteger>(integerDomain); var integerParser = new BigIntegerParser <string>(); var fractionParser = new FieldDrivenExpressionParser <Fraction <BigInteger> >( new SimpleElementFractionParser <BigInteger>(integerParser, integerDomain), fractionField); var conversion = new IntegerBigIntFractionConversion(integerDomain, new BigIntegerToIntegerConversion()); var polInputReader = new StringReader(polynomial); var polSymbolReader = new StringSymbolReader(polInputReader, false); var polParser = new UnivariatePolynomialReader <Fraction <BigInteger>, CharSymbolReader <string> >( "x", fractionParser, fractionField); var result = default(UnivariatePolynomialNormalForm <Fraction <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 PowerTest_BigInteger() { var multiplier = new BigIntegerDomain(); var intPowers = new int[] { 2, 3, 4, 1, 20, 13 }; var longPowers = new long[] { 2, 3, 4, 1, 20, 13 }; var bigIntPowers = new BigInteger[] { 2, 3, 4, 1, 20, 13 }; var values = new BigInteger[] { 13, 25, 11, 100, 2, 3 }; var expected = new BigInteger[] { 169, 15625, 14641, 100, 1048576, 1594323 }; // Potências inteiras. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], intPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências longas for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], longPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências com inteiros grandes. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], bigIntPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências genéricas. var integerNumber = new IntegerDomain(); var longNumber = new LongDomain(); var bigIntegerNumber = new BigIntegerDomain(); // Potências inteiras. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], intPowers[i], multiplier, integerNumber); Assert.AreEqual(expected[i], actual); } // Potências longas for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], longPowers[i], multiplier, longNumber); Assert.AreEqual(expected[i], actual); } // Potências com inteiros grandes. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], bigIntPowers[i], multiplier, bigIntegerNumber); Assert.AreEqual(expected[i], 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 void GreatCommonDivisorTest_BigIntegerNumber() { var domain = new BigIntegerDomain(); var firstValues = new BigInteger[] { 6, 48, 3251, 100, 3528, 427 }; var secondValues = new BigInteger[] { 12, 36, 4525, 75, 4116, 254 }; var expected = new BigInteger[] { 6, 12, 1, 25, 588, 1 }; for (int i = 0; i < firstValues.Length; ++i) { var actual = MathFunctions.GreatCommonDivisor(firstValues[i], secondValues[i], domain); Assert.AreEqual(expected[i], actual); } }
public void RunTest() { var inputMatrix = "[[1,2,1],[2,-1,-1],[1,-1,3]]"; var inputVector = "[[1,2,3]]"; var expectedText = "[[21/19,-6/19,10/19]]"; var integerDomain = new BigIntegerDomain(); var integerParser = new BigIntegerParser <string>(); var fractionField = new FractionField <BigInteger>(integerDomain); var fractionFieldParser = new FieldDrivenExpressionParser <Fraction <BigInteger> >( new SimpleElementFractionParser <BigInteger>(integerParser, integerDomain), fractionField); var matrixFactory = new ArrayMathMatrixFactory <Fraction <BigInteger> >(); // Leitura da matriz que representa o sistema de equações. var coeffsMatrix = TestsHelper.ReadMatrix <Fraction <BigInteger> >( 3, 3, inputMatrix, (i, j) => new ArrayMathMatrix <Fraction <BigInteger> >(i, j), fractionFieldParser); // Leitura do vector de termos independente. var vectorMatrix = TestsHelper.ReadMatrix <Fraction <BigInteger> >( 3, 1, inputVector, (i, j) => new ArrayMathMatrix <Fraction <BigInteger> >(i, j), fractionFieldParser); var expectedMatrix = TestsHelper.ReadMatrix <Fraction <BigInteger> >( 3, 1, expectedText, (i, j) => new ArrayMathMatrix <Fraction <BigInteger> >(i, j), fractionFieldParser); var systemSolver = new SequentialLanczosAlgorithm <Fraction <BigInteger>, FractionField <BigInteger> >( matrixFactory, fractionField); var squareMatrix = (coeffsMatrix as ArrayMathMatrix <Fraction <BigInteger> >).AsSquare(); var actual = systemSolver.Run(squareMatrix, vectorMatrix); for (int i = 0; i < 3; ++i) { Assert.AreEqual(expectedMatrix[i, 0], actual[i, 0]); } }
public void AddPowerTest_BigIntegerFraction() { var domain = new BigIntegerDomain(); var multiplier = new FractionField <BigInteger>(domain); var intPowers = new int[] { 2, 3, 4, 1, 20, 13 }; var longPowers = new long[] { 2, 3, 4, 1, 20, 13 }; var bigIntPowers = new BigInteger[] { 2, 3, 4, 1, 20, 13 }; var values = new[] { new Fraction <BigInteger>(13, 17, domain), new Fraction <BigInteger>(25, 23, domain), new Fraction <BigInteger>(11, 15, domain), new Fraction <BigInteger>(100, 99, domain), new Fraction <BigInteger>(1, 2, domain), new Fraction <BigInteger>(3, 5, domain) }; var expected = new[] { new Fraction <BigInteger>(26, 17, domain), new Fraction <BigInteger>(75, 23, domain), new Fraction <BigInteger>(44, 15, domain), new Fraction <BigInteger>(100, 99, domain), new Fraction <BigInteger>(10, 1, domain), new Fraction <BigInteger>(39, 5, domain) }; // Potências inteiras. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.AddPower(values[i], intPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências longas for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.AddPower(values[i], longPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências com inteiros grandes. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.AddPower(values[i], bigIntPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } }
public void RunTest_BigInteger() { var integerSquareRootAlg = new BigIntSquareRootAlgorithm(); var integerNumber = new BigIntegerDomain(); var factorizationAlg = new NaiveIntegerFactorizationAlgorithm <BigInteger>( integerSquareRootAlg, integerNumber); 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 = factorizationAlg.Run(value); CollectionAssert.AreEquivalent(expected, actual); }
public void RunTest_BigInteger() { var squareRootAlgorithm = new BigIntSquareRootAlgorithm(); var primeNumberFactory = new BigIntegerPrimeNumbersIteratorFactory(); var integerNumber = new BigIntegerDomain(); var totientAlgorithm = new EulerTotFuncAlg <BigInteger>( squareRootAlgorithm, primeNumberFactory, integerNumber); var values = new[] { 1, 12, 343, 720, 73, 100 }; var expected = new[] { 1, 4, 294, 192, 72, 40 }; for (int i = 0; i < values.Length; ++i) { var actual = totientAlgorithm.Run(values[i]); Assert.AreEqual(expected[i], actual); } }
public void RunTest_BigInteger() { var integerDomain = new BigIntegerDomain(); var lagAlg = new LagrangeAlgorithm <BigInteger>(integerDomain); var firstValue = BigInteger.Parse("91986494539681"); var secondValue = BigInteger.Parse("19645957369297"); var result = lagAlg.Run(firstValue, secondValue); Assert.AreEqual(9590959, result.GreatestCommonDivisor); var actualExpression = result.FirstFactor * result.FirstItem + result.SecondFactor * result.SecondItem; Assert.AreEqual(result.GreatestCommonDivisor, actualExpression); actualExpression = result.GreatestCommonDivisor * result.FirstCofactor; Assert.AreEqual(result.FirstItem, actualExpression); actualExpression = result.GreatestCommonDivisor * result.SecondCofactor; Assert.AreEqual(result.SecondItem, actualExpression); }
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); }
public void Statistcs_ListGeneralizeMeanBlockAlgorithmTest() { var target = new ListGeneralizedMeanAlgorithm <int, double>( i => i, d => d, (d, i) => d / i, new DoubleField()); var blockNumber = 2500; var integerSequence = new List <int>(); for (var i = 1; i < 5500; ++i) { integerSequence.Add(i); var expected = (i + 1) / 2.0; var actual = target.Run <double>( integerSequence, blockNumber, (j, k) => j / (double)k, (d1, d2) => d1 * d2); Assert.IsTrue(Math.Abs(expected - actual) < 0.001); } var n = 1000500; for (var i = 5500; i <= n; ++i) { integerSequence.Add(i); } var outerExpected = (n + 1) / 2.0; var outerActual = target.Run <double>( integerSequence, blockNumber, (j, k) => j / (double)k, (d1, d2) => d1 * d2); Assert.AreEqual(outerExpected, outerActual); var integerDomain = new BigIntegerDomain(); var fractionField = new FractionField <BigInteger>(integerDomain); var fracTarget = new ListGeneralizedMeanAlgorithm <int, Fraction <BigInteger> >( i => new Fraction <BigInteger>(i, 1, integerDomain), d => d, (d, i) => d.Divide(i, integerDomain), fractionField); var fractionExpected = new Fraction <BigInteger>(n + 1, 2, integerDomain); var fractionActual = fracTarget.Run <Fraction <BigInteger> >( integerSequence, blockNumber, (j, k) => new Fraction <BigInteger>(j, k, integerDomain), (d1, d2) => d1.Multiply(d2, integerDomain)); Assert.AreEqual(fractionExpected, fractionActual); // Teste com alteração da função directa fracTarget.DirectFunction = i => new Fraction <BigInteger>(new BigInteger(i) * i, 1, integerDomain); fractionExpected = new Fraction <BigInteger>( (new BigInteger(n) + BigInteger.One) * (2 * new BigInteger(n) + 1), 6, integerDomain); fractionActual = fracTarget.Run <Fraction <BigInteger> >( integerSequence, blockNumber, (j, k) => new Fraction <BigInteger>(j, k, integerDomain), (d1, d2) => d1.Multiply(d2, integerDomain)); // Teste com transformação var transformedTarget = new ListGeneralizedMeanAlgorithm <BigInteger, Fraction <BigInteger> >( i => new Fraction <BigInteger>(i, 1, integerDomain), d => d, (d, i) => d.Divide(i, integerDomain), fractionField); var transformedSeq = new TransformList <int, BigInteger>( integerSequence, i => new BigInteger(i) * i); var transformedExpected = new Fraction <BigInteger>( (new BigInteger(n) + BigInteger.One) * (2 * new BigInteger(n) + 1), 6, integerDomain); var transformedActual = transformedTarget.Run <Fraction <BigInteger> >( transformedSeq, blockNumber, (j, k) => new Fraction <BigInteger>(j, k, integerDomain), (d1, d2) => d1.Multiply(d2, integerDomain)); Assert.AreEqual(transformedExpected, transformedActual); }
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); } }
public void Statistcs_EnumGeneralizeMeanAlgorithmTest() { var integerNumb = new IntegerDomain(); var target = new EnumGeneralizedMeanAlgorithm <int, double, int>( i => i, d => d, (d, i) => d / i, new DoubleField(), integerNumb); var integerSequence = new IntegerSequence(); for (var i = 1; i < 5000; ++i) { integerSequence.Add(i); var expected = (i + 1) / 2.0; var actual = target.Run(integerSequence); Assert.AreEqual(expected, actual); } integerSequence = new IntegerSequence(); var n = 1000000; integerSequence.Add(1, n); var outerExpected = (n + 1) / 2.0; var outerActual = target.Run(integerSequence); Assert.AreEqual(outerExpected, outerActual); var bigIntegerDomain = new BigIntegerDomain(); var fractionField = new FractionField <BigInteger>(bigIntegerDomain); var fractionTarget = new EnumGeneralizedMeanAlgorithm <int, Fraction <BigInteger>, int>( i => new Fraction <BigInteger>(i, 1, bigIntegerDomain), d => d, (d, i) => d.Divide(i, bigIntegerDomain), fractionField, integerNumb); var fractionExpected = new Fraction <BigInteger>(n + 1, 2, bigIntegerDomain); var fractionActual = fractionTarget.Run(integerSequence); Assert.AreEqual(fractionExpected, fractionActual); // Teste com alteração da função directa fractionTarget.DirectFunction = i => new Fraction <BigInteger>(new BigInteger(i) * i, 1, bigIntegerDomain); fractionExpected = new Fraction <BigInteger>( (new BigInteger(n) + BigInteger.One) * (2 * new BigInteger(n) + 1), 6, bigIntegerDomain); fractionActual = fractionTarget.Run(integerSequence); Assert.AreEqual(fractionExpected, fractionActual); // Teste com transformação var transformedTarget = new EnumGeneralizedMeanAlgorithm <BigInteger, Fraction <BigInteger>, int>( i => new Fraction <BigInteger>(i, 1, bigIntegerDomain), d => d, (d, i) => d.Divide(i, bigIntegerDomain), fractionField, integerNumb); var transformedSeq = new TransformEnumerable <int, BigInteger>( integerSequence, i => new BigInteger(i) * i); var transformedExpected = new Fraction <BigInteger>( (new BigInteger(n) + BigInteger.One) * (2 * new BigInteger(n) + 1), 6, bigIntegerDomain); var transformedActual = transformedTarget.Run(transformedSeq); Assert.AreEqual(transformedExpected, transformedActual); }
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); } }
public void GetPolynomialDerivativeTest_IntegerMatrix() { // Os valores a serem lidos var polynomialText = "[[1,2],[3,4]]*x^2-[[1,0],[0,1]]*x+[[7,6],[9,8]]"; var polynomialDerivativeText = "[[2,4],[6,8]]*x+[[-1,0],[0,-1]]"; var variableName = "x"; var arrayDelimiters = new Dictionary <string, string>(); arrayDelimiters.Add("left_bracket", "right_bracket"); // Os domínios responsáveis sobre as 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>(); // As fábricas responsáveis pela instanciação de matrizes var integerSquareArrayMatrixfactory = new ArraySquareMatrixFactory <int>(); var longSquareArrayMatrixFactory = new ArraySquareMatrixFactory <long>(); var bigIntegerSquareArrayMatrixfactory = new ArraySquareMatrixFactory <BigInteger>(); // Os anéis de matrizes var integerGenericMatrixRing = new GeneralMatrixRing <int>( 2, integerSquareArrayMatrixfactory, integerDomain); var longGenericMatrixRing = new GeneralMatrixRing <long>( 2, longSquareArrayMatrixFactory, longDomain); var bigIntegerGenericMatrixRing = new GeneralMatrixRing <BigInteger>( 2, bigIntegerSquareArrayMatrixfactory, bigIntegerDomain); // Os objectos responsáveis pela conversão entre os coeficientes e o grau (inteiro) var integerMatrixConversion = new CantConvertConversion <int, IMatrix <int> >(); var longMatrixConversion = new CantConvertConversion <int, IMatrix <long> >(); var bigIntegerMatrixConversion = new CantConvertConversion <int, IMatrix <BigInteger> >(); var integerMatrixConfigParser = new ConfigMatrixParser <int, IMatrix <int> >( integerParser, 2, 2, (i, j) => integerSquareArrayMatrixfactory.CreateMatrix(i, j)); integerMatrixConfigParser.SeparatorSymbType = "comma"; integerMatrixConfigParser.MapInternalDelimiters("left_bracket", "right_bracket"); integerMatrixConfigParser.AddBlanckSymbolType("blancks"); var longMatrixConfigParser = new ConfigMatrixParser <long, IMatrix <long> >( longParser, 2, 2, (i, j) => longSquareArrayMatrixFactory.CreateMatrix(i, j)); longMatrixConfigParser.SeparatorSymbType = "comma"; longMatrixConfigParser.MapInternalDelimiters("left_bracket", "right_bracket"); longMatrixConfigParser.AddBlanckSymbolType("blancks"); var bigIntegerMatrixConfigParser = new ConfigMatrixParser <BigInteger, IMatrix <BigInteger> >( bigIntegerParser, 2, 2, (i, j) => bigIntegerSquareArrayMatrixfactory.CreateMatrix(i, j)); bigIntegerMatrixConfigParser.SeparatorSymbType = "comma"; bigIntegerMatrixConfigParser.MapInternalDelimiters("left_bracket", "right_bracket"); bigIntegerMatrixConfigParser.AddBlanckSymbolType("blancks"); // Leitura dos polinómios e subsequente teste. var integerPolynomial = TestsHelper.ReadUnivarPolynomial <IMatrix <int> >( polynomialText, integerGenericMatrixRing, integerMatrixConfigParser, integerMatrixConversion, variableName, arrayDelimiters); var integerExpectedDerivative = TestsHelper.ReadUnivarPolynomial( polynomialDerivativeText, integerGenericMatrixRing, integerMatrixConfigParser, integerMatrixConversion, variableName, arrayDelimiters, true); var integerActualDerivative = integerPolynomial.GetPolynomialDerivative(integerGenericMatrixRing); Assert.IsTrue( new UnivarPolynomNormalFormEqualityComparer <IMatrix <int> >(integerGenericMatrixRing).Equals(integerExpectedDerivative, integerActualDerivative), string.Format("Expected {0} isn't equal to actual {1}.", integerExpectedDerivative, integerActualDerivative)); var longPolynomial = TestsHelper.ReadUnivarPolynomial( polynomialText, longGenericMatrixRing, longMatrixConfigParser, longMatrixConversion, variableName, arrayDelimiters); var longExpectedDerivative = TestsHelper.ReadUnivarPolynomial( polynomialDerivativeText, longGenericMatrixRing, longMatrixConfigParser, longMatrixConversion, variableName, arrayDelimiters, true); var longActualDerivative = longPolynomial.GetPolynomialDerivative(longGenericMatrixRing); Assert.IsTrue( new UnivarPolynomNormalFormEqualityComparer <IMatrix <long> >(longGenericMatrixRing).Equals(longExpectedDerivative, longActualDerivative), string.Format("Expected {0} isn't equal to actual {1}.", integerExpectedDerivative, integerActualDerivative)); var bigIntegerPolynomial = TestsHelper.ReadUnivarPolynomial( polynomialText, bigIntegerGenericMatrixRing, bigIntegerMatrixConfigParser, bigIntegerMatrixConversion, variableName, arrayDelimiters); var bigIntegerExpectedDerivative = TestsHelper.ReadUnivarPolynomial( polynomialDerivativeText, bigIntegerGenericMatrixRing, bigIntegerMatrixConfigParser, bigIntegerMatrixConversion, variableName, arrayDelimiters, true); var bigIntegerActualDerivative = bigIntegerPolynomial.GetPolynomialDerivative(bigIntegerGenericMatrixRing); Assert.IsTrue( new UnivarPolynomNormalFormEqualityComparer <IMatrix <BigInteger> >( bigIntegerGenericMatrixRing).Equals(bigIntegerExpectedDerivative, bigIntegerActualDerivative), string.Format("Expected {0} isn't equal to actual {1}.", integerExpectedDerivative, integerActualDerivative)); }
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); }
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); }
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); }
public void PowerTest_BigIntegerFraction() { var domain = new BigIntegerDomain(); var multiplier = new FractionField <BigInteger>(domain); var intPowers = new int[] { 2, 3, 4, 1, 20, 13 }; var longPowers = new long[] { 2, 3, 4, 1, 20, 13 }; var bigIntPowers = new BigInteger[] { 2, 3, 4, 1, 20, 13 }; var values = new[] { new Fraction <BigInteger>(13, 17, domain), new Fraction <BigInteger>(25, 23, domain), new Fraction <BigInteger>(11, 15, domain), new Fraction <BigInteger>(100, 99, domain), new Fraction <BigInteger>(1, 2, domain), new Fraction <BigInteger>(3, 5, domain) }; var expected = new[] { new Fraction <BigInteger>(169, 289, domain), new Fraction <BigInteger>(15625, 12167, domain), new Fraction <BigInteger>(14641, 50625, domain), new Fraction <BigInteger>(100, 99, domain), new Fraction <BigInteger>(1, 1048576, domain), new Fraction <BigInteger>(1594323, 1220703125, domain) }; // Potências inteiras. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], intPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências longas for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], longPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências com inteiros grandes. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], bigIntPowers[i], multiplier); Assert.AreEqual(expected[i], actual); } // Potências genéricas. var integerNumber = new IntegerDomain(); var longNumber = new LongDomain(); var bigIntegerNumber = new BigIntegerDomain(); // Potências inteiras. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], intPowers[i], multiplier, integerNumber); Assert.AreEqual(expected[i], actual); } // Potências longas for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], longPowers[i], multiplier, longNumber); Assert.AreEqual(expected[i], actual); } // Potências com inteiros grandes. for (int i = 0; i < intPowers.Length; ++i) { var actual = MathFunctions.Power(values[i], bigIntPowers[i], multiplier, bigIntegerNumber); Assert.AreEqual(expected[i], actual); } }