Search in sources :

Example 31 with Rational

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

the class Measure method get.

public static Rational get(Expression intensionalSetExpression, Context context) {
    Rational result;
    if (Sets.isIntensionalSet(intensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) intensionalSetExpression;
        IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
        List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
        if (indices.size() == 1) {
            Expression evaluatedResult;
            Expression intensionalSetIndex = indices.get(0);
            Expression intensionalSetHead = intensionalSet.getHead();
            if (!intensionalSetHead.equals(intensionalSetIndex)) {
                throw new UnsupportedOperationException("Index and Head must be the same to calculate the meaure of an Intensional : " + intensionalSet);
            }
            Expression intensionalSetCondition = intensionalSet.getCondition();
            Context intensionalSetContext = context.extendWith(indexExpressionsSet);
            Type indexType = GrinderUtil.getTypeOfExpression(intensionalSetIndex, intensionalSetContext);
            if (intensionalSetCondition.equals(false)) {
                // short circuit known empty sets up front.
                evaluatedResult = Expressions.ZERO;
            } else if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
                // NOTE : For Reals can always assume the condition is of this type.
                SingleVariableLinearRealArithmeticConstraint svConstraint = (SingleVariableLinearRealArithmeticConstraint) intensionalSetCondition;
                MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver realSolver = new MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(svConstraint);
                evaluatedResult = realSolver.solve(intensionalSetContext);
            } else if (indexType instanceof FunctionType) {
                if (!intensionalSetCondition.equals(true)) {
                    throw new UnsupportedOperationException("Measure of intensional set with a function type domain currently do not support conditions: " + intensionalSet);
                }
                // measure(co-domain)^measure(domain)
                FunctionType indexFunctionType = (FunctionType) indexType;
                Expression condomainIntensionalSet = constructComponentIntensionalSet(indexFunctionType.getCodomain(), intensionalSet, ZERO, intensionalSetContext);
                Rational codomainMeasure = get(condomainIntensionalSet, intensionalSetContext);
                Rational domainMeasure = Rational.ONE;
                for (Type argDomainType : indexFunctionType.getArgumentTypes()) {
                    Expression argDomainIntensionalSet = constructComponentIntensionalSet(argDomainType, intensionalSet, ZERO, intensionalSetContext);
                    Rational argMeasure = get(argDomainIntensionalSet, intensionalSetContext);
                    domainMeasure = domainMeasure.multiply(argMeasure);
                }
                evaluatedResult = Expressions.makeSymbol(codomainMeasure.pow(domainMeasure.intValueExact()));
            } else if (indexType instanceof TupleType) {
                if (!intensionalSetCondition.equals(true)) {
                    throw new UnsupportedOperationException("Measure of intensional set with a tuple type domain currently do not support conditions: " + intensionalSet);
                }
                // (element_1, ..., element_n) = measure(element_1) * ... * measure(element_n)
                TupleType indexTupleType = (TupleType) indexType;
                Rational elementMeasuresProduct = Rational.ONE;
                for (Type elementType : indexTupleType.getElementTypes()) {
                    Expression elementDomainIntensionalSet = constructComponentIntensionalSet(elementType, intensionalSet, ZERO, intensionalSetContext);
                    Rational elementMeasure = get(elementDomainIntensionalSet, intensionalSetContext);
                    elementMeasuresProduct = elementMeasuresProduct.multiply(elementMeasure);
                }
                evaluatedResult = Expressions.makeSymbol(elementMeasuresProduct);
            } else {
                Expression countingFormula = new DefaultCountingFormula(indexExpressionsSet, intensionalSet.getCondition());
                evaluatedResult = context.getTheory().evaluate(countingFormula, context);
            }
            if (Expressions.isNumber(evaluatedResult)) {
                result = evaluatedResult.rationalValue();
            } else {
                throw new UnsupportedOperationException("Unable to compute a finite measure for: " + intensionalSet + ", got : " + evaluatedResult);
            }
        } else {
            throw new UnsupportedOperationException("Currently only support the measure of single indexed intensional sets: " + intensionalSet);
        }
    } else {
        throw new IllegalArgumentException("Not an intensional set: " + intensionalSetExpression);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) Rational(com.sri.ai.util.math.Rational) FunctionType(com.sri.ai.expresso.type.FunctionType) DefaultCountingFormula(com.sri.ai.expresso.core.DefaultCountingFormula) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) TupleType(com.sri.ai.expresso.type.TupleType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver)

Example 32 with Rational

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

the class DefaultSyntaxLeaf method getRestrictedPrecisionNumberRepresentation.

private String getRestrictedPrecisionNumberRepresentation() {
    String result;
    Rational rationalValue = (Rational) valueOrRootSyntaxTree;
    if (isDisplayNumericsExactlyForSymbols() && exactDecimalRepresentationMayExceedAllowedPrecision(rationalValue)) {
        result = getExactRepresentationEvenIfThatMeansARatio(rationalValue);
    } else {
        result = getDecimalRepresentation(rationalValue);
    }
    return result;
}
Also used : Rational(com.sri.ai.util.math.Rational)

Example 33 with Rational

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

the class DefaultSyntaxLeaf method exactDecimalRepresentationMayExceedAllowedPrecision.

private boolean exactDecimalRepresentationMayExceedAllowedPrecision(Rational rationalValue) {
    boolean decimalExpansionIsKnownToBeFinite = isKnownToBePowerOf2And5Only(new Rational(rationalValue.getDenominator()));
    boolean decimalExpansionMayBeInfinite = !decimalExpansionIsKnownToBeFinite;
    return decimalExpansionMayBeInfinite;
}
Also used : Rational(com.sri.ai.util.math.Rational)

Example 34 with Rational

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

the class DefaultSyntaxLeaf method removeAllFactorsEqualTo.

private Rational removeAllFactorsEqualTo(Rational rationalValue, int factor) {
    boolean changed;
    do {
        Rational fraction = rationalValue.divide(factor);
        if (fraction.isInteger()) {
            rationalValue = fraction;
            changed = true;
        } else {
            changed = false;
        }
    } while (changed);
    return rationalValue;
}
Also used : Rational(com.sri.ai.util.math.Rational)

Example 35 with Rational

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

the class DefaultSymbolTest method testScientificNotationTriggeredByNumberOfDecimalPlaces.

@Test
public void testScientificNotationTriggeredByNumberOfDecimalPlaces() {
    ExpressoConfiguration.setDisplayNumericsMostDecimalPlacesInApproximateRepresentationOfNumericalSymbols(10);
    ExpressoConfiguration.setDisplayNumericsGreatestInitialNonZeroDecimalPlacePositionBeforeSwitchingToScientificNotation(4);
    Assert.assertEquals("0.001", Expressions.makeSymbol(new Rational(1, 1000)).toString());
    Assert.assertEquals("0.0001", Expressions.makeSymbol(new Rational(1, 10000)).toString());
    Assert.assertEquals("1E-5", Expressions.makeSymbol(new Rational(1, 100000)).toString());
    Assert.assertEquals("1E-6", Expressions.makeSymbol(new Rational(1, 1000000)).toString());
    ExpressoConfiguration.setDisplayNumericsMostDecimalPlacesInApproximateRepresentationOfNumericalSymbols(3);
    ExpressoConfiguration.setDisplayNumericsGreatestInitialNonZeroDecimalPlacePositionBeforeSwitchingToScientificNotation(12);
    Assert.assertEquals("0.001", Expressions.makeSymbol(new Rational(1, 1000)).toString());
    Assert.assertEquals("1E-4", Expressions.makeSymbol(new Rational(1, 10000)).toString());
    Assert.assertEquals("1E-5", Expressions.makeSymbol(new Rational(1, 100000)).toString());
    Assert.assertEquals("1E-6", Expressions.makeSymbol(new Rational(1, 1000000)).toString());
}
Also used : Rational(com.sri.ai.util.math.Rational) Test(org.junit.Test)

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