use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class Minus method simplify.
public static Expression simplify(Expression expression) {
Expression result = expression;
Expression first = expression.get(0);
Expression second = expression.get(1);
if (Expressions.isNumber(first)) {
Rational firstValue = first.rationalValue();
if (Expressions.isNumber(second)) {
Rational secondValue = second.rationalValue();
result = Expressions.makeSymbol(firstValue.subtract(secondValue));
} else if (firstValue.isZero()) {
result = Expressions.apply("-", second);
}
} else if (Expressions.isNumber(second) && second.rationalValue().isZero()) {
result = first;
}
return result;
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class Division method simplify.
public static Expression simplify(Expression expression) {
if (expression.get(1).equals(INFINITY) || expression.get(1).equals(MINUS_INFINITY)) {
if (expression.get(0).equals(INFINITY) || expression.get(0).equals(MINUS_INFINITY)) {
throw new Error("Undefined value for division of infinities " + expression);
} else {
return ZERO;
}
}
if (expression.get(0).equals(INFINITY) || expression.get(0).equals(MINUS_INFINITY)) {
if (expression.get(1).equals(INFINITY) || expression.get(1).equals(MINUS_INFINITY)) {
throw new Error("Undefined value for division of infinities " + expression);
} else {
return expression.get(0);
}
}
if (expression.get(0).equals(expression.get(1))) {
// if numerator and denominator are equal, result is 1.
return ONE;
}
if (expression.get(0).equals(0)) {
// if numerator is 0, fraction is 0.
return ZERO;
}
if (expression.get(1).equals(1)) {
// if denominator is 1, fraction is numerator.
return expression.get(0);
}
if (ExpressionIsSymbolOfType.apply(expression.get(0), Number.class) && ExpressionIsSymbolOfType.apply(expression.get(1), Number.class)) {
Rational numerator = expression.get(0).rationalValue();
Rational denominator = expression.get(1).rationalValue();
Rational quotient = Util.divisionWithArbitraryPrecisionIfPossible(numerator, denominator);
if (quotient != null) {
return Expressions.makeSymbol(quotient);
}
}
return expression;
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class DefaultMonomialTest method testGetNumericConstantFactor.
@Test
public void testGetNumericConstantFactor() {
Assert.assertEquals(new Rational(0), makeMonomial("0").getNumericConstantFactor());
Assert.assertEquals(new Rational(0), makeMonomial("0*x^2").getNumericConstantFactor());
Assert.assertEquals(new Rational(1), makeMonomial("1").getNumericConstantFactor());
Assert.assertEquals(new Rational(-1), makeMonomial("-1").getNumericConstantFactor());
Assert.assertEquals(new Rational(2), makeMonomial("2").getNumericConstantFactor());
// Ensure the numeric constant is set to 1 if not explicitly represented in the expression
Assert.assertEquals(new Rational(1), makeMonomial("x^2").getNumericConstantFactor());
Assert.assertEquals(new Rational(4), makeMonomial("4*x^2").getNumericConstantFactor());
Assert.assertEquals(new Rational(4), makeMonomial("y*4*x^2").getNumericConstantFactor());
// Test that numerical constants are multiplied together
Assert.assertEquals(new Rational(8), makeMonomial("y^3*2*x^2*4").getNumericConstantFactor());
// Test edge case where the numeric constant is represented as a power
Assert.assertEquals(new Rational(8), makeMonomial("2^3*x^2").getNumericConstantFactor());
// Test edge case where the numeric constant is represented as a power and a separate constant
Assert.assertEquals(new Rational(24), makeMonomial("2^3*x^2*3").getNumericConstantFactor());
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class DefaultMonomialTest method testGetPowerOfFactor.
@Test
public void testGetPowerOfFactor() {
// Implicit power of 1
Monomial m = makeMonomial("2*x");
Assert.assertEquals(2, m.getFactors().size());
Assert.assertEquals(new Rational(0), m.getPowerOfFactor(Expressions.parse("1")));
Assert.assertEquals(new Rational(1), m.getPowerOfFactor(Expressions.parse("2")));
Assert.assertEquals(new Rational(1), m.getPowerOfFactor(Expressions.parse("x")));
m = makeMonomial("2*x^3*y^7*z^11");
Assert.assertEquals(new Rational(3), m.getPowerOfFactor(Expressions.parse("x")));
Assert.assertEquals(new Rational(7), m.getPowerOfFactor(Expressions.parse("y")));
Assert.assertEquals(new Rational(11), m.getPowerOfFactor(Expressions.parse("z")));
m = makeMonomial("2*z^3*y^7*x^11");
Assert.assertEquals(new Rational(11), m.getPowerOfFactor(Expressions.parse("x")));
Assert.assertEquals(new Rational(7), m.getPowerOfFactor(Expressions.parse("y")));
Assert.assertEquals(new Rational(3), m.getPowerOfFactor(Expressions.parse("z")));
m = makeMonomial("2*z^3*x^7*y^11");
Assert.assertEquals(new Rational(7), m.getPowerOfFactor(Expressions.parse("x")));
Assert.assertEquals(new Rational(11), m.getPowerOfFactor(Expressions.parse("y")));
Assert.assertEquals(new Rational(3), m.getPowerOfFactor(Expressions.parse("z")));
m = makeMonomial("2*z^3*x^7*x^11");
Assert.assertEquals(new Rational(18), m.getPowerOfFactor(Expressions.parse("x")));
Assert.assertEquals(new Rational(0), m.getPowerOfFactor(Expressions.parse("y")));
Assert.assertEquals(new Rational(3), m.getPowerOfFactor(Expressions.parse("z")));
// If factor not present in the monomial its power is 0.
m = makeMonomial("2");
Assert.assertEquals(new Rational(0), m.getPowerOfFactor(Expressions.parse("x")));
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class DefaultMonomialTest method testGetPowersOfOrderedNonNumericConstantFactors.
@Test
public void testGetPowersOfOrderedNonNumericConstantFactors() {
Monomial m = makeMonomial("2");
Assert.assertEquals(0, m.getPowersOfNonNumericFactors().size());
m = makeMonomial("2*x");
Assert.assertEquals(1, m.getPowersOfNonNumericFactors().size());
Assert.assertEquals(Arrays.asList(new Rational(1)), m.getPowersOfNonNumericFactors());
m = makeMonomial("2*x^3*y^7*z^11");
Assert.assertEquals(3, m.getPowersOfNonNumericFactors().size());
Assert.assertEquals(Arrays.asList(new Rational(3), new Rational(7), new Rational(11)), m.getPowersOfNonNumericFactors());
m = makeMonomial("2*z^3*y^7*x^11");
Assert.assertEquals(3, m.getPowersOfNonNumericFactors().size());
Assert.assertEquals(Arrays.asList(new Rational(11), new Rational(7), new Rational(3)), m.getPowersOfNonNumericFactors());
m = makeMonomial("2*z^3*x^7*y^11");
Assert.assertEquals(3, m.getPowersOfNonNumericFactors().size());
Assert.assertEquals(Arrays.asList(new Rational(7), new Rational(11), new Rational(3)), m.getPowersOfNonNumericFactors());
m = makeMonomial("2*z^3*x^7*x^11");
Assert.assertEquals(2, m.getPowersOfNonNumericFactors().size());
Assert.assertEquals(Arrays.asList(new Rational(18), new Rational(3)), m.getPowersOfNonNumericFactors());
}
Aggregations