use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class MeasureTest method testRealIntervalTypeDomain.
@Test
public void testRealIntervalTypeDomain() {
Assert.assertEquals(new Rational(4), measure("{{ (on X in [3;7]) X : true }}"));
Assert.assertEquals(new Rational(4), measure("{{ (on X in [3;7]) X : X != 5 }}"));
}
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 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.util.math.Rational in project aic-praise by aic-sri-international.
the class HOGModelVisitor method visitSort_real_interval.
// sort_real_interval_closed_closed
// INTERVAL_LOWER_CLOSED lower=RATIONAL SEMICOLON upper=RATIONAL INTERVAL_UPPER_CLOSED
@Override
public Expression visitSort_real_interval(HOGMParser.Sort_real_intervalContext ctx) {
String intervalType;
if (ctx.lower_bracket.getText().equals("[")) {
if (ctx.upper_bracket.getText().equals("]")) {
intervalType = REAL_INTERVAL_CLOSED_CLOSED;
} else {
// upper bracket is open i.e. [
intervalType = REAL_INTERVAL_CLOSED_OPEN;
}
} else {
// Lower bracket is open i.e. ]
if (ctx.upper_bracket.getText().equals("]")) {
intervalType = REAL_INTERVAL_OPEN_CLOSED;
} else {
// upper bracket is open i.e. [
intervalType = REAL_INTERVAL_OPEN_OPEN;
}
}
Rational lower = new Rational(ctx.lower.getText());
if (ctx.negate_lower != null) {
lower = lower.negate();
}
Rational upper = new Rational(ctx.upper.getText());
if (ctx.negate_upper != null) {
upper = upper.negate();
}
Expression result = Expressions.makeExpressionOnSyntaxTreeWithLabelAndSubTrees(intervalType, Expressions.makeSymbol(lower), Expressions.makeSymbol(upper));
return result;
}
Aggregations