use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class Expressions method round.
/**
* Takes an expression and, if it is numeric symbol,
* replaces it by a rounded value according to a precision (a number of significant digits to be kept);
* otherwise, return the expression itself.
*/
public static Expression round(Expression expression, int precision) {
if (isNumber(expression)) {
Rational value = expression.rationalValue();
String rounded = "";
if (value.isInteger()) {
rounded = value.toString();
} else {
rounded = value.toStringDotRelative(precision);
}
return Expressions.makeSymbol(rounded);
}
return expression;
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class SampleMultiIndexQuantifierEliminator method solve.
@Override
public Expression solve(AssociativeCommutativeGroup group, List<Expression> indices, Expression indicesCondition, Expression body, Context context) {
Expression result = null;
// Check if we want to sample
if (indices.size() == 1) {
// SetOfI = {{ (on I in Domain) I : Condition }}
Pair<Rational, Boolean> measureSetOfIAndSample = computeMeasureAndDetermineIfShouldSample(indices.get(0), indicesCondition, group.additiveIdentityElement(), context);
Rational measureSetOfI = measureSetOfIAndSample.first;
Boolean sample = measureSetOfIAndSample.second;
if (sample) {
// Quantifier({{ (on I in Samples) Head }} )
// NOTE: we are using the indices[2] with 2nd arg=TRUE so that the sampling logic can determine when it should activate
// in makeAssignmentsInterator() and makeSummand().
Expression sampleGroupSum = super.solve(group, Arrays.asList(indices.get(0), Expressions.TRUE), indicesCondition, body, context);
// Average = Quantifier( {{ (on I in Samples) Head }}) / n
Expression average = group.addNTimes(sampleGroupSum, Division.make(Expressions.ONE, Expressions.makeSymbol(sampleSizeN)), context);
// return Average * | SetOfI |
result = group.addNTimes(average, Expressions.makeSymbol(measureSetOfI), context);
}
}
if (result == null) {
result = super.solve(group, indices, indicesCondition, body, context);
}
return result;
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class Max method add.
@Override
public Expression add(Expression value1, Expression value2, Context context) {
Expression result;
if (value1.getValue() instanceof Number && value2.getValue() instanceof Number) {
Rational rationalValue1 = value1.rationalValue();
Rational rationalValue2 = value2.rationalValue();
if (rationalValue1.compareTo(rationalValue2) > 0) {
result = value1;
} else {
result = value2;
}
} else if (value1.equals(INFINITY) || value2.equals(INFINITY)) {
result = INFINITY;
} else if (value1.equals(MINUS_INFINITY)) {
result = value2;
} else if (value2.equals(MINUS_INFINITY)) {
result = value1;
} else {
result = Expressions.apply(MAX, value1, value2);
}
return result;
}
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;
}
use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.
the class DefaultSyntaxLeaf method toStringWithoutCaching.
@Override
public String toStringWithoutCaching() {
String result = "";
if (valueOrRootSyntaxTree instanceof String) {
result = (String) valueOrRootSyntaxTree;
if (isStringLiteral()) {
result = ESCAPE_STRING_VALUE.translate((String) valueOrRootSyntaxTree);
// Ensure is output as a String again
result = "\"" + result + "\"";
} else if (isSymbolValueQuotingRequired(result)) {
result = ESCAPE_STRING_VALUE.translate((String) valueOrRootSyntaxTree);
result = "'" + result + "'";
}
} else if (valueOrRootSyntaxTree instanceof Expression) {
result = "<" + valueOrRootSyntaxTree + ">";
} else if (valueOrRootSyntaxTree instanceof Number && _displayNumericPrecision != 0) {
Rational rLabel = ((Rational) valueOrRootSyntaxTree);
Rational absValue = rLabel.abs();
Rational[] integerAndFractionalPart = absValue.integerAndFractionalPart();
Rational integerPart = integerAndFractionalPart[0];
Rational fractionalPart = integerAndFractionalPart[1];
// Determine if we lose precision
boolean losePrecision = false;
Rational reducedIntegerPart = integerPart;
Rational increasedFractionalPart = fractionalPart;
for (int i = 0; i < _displayNumericPrecision; i++) {
if (reducedIntegerPart.compareTo(1) < 0) {
increasedFractionalPart = increasedFractionalPart.multiply(10);
if (increasedFractionalPart.isInteger()) {
// this means we won't lose precision
break;
}
} else {
reducedIntegerPart = reducedIntegerPart.divide(10);
}
}
if (!(reducedIntegerPart.compareTo(1) < 0 && increasedFractionalPart.isInteger())) {
losePrecision = true;
}
if (_displayNumericsExactly && losePrecision) {
if (rLabel.isInteger()) {
result = rLabel.getNumerator().toString();
} else {
// Output as an exact ratio
result = rLabel.getNumerator().toString() + "/" + rLabel.getDenominator().toString();
}
} else {
int approxPrecision = _displayNumericPrecision;
if (!(reducedIntegerPart.compareTo(1) < 0)) {
// Means we'll lose integer part precision
// Increase the precision to the max before scientific.
approxPrecision = _displayScientificGreaterNIntegerPlaces;
}
if (isTooLargeRequiresScientificFormat(integerPart, fractionalPart)) {
result = rLabel.toStringExponent(approxPrecision);
} else {
result = rLabel.toStringDotRelative(approxPrecision);
}
result = removeTrailingZerosToRight(result);
}
} else {
result = valueOrRootSyntaxTree.toString();
}
return result;
}
Aggregations