Search in sources :

Example 1 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)");
    Assert.assertEquals(Expressions.makeSymbol("*"), p.getFunctor());
    p = makePolynomial("z^4*y^2*x^3 + 10", "(x, y)");
    Assert.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 2 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)");
    Assert.assertEquals(Expressions.makeSymbol("*"), p.get(-1));
    Assert.assertEquals(Expressions.parse("x^2"), p.get(0));
    Assert.assertEquals(Expressions.parse("y^3"), p.get(1));
    p = makePolynomial("x^2*y^3 + 10", "(x, y)");
    Assert.assertEquals(Expressions.makeSymbol("+"), p.get(-1));
    Assert.assertEquals(Expressions.parse("x^2*y^3"), p.get(0));
    Assert.assertEquals(Expressions.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 3 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()");
    Assert.assertEquals(Collections.emptyList(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "tuple(x)");
    Assert.assertEquals(Expressions.parse("tuple(x)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "tuple(y)");
    Assert.assertEquals(Expressions.parse("tuple(y)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "tuple(3)");
    Assert.assertEquals(Expressions.parse("tuple(3)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "(x, y)");
    Assert.assertEquals(Expressions.parse("(x, y)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "(x, y, z)");
    Assert.assertEquals(Expressions.parse("(x, y, z)").getArguments(), p.getVariables());
    p = makePolynomial("3*x^2*y^4", "(x, y, 3)");
    Assert.assertEquals(Expressions.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)

Example 4 with Polynomial

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

the class DefaultPolynomialTest method testGetSignatureTermMap.

@Test
public void testGetSignatureTermMap() {
    Polynomial p = makePolynomial("3*x^2*y^4", "tuple()");
    Map<List<Rational>, Expression> expectedSignatureTermMap = new HashMap<>();
    expectedSignatureTermMap.put(Collections.emptyList(), makeMonomial("3*x^2*y^4"));
    Assert.assertEquals(expectedSignatureTermMap, p.getSignatureTermMap());
    p = makePolynomial("3*x^2*y^4", "tuple(x)");
    expectedSignatureTermMap = new HashMap<>();
    expectedSignatureTermMap.put(Arrays.asList(new Rational(2)), Expressions.parse("x^2*(3*y^4)"));
    Assert.assertEquals(expectedSignatureTermMap, p.getSignatureTermMap());
    p = makePolynomial("3*x^2*y^4", "tuple(y)");
    expectedSignatureTermMap = new HashMap<>();
    expectedSignatureTermMap.put(Arrays.asList(new Rational(4)), Expressions.parse("y^4*(3*x^2)"));
    Assert.assertEquals(expectedSignatureTermMap, p.getSignatureTermMap());
    p = makePolynomial("3*x^2*y^4", "tuple(x, y)");
    expectedSignatureTermMap = new HashMap<>();
    expectedSignatureTermMap.put(Arrays.asList(new Rational(2), new Rational(4)), Expressions.parse("3*x^2*y^4"));
    Assert.assertEquals(expectedSignatureTermMap, p.getSignatureTermMap());
    p = makePolynomial("3*x^2*y^4", "tuple(y, x)");
    expectedSignatureTermMap = new HashMap<>();
    expectedSignatureTermMap.put(Arrays.asList(new Rational(4), new Rational(2)), Expressions.parse("3*x^2*y^4"));
    Assert.assertEquals(expectedSignatureTermMap, p.getSignatureTermMap());
    p = makePolynomial("3*x^2*y^4", "tuple(x, y, z)");
    expectedSignatureTermMap = new HashMap<>();
    expectedSignatureTermMap.put(Arrays.asList(new Rational(2), new Rational(4), new Rational(0)), Expressions.parse("3*x^2*y^4"));
    Assert.assertEquals(expectedSignatureTermMap, p.getSignatureTermMap());
}
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) HashMap(java.util.HashMap) Expression(com.sri.ai.expresso.api.Expression) List(java.util.List) Test(org.junit.Test)

Example 5 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);
    Assert.assertEquals(Expressions.parse("0"), quotientAndRemainder.first);
    Assert.assertEquals(Expressions.parse("0"), quotientAndRemainder.second);
    dividend = makePolynomial("3", "tuple(x)");
    divisor = makePolynomial("2", "tuple(x)");
    quotientAndRemainder = dividend.divide(divisor);
    Assert.assertEquals(Expressions.parse("1.5"), quotientAndRemainder.first);
    Assert.assertEquals(Expressions.parse("0"), quotientAndRemainder.second);
    dividend = makePolynomial("2*x^2 + 3*x + 6", "tuple(x)");
    divisor = makePolynomial("2", "tuple(x)");
    quotientAndRemainder = dividend.divide(divisor);
    Assert.assertEquals(Expressions.parse("x^2 + 1.5*x + 3"), quotientAndRemainder.first);
    Assert.assertEquals(Expressions.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);
    Assert.assertEquals(Expressions.parse("x + -5"), quotientAndRemainder.first);
    Assert.assertEquals(Expressions.parse("0"), quotientAndRemainder.second);
    dividend = makePolynomial("x^3 - 1", "tuple(x)");
    divisor = makePolynomial("x + 2", "tuple(x)");
    quotientAndRemainder = dividend.divide(divisor);
    Assert.assertEquals(Expressions.parse("x^2 + -2*x + 4"), quotientAndRemainder.first);
    Assert.assertEquals(Expressions.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);
    Assert.assertEquals(Expressions.parse("3*x + -11"), quotientAndRemainder.first);
    Assert.assertEquals(Expressions.parse("28*x + 30"), quotientAndRemainder.second);
    Polynomial p = makePolynomial("(3*x^3 - 2*x^2 + 4*x - 3) / (x^2 + 3*x + 3)", "tuple(x)");
    Assert.assertEquals(Expressions.parse("3*x + (-11 + ((28*x + 30) / (x^2 + 3*x + 3)))"), p);
    // Note: the -11 from the quotient gets absorbed into the remainder/divisor term 
    // as they are like terms under the variables [x]
    Assert.assertEquals(2, p.numberOfTerms());
    Assert.assertEquals(Expressions.parse("3*x"), p.getOrderedSummands().get(0));
    Assert.assertEquals(Expressions.parse("-11 + ((28*x + 30) / (x^2 + 3*x + 3))"), p.getOrderedSummands().get(1));
}
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)25 DefaultPolynomial (com.sri.ai.grinder.polynomial.core.DefaultPolynomial)14 Expression (com.sri.ai.expresso.api.Expression)13 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 Monomial (com.sri.ai.grinder.polynomial.api.Monomial)9 Rational (com.sri.ai.util.math.Rational)7 Pair (com.sri.ai.util.base.Pair)4 List (java.util.List)4 DefaultFunctionApplication (com.sri.ai.expresso.core.DefaultFunctionApplication)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)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 Expressions (com.sri.ai.expresso.helper.Expressions)1