Search in sources :

Example 66 with Rational

use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.

the class DefaultSymbolTest method testDisplayLargeExact.

@Test
public void testDisplayLargeExact() {
    // NOTE: the tearDown method will set us back to the correct default.
    ExpressoConfiguration.setDisplayNumericsExactlyForSymbols(true);
    Rational largeRational = new Rational(3).pow(100000);
    Assert.assertEquals("1.334971414230401469458914390489782E47712", Expressions.makeSymbol(largeRational).toString());
    largeRational = new Rational(3).pow(100000).divide(new Rational(7).pow(100));
    Assert.assertEquals("1.334971414230401469458914390489782E+47661/3234476509624757991344647769100217", Expressions.makeSymbol(largeRational).toString());
}
Also used : Rational(com.sri.ai.util.math.Rational) Test(org.junit.Test)

Example 67 with Rational

use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.

the class MeasureTest method testFunctionTypeDomain.

// measure(co-domain)^measure(domain)
@Test
public void testFunctionTypeDomain() {
    Assert.assertEquals(new Rational(8), measure("{{ (on f in 0..2 -> Boolean) f : true }}"));
    Assert.assertEquals(new Rational(64), measure("{{ (on f in 0..2 -> [3;7]) f : true }}"));
}
Also used : Rational(com.sri.ai.util.math.Rational) Test(org.junit.Test)

Example 68 with Rational

use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.

the class MeasureTest method measure.

private Rational measure(String testIntensionalSetString) {
    Expression testIntensionalSetExpression = parse(testIntensionalSetString);
    Expression properlyConditionedIntensionalSetExpression = testIntensionalSetExpression;
    if (Sets.isIntensionalSet(testIntensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) testIntensionalSetExpression;
        List<Expression> indices = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
        if (indices.size() == 1) {
            Expression index = indices.get(0);
            Context intensionalSetContext = context.extendWith(intensionalSet.getIndexExpressions());
            Type type = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
            if (type instanceof RealExpressoType || type instanceof RealInterval) {
                SingleVariableLinearRealArithmeticConstraint singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
                singleVariableConstraint = (SingleVariableLinearRealArithmeticConstraint) singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
                properlyConditionedIntensionalSetExpression = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, intensionalSet.getIndexExpressions(), intensionalSet.getHead(), singleVariableConstraint);
            }
        }
    }
    Rational result = Measure.get(properlyConditionedIntensionalSetExpression, context);
    return result;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 69 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 70 with Rational

use of com.sri.ai.util.math.Rational in project aic-expresso by aic-sri-international.

the class Expressions method getExpressionBeingMultipliedByMinusOneInNumberOrNull.

private static Expression getExpressionBeingMultipliedByMinusOneInNumberOrNull(Expression expression) {
    Expression result;
    Rational value = expression.rationalValue();
    if (value.isNegative()) {
        result = makeSymbol(value.negate());
    } else {
        result = null;
    }
    return result;
}
Also used : Rational(com.sri.ai.util.math.Rational) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression)

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