use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.
the class DifferenceArithmeticTheory method applicationOfAtomFunctorIsIndeedAtom.
@Override
public boolean applicationOfAtomFunctorIsIndeedAtom(Expression applicationOfAtomFunctor, Context context) {
boolean result;
try {
Expression leftHandSideMinusRightHandSide = BinaryMinus.make(applicationOfAtomFunctor.get(0), applicationOfAtomFunctor.get(1));
Polynomial polynomial = DefaultPolynomial.make(leftHandSideMinusRightHandSide);
result = forAll(polynomial.getMonomials(), isDifferenceArithmeticTerm(context)) && thereIsAtMostOnePositiveAndOneNegativeVariableMonomial(polynomial);
} catch (IllegalArgumentException exception) {
result = false;
}
return result;
}
use of com.sri.ai.grinder.polynomial.api.Polynomial 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, list(variable));
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.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.
the class SummationOnLinearRealArithmeticAndPolynomialStepSolver method computeSummationGivenValues.
private Expression computeSummationGivenValues(Expression literalFreeBody, Expression values, Context context) {
Expression result;
if (values.equals(Sets.EMPTY_SET) || Sets.isExtensionalSet(values)) {
result = ZERO;
} else {
Expression lowerBound = values.get(0);
Expression upperBound = values.get(1);
Polynomial bodyPolynomial = DefaultPolynomial.make(literalFreeBody);
Predicate<Expression> isVariable = new IsVariable(context.getIsUniquelyNamedConstantPredicate());
result = PolynomialIntegration.definiteIntegral(bodyPolynomial, getIndex(), lowerBound, upperBound, isVariable);
}
return result;
}
use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.
the class IsolateUtil method isolate.
public static Expression isolate(Expression leftExpression, Expression operator, Expression rightExpression, Expression linearVariableToIsolate) {
Polynomial leftPolynomial = DefaultPolynomial.make(leftExpression);
Polynomial rightPolynomial = DefaultPolynomial.make(rightExpression);
Expression result = isolate(leftPolynomial, operator, rightPolynomial, linearVariableToIsolate);
return result;
}
use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.
the class IsolateUtil method isolate.
public static Expression isolate(Polynomial leftPolynomial, Expression operator, Polynomial rightPolynomial, Expression linearVariableToIsolate) {
assertSupportedOperator(operator);
Pair<List<Monomial>, List<Monomial>> splitIntoSimilarAndDissimilarTerms;
splitIntoSimilarAndDissimilarTerms = splitIntoSimilarAndDissimilarTerms(leftPolynomial, linearVariableToIsolate);
List<Monomial> leftSimilarTerms = splitIntoSimilarAndDissimilarTerms.first;
List<Monomial> leftDissimilarTerms = splitIntoSimilarAndDissimilarTerms.second;
splitIntoSimilarAndDissimilarTerms = splitIntoSimilarAndDissimilarTerms(rightPolynomial, linearVariableToIsolate);
List<Monomial> rightSimilarTerms = splitIntoSimilarAndDissimilarTerms.first;
List<Monomial> rightDissimilarTerms = splitIntoSimilarAndDissimilarTerms.second;
// First move the left dissimilar terms over to the right side
for (Monomial leftDissimilarTerm : leftDissimilarTerms) {
rightDissimilarTerms.add(DefaultMonomial.make(Times.make(Expressions.MINUS_ONE, leftDissimilarTerm)));
}
// Second move the right similar terms over to the left side
for (Monomial rightSimilarTerm : rightSimilarTerms) {
leftSimilarTerms.add(DefaultMonomial.make(Times.make(Expressions.MINUS_ONE, rightSimilarTerm)));
}
Polynomial leftSimilarPolynomial = DefaultPolynomial.make(Plus.make(new ArrayList<>(leftSimilarTerms)));
Polynomial rightDissimilarPolynomial = DefaultPolynomial.make(Plus.make(new ArrayList<Expression>(rightDissimilarTerms)));
Monomial isolated = null;
// Compute Alpha
List<Expression> alphaTerms = new ArrayList<>();
for (Monomial similarTerm : leftSimilarPolynomial.getMonomials()) {
List<Expression> dissimilarFactors = new ArrayList<>();
dissimilarFactors.add(Expressions.makeSymbol(similarTerm.getNumericFactor()));
for (Expression factor : similarTerm.getOrderedNonNumericFactors()) {
if (factor.equals(linearVariableToIsolate)) {
if (isolated == null) {
isolated = DefaultMonomial.make(Exponentiation.make(factor, Expressions.makeSymbol(similarTerm.getPowerOfFactor(factor))));
}
if (!Rational.ONE.equals(similarTerm.getPowerOfFactor(factor))) {
throw new IllegalArgumentException("Only linear (i.e. x^1) can be isolated by this API but found : " + factor);
}
} else {
dissimilarFactors.add(Exponentiation.make(factor, Expressions.makeSymbol(similarTerm.getPowerOfFactor(factor))));
}
}
alphaTerms.add(DefaultMonomial.make(Times.make(dissimilarFactors)));
}
Polynomial alpha = DefaultPolynomial.make(Plus.make(alphaTerms), rightDissimilarPolynomial.getVariables());
Expression rightDissimilarTerm;
if (alpha.equals(Expressions.ZERO)) {
isolated = DefaultMonomial.ZERO;
rightDissimilarTerm = rightDissimilarPolynomial;
} else {
Pair<Polynomial, Polynomial> quotientAndRemainder = rightDissimilarPolynomial.divide(alpha);
if (quotientAndRemainder.second.equals(Expressions.ZERO)) {
rightDissimilarTerm = quotientAndRemainder.first;
} else {
rightDissimilarTerm = Division.simplify(Division.make(rightDissimilarPolynomial, alpha));
}
}
Expression result;
// If alpha is numeric we can divide (or know it was 0 - handled accordingly already).
if (Expressions.isNumber(alpha)) {
result = Expressions.apply(flipIfRequired(operator, alpha), isolated, rightDissimilarTerm);
} else {
Expression thenBranch;
if (isEquality(operator)) {
thenBranch = Expressions.apply(operator, isolated, rightDissimilarTerm);
} else {
thenBranch = IfThenElse.make(Expressions.apply(FunctorConstants.GREATER_THAN, alpha, Expressions.ZERO), Expressions.apply(operator, isolated, rightDissimilarTerm), Expressions.apply(flipInequalityOperator(operator), isolated, rightDissimilarTerm));
}
result = IfThenElse.make(Expressions.apply(FunctorConstants.DISEQUALITY, alpha, Expressions.ZERO), thenBranch, Expressions.apply(FunctorConstants.EQUALITY, Expressions.ZERO, rightDissimilarPolynomial));
}
return result;
}
Aggregations