use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class SummationOnDifferenceArithmeticAndPolynomialStepSolver method getValueAtGivenPoint.
private Expression getValueAtGivenPoint(Expression literalFreeBody, Expression variable, Expression value, Theory theory, Context context) {
Expression newBody = literalFreeBody.replaceAllOccurrences(variable, value, context);
Expression valueAtPoint = theory.simplify(newBody, context);
return valueAtPoint;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class SummationOnDifferenceArithmeticAndPolynomialStepSolver method computeSummationGivenValues.
private Expression computeSummationGivenValues(Expression variable, Expression literalFreeBody, RangeAndExceptionsSet values, Context context) {
Expression result;
if (values.equals(RangeAndExceptionsSet.EMPTY)) {
result = ZERO;
} else {
Theory theory = context.getTheory();
if (values instanceof RangeAndExceptionsSet.Singleton) {
Expression value = ((RangeAndExceptionsSet.Singleton) values).getSingleValue();
Expression valueAtPoint = DefaultPolynomial.make(getValueAtGivenPoint(literalFreeBody, variable, value, theory, context));
result = valueAtPoint;
} else {
Expression interval;
List<Expression> disequals;
if (values.hasFunctor(MINUS)) {
interval = values.get(0);
disequals = values.get(1).getArguments();
} else {
interval = values;
disequals = list();
}
Expression strictLowerBound = interval.get(0);
Expression nonStrictUpperBound = interval.get(1);
Polynomial bodyPolynomial = DefaultPolynomial.make(literalFreeBody);
Expression intervalSummation = PolynomialSummation.sum(variable, strictLowerBound, nonStrictUpperBound, bodyPolynomial);
ArrayList<Expression> argumentsForSubtraction = new ArrayList<>(1 + disequals.size());
argumentsForSubtraction.add(intervalSummation);
for (Expression disequal : disequals) {
Expression valueAtDisequal = getValueAtGivenPoint(literalFreeBody, variable, disequal, theory, context);
argumentsForSubtraction.add(apply(MINUS, valueAtDisequal));
}
Expression intervalSummationMinusValuesAtDisequals = apply(PLUS, argumentsForSubtraction);
result = DefaultPolynomial.make(theory.simplify(intervalSummationMinusValuesAtDisequals, context));
}
}
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class Monomial method orderedUnionOfNonNumericConstantFactors.
/**
* Create an ordered union (no duplicates) of the non-numeric constant
* factors contained in two Monomials.
*
* @param m1
* the first monomial.
* @param m2
* the second monomial.
* @return the ordered union (no duplicates) of the non-numeric constant
* factors contained in two Monomials.
*/
public static List<Expression> orderedUnionOfNonNumericConstantFactors(Monomial m1, Monomial m2) {
List<Expression> m1Factors = m1.getOrderedNonNumericFactors();
List<Expression> m2Factors = m2.getOrderedNonNumericFactors();
// For efficiency ensure we have enough capacity in the union up front.
List<Expression> result = new ArrayList<>(m1Factors.size() + m2Factors.size());
// NOTE: we know m1 and m2's non-numeric constant factors are
// ordered, which we can take advantage of to perform the
// ordered union more efficiently.
int m1FactorsSize = m1Factors.size();
int m2FactorsSize = m2Factors.size();
Expression m1Factor, m2Factor;
for (int i1 = 0, i2 = 0; i1 < m1FactorsSize || i2 < m2FactorsSize; ) {
if (i1 < m1FactorsSize && i2 < m2FactorsSize) {
m1Factor = m1Factors.get(i1);
m2Factor = m2Factors.get(i2);
int m1FactorComparisonToM2Factor = m1Factor.compareTo(m2Factor);
if (m1FactorComparisonToM2Factor == 0) {
// are considered equal
// just add one of them (as considered same) as do not want
// duplicates in the returned union.
result.add(m1Factor);
i1++;
i2++;
} else if (m1FactorComparisonToM2Factor < 0) {
// i.e. m1Factor
// is before
// m2Factor
result.add(m1Factor);
i1++;
} else {
// i.e. m2Factor is before m1Factor
result.add(m2Factor);
i2++;
}
} else if (i1 < m1FactorsSize) {
// implies i2 is exhausted
result.add(m1Factors.get(i1));
i1++;
} else {
// implies i2 < m2FactorsSize and i1 is exhausted
result.add(m2Factors.get(i2));
i2++;
}
}
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class AssignmentsIterator method makeMapFromVariablesToIteratorMakersFrom.
private static Map<Expression, NullaryFunction<Iterator<Expression>>> makeMapFromVariablesToIteratorMakersFrom(IndexExpressionsSet indexExpressionsSet, Registry registry) {
Map<Expression, NullaryFunction<Iterator<Expression>>> fromVariableToIteratorMaker = map();
ExtensionalIndexExpressionsSet extensionalIndexExpressionsSet;
try {
extensionalIndexExpressionsSet = (ExtensionalIndexExpressionsSet) indexExpressionsSet;
} catch (ClassCastException e) {
throw new Error("AssignmentsIterator defined for extensional index expressions sets only.");
}
for (Expression indexExpression : extensionalIndexExpressionsSet.getList()) {
Expression variable = IndexExpressions.getIndex(indexExpression);
Expression typeDescription = IndexExpressions.getType(indexExpression);
if (typeDescription == null) {
typeDescription = GrinderUtil.getTypeExpression(variable, registry);
}
putVariableAndIteratorMakerIn(fromVariableToIteratorMaker, variable, typeDescription, registry);
}
return fromVariableToIteratorMaker;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class CompoundTheoryWithDifferenceArithmeticTest method basicTests.
@Test
public void basicTests() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
Expression condition = parse("X = Y and Y = X and P and not Q and P and X = a and X != b");
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
constraint = constraint.conjoin(condition, context);
Expression expected = parse("(Y = a) and not Q and P and (X = Y)");
assertEquals(expected, constraint);
// nested indices
Expression expression = parse("sum({{(on I in 1..2, J in 2..3) sum({{ (on I in 1..10, J in 1..2) I + J : I != J }}) }})");
context = new TrueContext(theoryTestingSupport.getTheory());
expected = parse("536");
Expression actual = theoryTestingSupport.getTheory().evaluate(expression, context);
println(actual);
assertEquals(expected, actual);
}
Aggregations