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