A module which implements predicates and assumption context.

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.

class sympy.assumptions.assume.AssumptionsContext[source]

Set containing default assumptions which are applied to the ask() function.


This is used to represent global assumptions, but you can also use this class to create your own local assumptions contexts. It is basically a thin wrapper to Python’s set, so see its documentation for advanced usage.


The default assumption context is global_assumptions, which is initially empty:

>>> from sympy import ask, Q
>>> from sympy.assumptions import global_assumptions
>>> global_assumptions

You can add default assumptions:

>>> from import x
>>> global_assumptions.add(Q.real(x))
>>> global_assumptions
>>> ask(Q.real(x))

And remove them:

>>> global_assumptions.remove(Q.real(x))
>>> print(ask(Q.real(x)))

The clear() method removes every assumption:

>>> global_assumptions.add(Q.positive(x))
>>> global_assumptions
>>> global_assumptions.clear()
>>> global_assumptions

See also



Add assumptions.

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.UndefinedPredicate(name, handlers=None)[source]

Predicate without handler.


This predicate is generated by using Predicate directly for construction. It does not have a handler, and evaluating this with arguments is done by SAT solver.


>>> from sympy import Predicate, Q
>>> Q.P = Predicate('P')
>>> Q.P.func
<class 'sympy.assumptions.assume.UndefinedPredicate'>

Context manager for assumptions.


>>> from sympy import assuming, Q, ask
>>> from import x, y
>>> print(ask(Q.integer(x + y)))
>>> with assuming(Q.integer(x), Q.integer(y)):
...     print(ask(Q.integer(x + y)))