use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.
the class DefaultPolynomialTest method testExponentiate.
@Test
public void testExponentiate() {
Polynomial p = makePolynomial("0", "tuple(x)");
assertEquals(makePolynomial("1", "tuple(x)"), p.exponentiate(0));
assertEquals(makePolynomial("0", "tuple(x)"), p.exponentiate(1));
assertEquals(makePolynomial("0", "tuple(x)"), p.exponentiate(3));
p = makePolynomial("1", "tuple(x)");
assertEquals(makePolynomial("1", "tuple(x)"), p.exponentiate(0));
assertEquals(makePolynomial("1", "tuple(x)"), p.exponentiate(1));
assertEquals(makePolynomial("1", "tuple(x)"), p.exponentiate(3));
p = makePolynomial("x", "tuple(x)");
assertEquals(makePolynomial("1", "tuple(x)"), p.exponentiate(0));
assertEquals(makePolynomial("x", "tuple(x)"), p.exponentiate(1));
assertEquals(makePolynomial("x^3", "tuple(x)"), p.exponentiate(3));
p = makePolynomial("x + 2", "tuple(x)");
assertEquals(makePolynomial("1", "tuple(x)"), p.exponentiate(0));
assertEquals(makePolynomial("x + 2", "tuple(x)"), p.exponentiate(1));
assertEquals(makePolynomial("x^2 + 4*x + 4", "tuple(x)"), p.exponentiate(2));
assertEquals(makePolynomial("x^3 + 6*x^2 + 12*x + 8", "tuple(x)"), p.exponentiate(3));
p = makePolynomial("x + y + 2", "(x, y)");
assertEquals(makePolynomial("1", "(x, y)"), p.exponentiate(0));
assertEquals(makePolynomial("x + y + 2", "(x, y)"), p.exponentiate(1));
assertEquals(makePolynomial("x^2 + 2*x*y + 4*x + y^2 + 4*y + 4", "(x, y)"), p.exponentiate(2));
assertEquals(makePolynomial("x^3 + 3*x^2*y + 6*x^2 + 3*x*y^2 + 12*x*y + 12*x + y^3 + 6*y^2 + 12*y + 8", "(x, y)"), p.exponentiate(3));
p = makePolynomial("x^2*y + y + z^3 + 2", "(x, y, z)");
assertEquals(makePolynomial("1", "(x, y, z)"), p.exponentiate(0));
assertEquals(makePolynomial("x^2*y + y + z^3 + 2", "tuple(x, y, z)"), p.exponentiate(1));
assertEquals(makePolynomial("x^4*y^2 + 2*x^2*y^2 + 2*x^2*y*z^3 + 4*x^2*y + y^2 + 2*y*z^3 + 4*y + z^6 + 4*z^3 + 4", "(x, y, z)"), p.exponentiate(2));
p = makePolynomial("a + b + c", "(a, b, c)");
assertEquals(makePolynomial("a^3 + b^3 + c^3 + 3*a^2*b + 3*a^2*c + 3*b^2*a + 3*b^2*c + 3*c^2*a + 3*c^2*b + 6*a*b*c", "(a, b, c)"), p.exponentiate(3));
p = makePolynomial("x + y + 1", "tuple(x)");
assertEquals(parse("x^2 + 2*x*(y + 1) + (y + 1)^2"), p.exponentiate(2));
// NOTE: of interest as it will cause terms to cancel each other out during computation
// due to the negative values and odd exponent.
p = makePolynomial("z^2 + -1*z + -1", "tuple(z)");
assertEquals(parse("z^6 - (3*z^5) + 5*z^3 -(3*z) - 1"), p.exponentiate(3));
}
use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.
the class DefaultPolynomialTest method testMultivariateDivide.
@Test
public void testMultivariateDivide() {
// NOTE: We currently don't support multivariate division so the remainder will be
// the same as the dividend.
Polynomial dividend = makePolynomial("x^3*y^3 + y^2 + 5", "tuple(x, y)");
Polynomial divisor = makePolynomial("x^2 + y", "tuple(x, y)");
Pair<Polynomial, Polynomial> quotientAndRemainder = dividend.divide(divisor);
assertEquals(parse("0"), quotientAndRemainder.first);
assertEquals(parse("x^3*y^3 + y^2 + 5"), quotientAndRemainder.second);
}
use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.
the class DefaultPolynomialTest method runTestInvalidPolynomials.
private void runTestInvalidPolynomials(String expressionString, String tupleVariablesString) {
Expression tuple = parse(tupleVariablesString);
Expression expression = parse(expressionString);
List<Expression> variables = tuple.getArguments();
try {
Polynomial polynomial = DefaultPolynomial.make(expression, variables);
fail("Should not have been able to turn " + expressionString + " into a polynomial of " + join(variables) + " but made " + polynomial);
} catch (IllegalArgumentException e) {
}
}
use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.
the class DefaultPolynomial method minus.
@Override
public Polynomial minus(Polynomial subtrahend) throws IllegalArgumentException {
assertSameVariables(subtrahend);
Polynomial result;
if (subtrahend.isZero()) {
result = this;
} else {
List<Monomial> negatedSubtrahendSummands = new ArrayList<>();
subtrahend.getMonomials().forEach(summand -> negatedSubtrahendSummands.add(summand.times(DefaultMonomial.MINUS_ONE)));
Polynomial negatedSubtrahend = new DefaultPolynomial(negatedSubtrahendSummands, getVariables());
result = add(negatedSubtrahend);
}
return result;
}
use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.
the class DefaultPolynomial method add.
@Override
public Polynomial add(Polynomial summand) throws IllegalArgumentException {
assertSameVariables(summand);
Polynomial result;
if (isZero()) {
result = summand;
} else if (summand.isZero()) {
result = this;
} else {
List<Monomial> summands = new ArrayList<>();
Set<List<Rational>> combinedSignatures = new LinkedHashSet<>(this.getMonomials().size() + summand.getMonomials().size());
combinedSignatures.addAll(this.getMapFromSignatureToMonomial().keySet());
combinedSignatures.addAll(summand.getMapFromSignatureToMonomial().keySet());
for (List<Rational> signature : combinedSignatures) {
// NOTE: at least one of these assignments is guaranteed to be non-null.
Monomial m1 = this.getMapFromSignatureToMonomial().get(signature);
Monomial m2 = summand.getMapFromSignatureToMonomial().get(signature);
if (m1 == null) {
if (!m2.isZero()) {
summands.add(m2);
}
} else if (m2 == null) {
if (!m1.isZero()) {
summands.add(m1);
}
} else {
Monomial sum = addMonomialsWithSameSignature(m1, m2);
if (!sum.isZero()) {
summands.add(sum);
}
}
}
// In case all the summands cancel each other out
if (summands.isEmpty()) {
result = makeFromMonomial(Expressions.ZERO, variables);
} else {
result = new DefaultPolynomial(summands, getVariables());
}
}
return result;
}
Aggregations