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;
}
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;
}
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;
}
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());
}
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());
}
Aggregations