Search in sources :

Example 16 with Rational

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

Example 17 with Rational

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

Example 18 with Rational

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

Example 19 with Rational

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());
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Rational(com.sri.ai.util.math.Rational) HashMap(java.util.HashMap) Expression(com.sri.ai.expresso.api.Expression) List(java.util.List) Test(org.junit.Test)

Example 20 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)

Aggregations

Rational (com.sri.ai.util.math.Rational)72 Expression (com.sri.ai.expresso.api.Expression)33 Test (org.junit.Test)26 Monomial (com.sri.ai.grinder.polynomial.api.Monomial)11 ArrayList (java.util.ArrayList)10 Type (com.sri.ai.expresso.api.Type)9 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)8 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)7 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)6 RealInterval (com.sri.ai.expresso.type.RealInterval)6 List (java.util.List)6 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)5 Symbol (com.sri.ai.expresso.api.Symbol)4 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)4 Categorical (com.sri.ai.expresso.type.Categorical)4 FunctionType (com.sri.ai.expresso.type.FunctionType)4 TupleType (com.sri.ai.expresso.type.TupleType)4 Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)4 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)3 HashMap (java.util.HashMap)3