Search in sources :

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

Example 47 with Rational

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

the class DefaultSyntaxLeaf method isTooLargeRequiresScientificFormat.

private static boolean isTooLargeRequiresScientificFormat(Rational integerPart, Rational fractionalPart) {
    boolean isDisplayIntegerPartScientific = false;
    boolean isDisplayFractPartScientific = false;
    Rational reducedIntegerPart = integerPart;
    for (int i = 0; i < _displayScientificGreaterNIntegerPlaces; i++) {
        if (reducedIntegerPart.compareTo(1) < 0) {
            // We don't need to display the integer part with scientific notation
            break;
        }
        reducedIntegerPart = reducedIntegerPart.divide(10);
    }
    if (!(reducedIntegerPart.compareTo(1) < 0)) {
        isDisplayIntegerPartScientific = true;
    }
    if (!isDisplayIntegerPartScientific) {
        // The integer part does not need to be displayed as scientific
        // test the decimal part.
        Rational increasedFractionalPart = fractionalPart;
        for (int i = 0; i < _displayScientificAfterNDecimalPlaces; i++) {
            if (increasedFractionalPart.isInteger()) {
                // We don't need to display the fractional part with scientific notation
                break;
            }
            increasedFractionalPart = increasedFractionalPart.multiply(10);
        }
        if (!increasedFractionalPart.isInteger()) {
            isDisplayFractPartScientific = true;
        }
    }
    boolean result = isDisplayIntegerPartScientific || isDisplayFractPartScientific;
    return result;
}
Also used : Rational(com.sri.ai.util.math.Rational)

Example 48 with Rational

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

the class SampleMultiIndexQuantifierEliminator method computeMeasureAndDetermineIfShouldSample.

private Pair<Rational, Boolean> computeMeasureAndDetermineIfShouldSample(Expression index, Expression indexCondition, Expression additiveIdentityElement, Context context) {
    Pair<Rational, Boolean> result;
    Expression indexType = GrinderUtil.getTypeExpression(index, context);
    IndexExpressionsSet indexExpressionsSet = new ExtensionalIndexExpressionsSet(IndexExpressions.makeIndexExpression(index, indexType));
    Expression intensionalSet = IntensionalSet.intensionalMultiSet(indexExpressionsSet, index, indexCondition);
    Rational measureSetOfI = Measure.get(intensionalSet, context);
    boolean sample = true;
    if (!alwaysSample) {
        Type type = GrinderUtil.getType(index, context);
        // NOTE: We always sample from continuous domains
        if (type != null && type.isDiscrete()) {
            if (measureSetOfI.compareTo(sampleSizeN) <= 0) {
                // Domain is discrete and sample size is >= the size of the domain
                // so we don't want to sample in this instance
                sample = false;
            }
        }
    }
    result = new Pair<>(measureSetOfI, sample);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Type(com.sri.ai.expresso.api.Type) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 49 with Rational

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

the class DefaultSymbolTest method testScientificOutput.

@Test
public void testScientificOutput() {
    //
    // Positive
    Assert.assertEquals("1.2E-7", Expressions.makeSymbol(0.000000123).toString());
    Assert.assertEquals("1E-9", Expressions.makeSymbol(0.000000001).toString());
    Assert.assertEquals("1E6", Expressions.makeSymbol(1000000.013).toString());
    Assert.assertEquals("1E6", Expressions.makeSymbol(1000000.016).toString());
    // 1000000.9
    Assert.assertEquals("1E6", Expressions.makeSymbol(new Rational(10000009, 10)).toString());
    Assert.assertEquals("1.23457E6", Expressions.makeSymbol(1234567.1).toString());
    Assert.assertEquals("1.23457E6", Expressions.makeSymbol(1234567.9).toString());
    //
    // Negative
    Assert.assertEquals("-1.2E-7", Expressions.makeSymbol(-0.000000123).toString());
    Assert.assertEquals("-1E-9", Expressions.makeSymbol(-0.000000001).toString());
    Assert.assertEquals("-1E6", Expressions.makeSymbol(-1000000.013).toString());
    Assert.assertEquals("-1E6", Expressions.makeSymbol(-1000000.016).toString());
    // 1000000.9
    Assert.assertEquals("-1E6", Expressions.makeSymbol(new Rational(-10000009, 10)).toString());
    Assert.assertEquals("-1.23457E6", Expressions.makeSymbol(-1234567.1).toString());
    Assert.assertEquals("-1.23457E6", Expressions.makeSymbol(-1234567.9).toString());
}
Also used : Rational(com.sri.ai.util.math.Rational) Test(org.junit.Test)

Example 50 with Rational

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

the class DefaultSymbolTest method testDisplayLargeApprox.

@Test
public void testDisplayLargeApprox() {
    Rational largeRational = new Rational(3).pow(100000);
    Assert.assertEquals("1.33497E47712", Expressions.makeSymbol(largeRational).toString());
    largeRational = new Rational(3).pow(100000).divide(new Rational(7).pow(100));
    Assert.assertEquals("4.12732E47627", Expressions.makeSymbol(largeRational).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