A module to implement logical predicates and assumption system.


class sympy.assumptions.assume.Predicate(*args, **kwargs)[source]

Base class for mathematical predicates. It also serves as a constructor for undefined predicate objects.


Predicate is a function that returns a boolean value [1].

Predicate function is object, and it is instance of predicate class. When a predicate is applied to arguments, AppliedPredicate instance is returned. This merely wraps the argument and remain unevaluated. To obtain the truth value of applied predicate, use the function ask.

Evaluation of predicate is done by multiple dispatching. You can register new handler to the predicate to support new types.

Every predicate in SymPy can be accessed via the property of Q. For example, Q.even returns the predicate which checks if the argument is even number.

To define a predicate which can be evaluated, you must subclass this class, make an instance of it, and register it to Q. After then, dispatch the handler by argument types.

If you directly construct predicate using this class, you will get UndefinedPredicate which cannot be dispatched. This is useful when you are building boolean expressions which do not need to be evaluated.


Applying and evaluating to boolean value:

>>> from sympy import Q, ask
>>> ask(

You can define a new predicate by subclassing and dispatching. Here, we define a predicate for sexy primes [2] as an example.

>>> from sympy import Predicate, Integer
>>> class SexyPrimePredicate(Predicate):
...     name = "sexyprime"
>>> Q.sexyprime = SexyPrimePredicate()
>>> @Q.sexyprime.register(Integer, Integer)
... def _(int1, int2, assumptions):
...     args = sorted([int1, int2])
...     if not all(ask(, assumptions) for a in args):
...         return False
...     return args[1] - args[0] == 6
>>> ask(Q.sexyprime(5, 11))

Direct constructing returns UndefinedPredicate, which can be applied but cannot be dispatched.

>>> from sympy import Predicate, Integer
>>> Q.P = Predicate("P")
>>> type(Q.P)
<class 'sympy.assumptions.assume.UndefinedPredicate'>
>>> Q.P(1)
>>> Q.P.register(Integer)(lambda expr, assump: True)
Traceback (most recent call last):
TypeError: <class 'sympy.assumptions.assume.UndefinedPredicate'> cannot be dispatched.


eval(args, assumptions=True)[source]

Evaluate self(*args) under the given assumptions.

This uses only direct resolution methods, not logical inference.

handler = <dispatched AskPredicateHandler>
classmethod register(*types, **kwargs)[source]

Register the signature to the handler.

classmethod register_many(*types, **kwargs)[source]

Register multiple signatures to same handler.

class sympy.assumptions.assume.AppliedPredicate(predicate, *args)[source]

The class of expressions resulting from applying Predicate to the arguments. AppliedPredicate merely wraps its argument and remain unevaluated. To evaluate it, use the ask() function.


>>> from sympy import Q, ask
>>> Q.integer(1)

The function attribute returns the predicate, and the arguments attribute returns the tuple of arguments.

>>> type(Q.integer(1))
<class 'sympy.assumptions.assume.AppliedPredicate'>
>>> Q.integer(1).function
>>> Q.integer(1).arguments

Applied predicates can be evaluated to a boolean value with ask:

>>> ask(Q.integer(1))
property arg

Return the expression used by this assumption.


>>> from sympy import Q, Symbol
>>> x = Symbol('x')
>>> a = Q.integer(x + 1)
>>> a.arg
x + 1
property arguments

Return the arguments which are applied to the predicate.

property function

Return the predicate.


Queries are used to ask information about expressions. Main method for this is ask():

sympy.assumptions.ask.ask(proposition, assumptions=True, context={})[source]

Function to evaluate the proposition with assumptions.


proposition : Boolean

Proposition which will be evaluated to boolean value. If this is not AppliedPredicate, it will be wrapped by Q.is_true.

assumptions : Boolean, optional

Local assumptions to evaluate the proposition.

context : AssumptionsContext, optional

Default assumptions to evaluate the proposition. By default, this is sympy.assumptions.global_assumptions variable.


True, False, or None


TypeError : proposition or assumptions is not valid logical expression.

ValueError : assumptions are inconsistent.


This function evaluates the proposition to True or False if the truth value can be determined. If not, it returns None.

It should be discerned from refine() which, when applied to a proposition, simplifies the argument to symbolic Boolean instead of Python built-in True, False or None.


  • ask(proposition)

    Evaluate the proposition in global assumption context.

  • ask(proposition, assumptions)

    Evaluate the proposition with respect to assumptions in global assumption context.


>>> from sympy import ask, Q, pi
>>> from import x, y
>>> ask(Q.rational(pi))
>>> ask(Q.even(x*y), Q.even(x) & Q.integer(y))
>>> ask(*x), Q.integer(x))

If the truth value cannot be determined, None will be returned.

>>> print(ask(Q.odd(3*x))) # cannot determine unless we know x

ValueError is raised if assumptions are inconsistent.

>>> ask(Q.integer(x), Q.even(x) & Q.odd(x))
Traceback (most recent call last):
ValueError: inconsistent assumptions Q.even(x) & Q.odd(x)


Relations in assumptions are not implemented (yet), so the following will not give a meaningful result.

>>> ask(Q.positive(x), x > 0)

It is however a work in progress.

See also


Simplification using assumptions. Proposition is not reduced to None if the truth value cannot be determined.

ask’s optional second argument should be a boolean expression involving assumptions about objects in expr. Valid values include:

  • Q.integer(x)

  • Q.positive(x)

  • Q.integer(x) & Q.positive(x)

  • etc.

Q is an object holding known predicates.

See documentation for the logic module for a complete list of valid boolean expressions.

You can also define a context so you don’t have to pass that argument each time to function ask(). This is done by using the assuming context manager from module sympy.assumptions.

>>> from sympy import *
>>> x = Symbol('x')
>>> y = Symbol('y')
>>> facts = Q.positive(x), Q.positive(y)
>>> with assuming(*facts):
...     print(ask(Q.positive(2*x + y)))


Performance improvements

On queries that involve symbolic coefficients, logical inference is used. Work on improving satisfiable function (sympy.logic.inference.satisfiable) should result in notable speed improvements.

Logic inference used in one ask could be used to speed up further queries, and current system does not take advantage of this. For example, a truth maintenance system ( could be implemented.


You can find more examples in the form of tests in the directory sympy/assumptions/tests/