Search in sources :

Example 11 with Polynomial

use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.

the class DefaultPolynomialTest method testGet.

@Test
public void testGet() {
    Polynomial p = makePolynomial("x^2*y^3", "(x, y)");
    assertEquals(Expressions.makeSymbol("*"), p.get(-1));
    assertEquals(parse("x^2"), p.get(0));
    assertEquals(parse("y^3"), p.get(1));
    p = makePolynomial("x^2*y^3 + 10", "(x, y)");
    assertEquals(Expressions.makeSymbol("+"), p.get(-1));
    assertEquals(parse("x^2*y^3"), p.get(0));
    assertEquals(parse("10"), p.get(1));
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Test(org.junit.Test)

Example 12 with Polynomial

use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.

the class DefaultPolynomialTest method testGetMapFromSignatureToMonomial.

@Test
public void testGetMapFromSignatureToMonomial() {
    Polynomial p = makePolynomial("3*x^2*y^4", "tuple()");
    Map<List<Rational>, Expression> expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Collections.emptyList(), makeMonomial("3*x^2*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(x)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(2)), parse("x^2*3*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(y)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(4)), parse("y^4*3*x^2"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(x, y)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(2), new Rational(4)), parse("3*x^2*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(y, x)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(4), new Rational(2)), parse("3*x^2*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(x, y, z)");
    expectedMapFromSignatureToMonomial = new HashMap<>();
    expectedMapFromSignatureToMonomial.put(Arrays.asList(new Rational(2), new Rational(4), new Rational(0)), parse("3*x^2*y^4"));
    assertEquals(expectedMapFromSignatureToMonomial, p.getMapFromSignatureToMonomial());
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) HashMap(java.util.HashMap) List(java.util.List) Test(org.junit.Test)

Example 13 with Polynomial

use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.

the class DefaultPolynomialTest method testUnivariateDivide.

@Test
public void testUnivariateDivide() {
    Polynomial dividend = makePolynomial("0", "tuple(x)");
    Polynomial divisor = makePolynomial("x^2 + 3", "tuple(x)");
    Pair<Polynomial, Polynomial> quotientAndRemainder = dividend.divide(divisor);
    assertEquals(parse("0"), quotientAndRemainder.first);
    assertEquals(parse("0"), quotientAndRemainder.second);
    dividend = makePolynomial("3", "tuple(x)");
    divisor = makePolynomial("2", "tuple(x)");
    quotientAndRemainder = dividend.divide(divisor);
    assertEquals(parse("1.5"), quotientAndRemainder.first);
    assertEquals(parse("0"), quotientAndRemainder.second);
    dividend = makePolynomial("2*x^2 + 3*x + 6", "tuple(x)");
    divisor = makePolynomial("2", "tuple(x)");
    quotientAndRemainder = dividend.divide(divisor);
    assertEquals(parse("x^2 + 1.5*x + 3"), quotientAndRemainder.first);
    assertEquals(parse("0"), quotientAndRemainder.second);
    dividend = makePolynomial("x^3 - 5*x^2 + 3*x - 15", "tuple(x)");
    divisor = makePolynomial("x^2 + 3", "tuple(x)");
    quotientAndRemainder = dividend.divide(divisor);
    assertEquals(parse("x - 5"), quotientAndRemainder.first);
    assertEquals(parse("0"), quotientAndRemainder.second);
    dividend = makePolynomial("x^3 - 1", "tuple(x)");
    divisor = makePolynomial("x + 2", "tuple(x)");
    quotientAndRemainder = dividend.divide(divisor);
    assertEquals(parse("x^2 -2*x + 4"), quotientAndRemainder.first);
    assertEquals(parse("-9"), quotientAndRemainder.second);
    dividend = makePolynomial("3*x^3 - 2*x^2 + 4*x - 3", "tuple(x)");
    divisor = makePolynomial("x^2 + 3*x + 3", "tuple(x)");
    quotientAndRemainder = dividend.divide(divisor);
    assertEquals(parse("3*x - 11"), quotientAndRemainder.first);
    assertEquals(parse("28*x + 30"), quotientAndRemainder.second);
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Test(org.junit.Test)

Example 14 with Polynomial

use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.

the class DefaultPolynomialTest method testGetFunctor.

// 
// FunctionaApplication API related tests
@Test
public void testGetFunctor() {
    Polynomial p = makePolynomial("0", "(x, y)");
    Assert.assertNull(p.getFunctor());
    p = makePolynomial("z^4*y^2*x^3", "(x, y)");
    assertEquals(Expressions.makeSymbol("*"), p.getFunctor());
    p = makePolynomial("z^4*y^2*x^3 + 10", "(x, y)");
    assertEquals(Expressions.makeSymbol("+"), p.getFunctor());
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Test(org.junit.Test)

Example 15 with Polynomial

use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.

the class DefaultPolynomialTest method testGetVariables.

@Test
public void testGetVariables() {
    Polynomial p = makePolynomial("3*x^2*y^4", "tuple()");
    assertEquals(Collections.emptyList(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "tuple(x)");
    assertEquals(parse("tuple(x)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "tuple(y)");
    assertEquals(parse("tuple(y)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "tuple(3)");
    assertEquals(parse("tuple(3)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "(x, y)");
    assertEquals(parse("(x, y)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "(x, y, z)");
    assertEquals(parse("(x, y, z)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "(x, y, 3)");
    assertEquals(parse("(x, y, 3)").getArguments(), p.getVariables());
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Test(org.junit.Test)

Aggregations

Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)32 Expression (com.sri.ai.expresso.api.Expression)20 DefaultPolynomial (com.sri.ai.grinder.polynomial.core.DefaultPolynomial)20 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)11 Monomial (com.sri.ai.grinder.polynomial.api.Monomial)8 Rational (com.sri.ai.util.math.Rational)7 List (java.util.List)5 Expressions.containsAnyOfGivenCollectionAsSubExpression (com.sri.ai.expresso.helper.Expressions.containsAnyOfGivenCollectionAsSubExpression)4 Pair (com.sri.ai.util.base.Pair)4 HashMap (java.util.HashMap)4 LinkedHashSet (java.util.LinkedHashSet)4 DefaultFunctionApplication (com.sri.ai.expresso.core.DefaultFunctionApplication)3 LinkedHashMap (java.util.LinkedHashMap)3 Polynomial.makeRandomPolynomial (com.sri.ai.grinder.polynomial.api.Polynomial.makeRandomPolynomial)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Beta (com.google.common.annotations.Beta)1 AbstractExpressionWrapper (com.sri.ai.expresso.helper.AbstractExpressionWrapper)1 ExpressionComparator (com.sri.ai.expresso.helper.ExpressionComparator)1