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());
}
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));
}
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());
}
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());
}
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));
}
Aggregations