use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.
the class DefaultMonomialTest method testGet.
@Test
public void testGet() {
Monomial m = makeMonomial("x^2*y^3");
Assert.assertEquals(Expressions.makeSymbol("*"), m.get(-1));
Assert.assertEquals(Expressions.parse("x^2"), m.get(0));
Assert.assertEquals(Expressions.parse("y^3"), m.get(1));
m = makeMonomial("z^4*y^2*x^3*6");
Assert.assertEquals(Expressions.makeSymbol("*"), m.get(-1));
Assert.assertEquals(Expressions.parse("6"), m.get(0));
Assert.assertEquals(Expressions.parse("x^3"), m.get(1));
Assert.assertEquals(Expressions.parse("y^2"), m.get(2));
Assert.assertEquals(Expressions.parse("z^4"), m.get(3));
}
use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.
the class DefaultMonomialTest method testOrderedUnionOfNonNumericConstantFactors.
@Test
public void testOrderedUnionOfNonNumericConstantFactors() {
Monomial m1 = makeMonomial("0");
Monomial m2 = makeMonomial("0");
Assert.assertEquals(Collections.emptyList(), Monomial.orderedUnionOfNonNumericFactors(m1, m2));
m1 = makeMonomial("x");
m2 = makeMonomial("x");
Assert.assertEquals(Expressions.parse("tuple(x)").getArguments(), Monomial.orderedUnionOfNonNumericFactors(m1, m2));
m1 = makeMonomial("x^2*y^3");
m2 = makeMonomial("y^2*x^3");
Assert.assertEquals(Expressions.parse("tuple(x, y)").getArguments(), Monomial.orderedUnionOfNonNumericFactors(m1, m2));
m1 = makeMonomial("x^2*y^3");
m2 = makeMonomial("z^4*y^2*x^3");
Assert.assertEquals(Expressions.parse("tuple(x, y, z)").getArguments(), Monomial.orderedUnionOfNonNumericFactors(m1, m2));
}
use of com.sri.ai.grinder.polynomial.api.Monomial in project aic-expresso by aic-sri-international.
the class DefaultMonomial method times.
@Override
public Monomial times(Monomial multiplier) {
Monomial result;
// Optimization: return 0 if either numeric factor is 0
if (isZero() || multiplier.isZero()) {
result = ZERO;
} else if (isOne()) {
// Optimization, neutral element
result = multiplier;
} else if (multiplier.isOne()) {
// Optimization, neutral element
result = this;
} else {
List<Expression> combinedNonNumericFactors = Monomial.orderedUnionOfNonNumericFactors(this, multiplier);
List<Rational> thisSignature = this.getSignature(combinedNonNumericFactors);
List<Rational> multiplierSignature = multiplier.getSignature(combinedNonNumericFactors);
Rational resultNumericFactor = getNumericFactor().multiply(multiplier.getNumericFactor());
List<Rational> resultPowers = zipWith((power1, power2) -> power1.add(power2), thisSignature, multiplierSignature);
result = make(resultNumericFactor, combinedNonNumericFactors, resultPowers);
}
return result;
}
use of com.sri.ai.grinder.polynomial.api.Monomial 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.Monomial 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