public static Value ConvertStringToNumber(Value source) { if (source.IsEmpty) { return(0); } var sourceText = source.Text; Parser parser = new FloatParser(); var coloring = Parser.Coloring; Parser.Coloring = false; if (parser.Scan(sourceText, 0)) { Parser.Coloring = coloring; return(parser.Result.Value); } parser = new IntegerParser(); if (parser.Scan(sourceText, 0)) { Parser.Coloring = coloring; return(parser.Result.Value); } Parser.Coloring = coloring; return(0); }
public void IntegerParserCreationTest5() { int minValue = 255; int maxValue = 0; IntegerParser parser = new IntegerParser(minValue, maxValue); }
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); }
public void ReplaceTest_Integer() { // Representação dos polinómios. var polynomText = "x^5+2*x^4+3*x^3+4*x^2+5*x+6"; var variableName = "x"; // Estabelece os domínios. var integerDomain = new IntegerDomain(); // Estabelece os conversores. var integerToIntegerConv = new ElementToElementConversion <int>(); // Estabelece os leitores individuais. var integerParser = new IntegerParser <string>(); // Estabelece os polinómios. var integerPolynomial = TestsHelper.ReadUnivarPolynomial( polynomText, integerDomain, integerParser, integerToIntegerConv, variableName); var integerReplaceValues = new int[] { 0, 1, 2, 3 }; var integerExpectedValues = new int[] { 6, 21, 120, 543 }; for (int i = 0; i < integerReplaceValues.Length; ++i) { var integerActualValue = integerPolynomial.Replace(integerReplaceValues[i], integerDomain); Assert.AreEqual(integerExpectedValues[i], integerActualValue); } }
public void RunTest_IntegerNumbersRhoAlg() { var integerNumber = new IntegerDomain(); var integerParser = new IntegerParser <string>(); var conversion = new ElementToElementConversion <int>(); var variableName = "x"; var testPols = new List <UnivariatePolynomialNormalForm <int> >(); testPols.Add(TestsHelper.ReadUnivarPolynomial("x^2+1", integerNumber, integerParser, conversion, variableName)); testPols.Add(TestsHelper.ReadUnivarPolynomial("x^2+x+1", integerNumber, integerParser, conversion, variableName)); var rhoAlgorithm = new PollardRhoAlgorithm <int>( testPols, new ModularIntegerFieldFactory(), integerNumber); var factorizationTarget = new DecompositionFactorizationAlgorithm <int, int>( rhoAlgorithm, 1, integerNumber, integerNumber); var value = 72; var expected = new Dictionary <int, int>(); expected.Add(2, 3); expected.Add(3, 2); var actual = factorizationTarget.Run(value); CollectionAssert.AreEqual(expected, actual); }
public void IntegerParserParsingTest3() { string testValue = "45abc"; IntegerParser parser = new IntegerParser(); parser.GetValue(testValue); }
/// <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 <int> Read(string polynomial) { var integerDomain = new IntegerDomain(); var integerParser = new IntegerParser <string>(); var conversion = new ElementToElementConversion <int>(); var polInputReader = new StringReader(polynomial); var polSymbolReader = new StringSymbolReader(polInputReader, false); var polParser = new UnivariatePolynomialReader <int, CharSymbolReader <string> >( "x", integerParser, integerDomain); var result = default(UnivariatePolynomialNormalForm <int>); 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 TestIntegerParser() { var parser = new IntegerParser(); Assert.Equal(123, parser.ParseOrDefault("123")); Assert.Equal(234, parser.ParseOrFallback("", 234)); Assert.Equal(567, parser.ParseOrFallback("wrong input", 567)); }
public void IntegerParserValidationTest1() { string testValue = "45"; IntegerParser parser = new IntegerParser(); ValueValidationResult validationResult = parser.ValidateValue(testValue); Assert.AreEqual(ValueValidationResult.OK, validationResult); }
public void IntegerParserValidationTest3() { string testValue = "error"; IntegerParser parser = new IntegerParser(); ValueValidationResult validationResult = parser.ValidateValue(testValue); Assert.AreEqual(ValueValidationResult.TypeError, validationResult); }
public void IntegerParserParsingTest1() { string testValue = "45"; IntegerParser parser = new IntegerParser(); int parsedValue = (int)parser.GetValue(testValue); Assert.AreEqual(45, parsedValue); }
public void TestIntArrayParser() { var parser = new IntegerParser(); Assert.Equal(new [] { 1, 2, 3 }, parser.ParseToArray("1,2,3")); Assert.Equal(new [] { 4, 5, 6 }, parser.ParseToArray("4, 5,6")); Assert.Equal(new [] { 7, 8, 9 }, parser.ParseToArrayOrFallback("", new [] { 7, 8, 9 }).ToArray()); Assert.Null(parser.ParseToArrayOrNull("")); Assert.Null(parser.ParseToArrayOrNull("1,2,3,kill")); }
public void IntegerParserParsingTest4() { string testValue = "256"; int minValue = 0; int maxValue = 255; IntegerParser parser = new IntegerParser(minValue, maxValue); parser.GetValue(testValue); }
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); }
public void IntegerParserParsingTest2() { string testValue = "45"; int minValue = 0; int maxValue = 255; IntegerParser parser = new IntegerParser(minValue, maxValue); int parsedValue = (int)parser.GetValue(testValue); Assert.AreEqual(45, parsedValue); }
public void IntegerParserValidationTest4() { string testValue = "256"; int minValue = 0; int maxValue = 255; IntegerParser parser = new IntegerParser(minValue, maxValue); ValueValidationResult validationResult = parser.ValidateValue(testValue); Assert.AreEqual(ValueValidationResult.RangeError, validationResult); }
public void ReplaceTest_ReplaceByFraction() { // Representação dos polinómios. var polynomText = "x^5+2*x^4+3*x^3+4*x^2+5*x+6"; var variableName = "x"; // Estabelece os domínios. var integerDomain = new IntegerDomain(); // Estabelece os conversores. var integerToIntegerConv = new ElementToElementConversion <int>(); // Estabelece os leitores individuais. var integerParser = new IntegerParser <string>(); var fractionField = new FractionField <int>(integerDomain); var integerFractionAddOp = new ElementFractionAddOper <int>(integerDomain); // Estabelece os polinómios. var integerPolynomial = TestsHelper.ReadUnivarPolynomial( polynomText, integerDomain, integerParser, integerToIntegerConv, variableName); var fractionValues = new Fraction <int>[] { new Fraction <int>(0, 1, integerDomain), new Fraction <int>(1, 1, integerDomain), new Fraction <int>(1, 2, integerDomain), new Fraction <int>(1, 3, integerDomain) }; var fractionExpectedValues = new Fraction <int>[] { new Fraction <int>(6, 1, integerDomain), new Fraction <int>(21, 1, integerDomain), new Fraction <int>(321, 32, integerDomain), new Fraction <int>(2005, 243, integerDomain) }; for (int i = 0; i < fractionValues.Length; ++i) { var integerActualValue = integerPolynomial.Replace( fractionValues[i], integerFractionAddOp, fractionField); Assert.AreEqual(fractionExpectedValues[i], integerActualValue); } }
public void GetRootPowerSumsTest_IntegerFraction() { // Representação dos polinómios. var polynomText = "(x-3)*(x-2)^2*(x+1)^3"; var variableName = "x"; // Estabelece os domínios. var integerDomain = new IntegerDomain(); // Estabelece o corpo responsável pelas operações sobre as fracções. var fractionField = new FractionField <int>(integerDomain); // Estabelece os conversores. var integerToFractionConversion = new ElementFractionConversion <int>(integerDomain); // Estabelece os leitores individuais. var integerParser = new IntegerParser <string>(); // Estabelece o leitor de fracções. var fractionParser = new ElementFractionParser <int>(integerParser, integerDomain); // Estabelece os polinómios. var integerPolynomial = TestsHelper.ReadUnivarPolynomial( polynomText, fractionField, fractionParser, integerToFractionConversion, variableName); var integerFractionExpectedVector = new ArrayVector <Fraction <int> >(6); integerFractionExpectedVector[0] = new Fraction <int>(4, 1, integerDomain); integerFractionExpectedVector[1] = new Fraction <int>(20, 1, integerDomain); integerFractionExpectedVector[2] = new Fraction <int>(40, 1, integerDomain); integerFractionExpectedVector[3] = new Fraction <int>(116, 1, integerDomain); integerFractionExpectedVector[4] = new Fraction <int>(304, 1, integerDomain); integerFractionExpectedVector[5] = new Fraction <int>(860, 1, integerDomain); var integerFractionActualVector = integerPolynomial.GetRootPowerSums( fractionField, new SparseDictionaryMathVectorFactory <Fraction <int> >()); Assert.AreEqual( integerFractionExpectedVector.Length, integerFractionActualVector.Length, "Vector lengths aren't equal."); for (int i = 0; i < integerFractionActualVector.Length; ++i) { Assert.AreEqual(integerFractionExpectedVector[i], integerFractionActualVector[i]); } }
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 mainPolText = "x^3+10*x^2-432*x+5040"; var firstFactorText = "x"; var secondFactorText = "x^2-2"; var variableName = "x"; var prime = 5; var integerDomain = new IntegerDomain(); var integerParser = new IntegerParser <string>(); var integerConversion = new ElementToElementConversion <int>(); var mainPol = TestsHelper.ReadUnivarPolynomial( mainPolText, integerDomain, integerParser, integerConversion, variableName); var firstFactor = TestsHelper.ReadUnivarPolynomial( firstFactorText, integerDomain, integerParser, integerConversion, variableName); var secondFactor = TestsHelper.ReadUnivarPolynomial( secondFactorText, integerDomain, integerParser, integerConversion, variableName); // Testa o levantamento linear. var linearLift = new LinearLiftAlgorithm <int>( new ModularSymmetricIntFieldFactory(), new UnivarPolEuclideanDomainFactory <int>(), integerDomain); var liftingStatus = new LinearLiftingStatus <int>(mainPol, firstFactor, secondFactor, prime); var result = linearLift.Run(liftingStatus, 3); Assert.AreEqual(625, liftingStatus.LiftedFactorizationModule); var expected = liftingStatus.UFactor.Multiply(liftingStatus.WFactor, new ModularIntegerField(625)); var actual = mainPol.ApplyFunction(coeff => this.GetSymmetricRemainder(coeff, 625), integerDomain); Assert.AreEqual(expected, actual); }
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]); } }
public void RunTest_TestFactors2() { var polText = "x^3+2"; var variableName = "x"; var prime = 5; var integerDomain = new IntegerDomain(); var integerParser = new IntegerParser <string>(); var integerConversion = new ElementToElementConversion <int>(); // Faz a leitura do polinómio. var pol = TestsHelper.ReadUnivarPolynomial( polText, integerDomain, integerParser, integerConversion, variableName); // Testa os factores. var integerModule = new ModularIntegerField(prime); var finiteFieldPolAlg = new FiniteFieldPolFactorizationAlgorithm <int>( new DenseCondensationLinSysAlgorithm <int>(integerModule), integerDomain); var result = finiteFieldPolAlg.Run(pol, integerModule); var factorsEnumerator = result.Factors.GetEnumerator(); if (factorsEnumerator.MoveNext()) { var expected = factorsEnumerator.Current; while (factorsEnumerator.MoveNext()) { expected = expected.Multiply(factorsEnumerator.Current, integerModule); } expected = expected.Multiply(result.IndependentCoeff, integerModule); Assert.AreEqual(expected, pol.ApplyFunction(coeff => this.GetSymmetricRemainder(coeff, prime), integerModule)); } else { Assert.Fail("At least the main polynomial may be regarded as a factor."); } }
public static Version GetVersion() { var cli = new CliRunner("cmd.exe", "/C ver"); var output = cli.ReadToEnd(); const string pattern = @"(?<major>\d+)(\.(?<minor>\d+)(\.(?<build>\d+))?)?"; var m = Regex.Match(output, pattern); var major = 0; var minor = 0; var build = 0; if (m.Success) { var parser = new IntegerParser(); major = parser.ParseOrFallback(m.Groups["major"].Value, 1); minor = parser.ParseOrFallback(m.Groups["minor"].Value, 0); build = parser.ParseOrFallback(m.Groups["build"].Value, 0); } return(new Version(major, minor, build)); }
public void RunTest_IntegerMatrix() { // A leitura é realizada por colunas. var matrixText = "[[1,-1,2], [3,4,5], [2,1,1]]"; var integerDomain = new IntegerDomain(); var variableName = "x"; var integerParser = new IntegerParser <string>(); var conversion = new ElementToElementConversion <int>(); var matrix = TestsHelper.ReadMatrix <int>( 3, 3, matrixText, (i, j) => new ArraySquareMathMatrix <int>(i), integerParser, true); var fastDivFreeCharacPolAlg = new FastDivisionFreeCharPolynomCalculator <int>(variableName, integerDomain); var expected = TestsHelper.ReadUnivarPolynomial("x^3-6*x^2+3*x+18", integerDomain, integerParser, conversion, variableName); var actual = fastDivFreeCharacPolAlg.Run(matrix as ISquareMathMatrix <int>); Assert.AreEqual(expected, actual); }
public void RunTest_TabularItem() { var csvStringBuilder = new StringBuilder(); csvStringBuilder.AppendLine("12, 9,7,6,4"); csvStringBuilder.AppendLine("12,11,8,7,5"); csvStringBuilder.AppendLine("13,10,9,6,3"); csvStringBuilder.Append("12,8,6,4,2"); var csvString = csvStringBuilder.ToString(); var csvStringReader = new StringReader(csvString); var parser = new IntegerParser <string>(); var csvParser = new CsvFileParser <ITabularItem, int, string, string>( "new_line", "comma", (i, j) => parser); csvParser.AddIgnoreType("space"); csvParser.AddIgnoreType("carriage_return"); var csvSymbolReader = new StringSymbolReader(csvStringReader, true, false); var adder = new TabularItemAdder <int>(); var tabularItem = new TabularListsItem(); csvParser.Parse(csvSymbolReader, tabularItem, adder); var integerDomain = new IntegerDomain(); var numberTdecomposition = new IntegerMinWeightTdecomposition <int>( Comparer <int> .Default, integerDomain); var result = numberTdecomposition.Run(18, tabularItem); var expectedMedians = new List <int>() { 3, 5, 5, 5 }; Assert.AreEqual(17, result.Cost); CollectionAssert.AreEquivalent(expectedMedians, result.Medians.ToList()); }
public void GetRootPowerSumsTest_Integer() { // Representação dos polinómios. var polynomText = "(x-3)*(x-2)^2*(x+1)^3"; var variableName = "x"; // Estabelece os domínios. var integerDomain = new IntegerDomain(); // Estabelece os conversores. var integerToIntegerConv = new ElementToElementConversion <int>(); // Estabelece os leitores individuais. var integerParser = new IntegerParser <string>(); // Estabelece os polinómios. var integerPolynomial = TestsHelper.ReadUnivarPolynomial( polynomText, integerDomain, integerParser, integerToIntegerConv, variableName); var integerExpectedVector = new ArrayVector <int>(6); integerExpectedVector[0] = 4; integerExpectedVector[1] = 20; integerExpectedVector[2] = 40; integerExpectedVector[3] = 116; integerExpectedVector[4] = 304; integerExpectedVector[5] = 860; var integerActualVector = integerPolynomial.GetRootPowerSums(integerDomain); Assert.AreEqual(integerExpectedVector.Length, integerActualVector.Length, "Vector lengths aren't equal."); for (int i = 0; i < integerActualVector.Length; ++i) { Assert.AreEqual(integerExpectedVector[i], integerActualVector[i]); } }
public void GetElementarySymmetricRepresentationTest() { var domain = new IntegerDomain(); var coeffsParser = new IntegerParser <string>(); var conversion = new ElementToElementConversion <int>(); var dictionary = new Dictionary <int, int>(); dictionary.Add(5, 2); dictionary.Add(0, 2); var varDictionary = new Dictionary <int, Tuple <bool, string, int> >(); varDictionary.Add(1, Tuple.Create(true, "s[1]", 0)); var symmetric = new SymmetricPolynomial <int>( new List <string>() { "x", "y", "z", "w" }, dictionary, 1, domain); var rep = symmetric.GetElementarySymmetricRepresentation(varDictionary, new IntegerDomain()); var expanded = rep.GetExpanded(domain); var expectedPolText = "5*s4^2*s2+-5*s4*s2^3+5*s4*s3^2+5*s2^2*s3^2+1*s2^5"; var expected = TestsHelper.ReadPolynomial( expectedPolText, domain, conversion, coeffsParser); Assert.AreEqual(expected, expanded); }
public NumberEnumerationNameParser() : base($"^ /(/s*) /({REGEX_VARIABLE}) /(/s* '=' /s*)") { integerParser = new IntegerParser(); hexParser = new HexParser(); }
public void BasicTest(int expected, string input) { var sut = new IntegerParser(); Assert.Equal(expected, sut.ParseOrDefault(input)); }
public void Init() { parser = new IntegerParser(); }