Search in sources :

Example 6 with Polynomial

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;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Expression(com.sri.ai.expresso.api.Expression)

Example 7 with Polynomial

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;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Expression(com.sri.ai.expresso.api.Expression) Theory(com.sri.ai.grinder.api.Theory) ArrayList(java.util.ArrayList)

Example 8 with Polynomial

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;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Expression(com.sri.ai.expresso.api.Expression) IsVariable(com.sri.ai.grinder.library.IsVariable)

Example 9 with Polynomial

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;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Expression(com.sri.ai.expresso.api.Expression)

Example 10 with Polynomial

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;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Expression(com.sri.ai.expresso.api.Expression) Monomial(com.sri.ai.grinder.polynomial.api.Monomial) DefaultMonomial(com.sri.ai.grinder.polynomial.core.DefaultMonomial) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Polynomial (com.sri.ai.grinder.polynomial.api.Polynomial)32 Expression (com.sri.ai.expresso.api.Expression)20 DefaultPolynomial (com.sri.ai.grinder.polynomial.core.DefaultPolynomial)20 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)11 Monomial (com.sri.ai.grinder.polynomial.api.Monomial)8 Rational (com.sri.ai.util.math.Rational)7 List (java.util.List)5 Expressions.containsAnyOfGivenCollectionAsSubExpression (com.sri.ai.expresso.helper.Expressions.containsAnyOfGivenCollectionAsSubExpression)4 Pair (com.sri.ai.util.base.Pair)4 HashMap (java.util.HashMap)4 LinkedHashSet (java.util.LinkedHashSet)4 DefaultFunctionApplication (com.sri.ai.expresso.core.DefaultFunctionApplication)3 LinkedHashMap (java.util.LinkedHashMap)3 Polynomial.makeRandomPolynomial (com.sri.ai.grinder.polynomial.api.Polynomial.makeRandomPolynomial)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Beta (com.google.common.annotations.Beta)1 AbstractExpressionWrapper (com.sri.ai.expresso.helper.AbstractExpressionWrapper)1 ExpressionComparator (com.sri.ai.expresso.helper.ExpressionComparator)1