Search in sources :

Example 26 with Rational

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;
}
Also used : Rational(com.sri.ai.util.math.Rational)

Example 27 with Rational

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;
}
Also used : Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression)

Example 28 with Rational

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;
}
Also used : Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression)

Example 29 with Rational

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;
}
Also used : Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression)

Example 30 with Rational

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;
}
Also used : Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression)

Aggregations

Rational (com.sri.ai.util.math.Rational)47 Expression (com.sri.ai.expresso.api.Expression)22 Test (org.junit.Test)17 Monomial (com.sri.ai.grinder.polynomial.api.Monomial)12 ArrayList (java.util.ArrayList)10 Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)6 Type (com.sri.ai.expresso.api.Type)5 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)4 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)4 Context (com.sri.ai.grinder.sgdpllt.api.Context)4 List (java.util.List)4 Symbol (com.sri.ai.expresso.api.Symbol)3 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)3 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)3 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)3 RealInterval (com.sri.ai.expresso.type.RealInterval)3 DefaultFunctionApplication (com.sri.ai.expresso.core.DefaultFunctionApplication)2 FunctionType (com.sri.ai.expresso.type.FunctionType)2 TupleType (com.sri.ai.expresso.type.TupleType)2 DefaultMonomial (com.sri.ai.grinder.polynomial.core.DefaultMonomial)2