Search in sources :

Example 16 with Polynomial

use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.

the class DefaultPolynomialTest method testIsMonomial.

@Test
public void testIsMonomial() {
    Polynomial p = makePolynomial("0", "(x, y)");
    Assert.assertTrue(p.isMonomial());
    p = makePolynomial("1", "(x, y)");
    Assert.assertTrue(p.isMonomial());
    // Note: the numeric constants should just be added up together
    p = makePolynomial("1 + 2 + 4", "(x, y)");
    Assert.assertTrue(p.isMonomial());
    p = makePolynomial("3*x^2*y^4", "tuple(y, x)");
    Assert.assertTrue(p.isMonomial());
    // Note: the 0 should be dropped, so remains a monomial
    p = makePolynomial("3*x^2*y^4 + 0", "tuple(y, x)");
    Assert.assertTrue(p.isMonomial());
    p = makePolynomial("3*x^2*y^4 + 1", "tuple(y, x)");
    Assert.assertFalse(p.isMonomial());
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Test(org.junit.Test)

Example 17 with Polynomial

use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.

the class PolynomialIntegration method definiteIntegral.

/**
	 * This method will return the polynomial equivalent to:<br>
	 * <pre>
	 * Q.replace(variable, end) - Q.replace(variable, start)
	 * </pre>
	 * where 'Q = indefiniteIntegral(polynomial, variable)'
	 *  
	 * @param polynomial
	 *        the polynomial the definite integral is to be found for.
	 * @param variable
	 *        the variable integration is with respect to.
	 * @param start
	 *        the starting limit of the integral.
	 * @param end
	 *        the ending limit of the integral.
	 * @return the definite integral of the polynomial for the given limits.
	 */
public static Polynomial definiteIntegral(Polynomial polynomial, Expression variable, Expression start, Expression end) {
    Polynomial q = indefiniteIntegral(polynomial, variable);
    // Note: will include variable due to calling indefiniteIntegral
    Set<Expression> variableSet = new LinkedHashSet<>(q.getVariables());
    if (!Expressions.isNumber(start)) {
        variableSet.add(start);
    }
    if (!Expressions.isNumber(end)) {
        variableSet.add(end);
    }
    List<Expression> variables = new ArrayList<>(variableSet);
    Polynomial minuendPolynomial = replaceFactor(q, variable, end, variables);
    Polynomial subtrahendPolynomial = replaceFactor(q, variable, start, variables);
    Polynomial result = minuendPolynomial.minus(subtrahendPolynomial);
    return result;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) LinkedHashSet(java.util.LinkedHashSet) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList)

Example 18 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 variable, 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);
        result = PolynomialIntegration.definiteIntegral(bodyPolynomial, variable, lowerBound, upperBound);
    }
    return result;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) Polynomial.makeRandomPolynomial(com.sri.ai.grinder.polynomial.api.Polynomial.makeRandomPolynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Expression(com.sri.ai.expresso.api.Expression)

Example 19 with Polynomial

use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.

the class DefaultPolynomialTest method testSet.

@Test
public void testSet() {
    Polynomial p = makePolynomial("x^2*y^3", "(x, y)");
    assertEquals(parse("2*y^3"), p.set(0, parse("2")));
    assertEquals(parse("x^5"), p.set(1, parse("x^3")));
    assertEquals(parse("x^2*z^4"), p.set(1, parse("z^4")));
    p = makePolynomial("x^2*y^3 + 10", "(x, y)");
    assertEquals(parse("2 + 10"), p.set(0, parse("2")));
    assertEquals(parse("x^2*y^3 + 0"), p.set(1, parse("0")));
    assertEquals(parse("(2 + 3) + 10"), p.set(0, parse("2 + 3")));
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Test(org.junit.Test)

Example 20 with Polynomial

use of com.sri.ai.grinder.polynomial.api.Polynomial in project aic-expresso by aic-sri-international.

the class DefaultPolynomialTest method testNumberOfArguments.

@Test
public void testNumberOfArguments() {
    Polynomial p = makePolynomial("0", "(x, y)");
    assertEquals(0, p.numberOfArguments());
    p = makePolynomial("x^2*y^3", "(x, y)");
    assertEquals(2, p.numberOfArguments());
    p = makePolynomial("x^2*y^3 + 10", "(x, y)");
    assertEquals(2, p.numberOfArguments());
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Test(org.junit.Test)

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