/// <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."); } }
/// <summary> /// Permit realizar a leitura de um polinómio com coeficientes fraccionários. /// </summary> /// <typeparam name="T">O tipo de dados dos componentes das fracções.</typeparam> /// <param name="polynomialRepresentation">A representação polinomial.</param> /// <param name="domain">O domínio responsável pelas operações sobre os elementos das fracções.</param> /// <param name="itemsParser">O leitor de elementos da fracção.</param> /// <param name="conversion">A conversão entre cada fracção e o valor inteiro.</param> /// <param name="variableName">O nome da variável.</param> /// <param name="readNegativeNumbers">Indica se são lidos os números negativos.</param> /// <returns>O polinómio lido.</returns> public static UnivariatePolynomialNormalForm <Fraction <T> > ReadFractionalCoeffsUnivarPol <T, D>( string polynomialRepresentation, D domain, IParse <T, string, string> itemsParser, IConversion <int, Fraction <T> > conversion, string variableName, bool readNegativeNumbers = false) where D : IEuclidenDomain <T> { var fractionField = new FractionField <T>(domain); var fractionParser = new FieldDrivenExpressionParser <Fraction <T> >( new SimpleElementFractionParser <T>(itemsParser, domain), fractionField); var polInputReader = new StringReader(polynomialRepresentation); var polSymbolReader = new StringSymbolReader(polInputReader, readNegativeNumbers); var polParser = new UnivariatePolynomialReader <Fraction <T>, CharSymbolReader <string> >( "x", fractionParser, fractionField); var result = default(UnivariatePolynomialNormalForm <Fraction <T> >); 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 polynomial."); } }
public void ReplaceTest_ReplaceByMatrixWithMatrixAlgebra() { // Representação dos polinómios. var polynomText = "x^2 + 2*x + 1"; var variableName = "x"; var integerDomain = new IntegerDomain(); var integerToIntegerConv = new ElementToElementConversion <int>(); var integerParser = new IntegerParser <string>(); var fractionField = new FractionField <int>(integerDomain); var fractionFieldParser = new FieldDrivenExpressionParser <Fraction <int> >( new SimpleElementFractionParser <int>(integerParser, integerDomain), fractionField); var polynomial = TestsHelper.ReadUnivarPolynomial <Fraction <int> >( polynomText, fractionField, fractionFieldParser, new ElementFractionConversion <int>(integerDomain), variableName); // Leitura da matriz. var matrix = TestsHelper.ReadMatrix <Fraction <int> >( 2, 2, "[[1/2+1/3,1/2-1/3],[1/5+1/4,1/5-1/4]]", (i, j) => new ArrayMathMatrix <Fraction <int> >(i, j), fractionFieldParser); var matrixAlgebra = new GeneralMatrixAlgebra <Fraction <int> >( 2, new ArrayMathMatrixFactory <Fraction <int> >(), fractionField); var actual = polynomial.Replace(matrix, matrixAlgebra); var expected = TestsHelper.ReadMatrix <Fraction <int> >( 2, 2, "[[1237/360,167/360],[501/400,391/400]]", (i, j) => new ArrayMathMatrix <Fraction <int> >(i, j), fractionFieldParser); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { Assert.AreEqual(expected[i, j], actual[i, j]); } } }
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 RunTest() { var coefficientsMatrixText = "[[1,0,0],[0,0,0],[0,3,3],[2,0,1]]"; var independentVectorText = "[[1,3,3]]"; var expectedText = "[[1,0,1,0]]"; var integerDomain = new IntegerDomain(); var integerParser = new IntegerParser <string>(); var fractionField = new FractionField <int>(integerDomain); var fractionFieldParser = new FieldDrivenExpressionParser <Fraction <int> >( new SimpleElementFractionParser <int>(integerParser, integerDomain), fractionField); var matrixFactory = new ArrayMatrixFactory <Fraction <int> >(); // Leitura da matriz que representa o sistema de equações. var coeffsMatrix = TestsHelper.ReadMatrix <Fraction <int> >( 3, 4, coefficientsMatrixText, matrixFactory, fractionFieldParser); // Leitura do vector de termos independente. var vectorMatrix = TestsHelper.ReadMatrix <Fraction <int> >( 3, 1, independentVectorText, new ArrayMatrixFactory <Fraction <int> >(), fractionFieldParser); var expectedMatrixVector = TestsHelper.ReadMatrix <Fraction <int> >( 4, 1, expectedText, matrixFactory, fractionFieldParser); var algorithm = new DenseCondensationLinSysAlgorithm <Fraction <int> >(fractionField); var actual = algorithm.Run(coeffsMatrix, vectorMatrix); Assert.AreEqual(expectedMatrixVector.GetLength(0), actual.Vector.Length); for (int i = 0; i < actual.Vector.Length; ++i) { Assert.AreEqual(expectedMatrixVector[i, 0], actual.Vector[i]); } }