Queries are used to ask information about expressions. Main method for this is ask():
Method for inferring properties about objects.
Syntax
ask(proposition)
ask(proposition, assumptions)
where proposition is any boolean expression
Examples
>>> from sympy import ask, Q, pi
>>> from sympy.abc import x, y
>>> ask(Q.rational(pi))
False
>>> ask(Q.even(x*y), Q.even(x) & Q.integer(y))
True
>>> ask(Q.prime(x*y), Q.integer(x) & Q.integer(y))
False
Relations in assumptions are not implemented (yet), so the following will not give a meaningful result.
>>> ask(Q.positive(x), Q.is_true(x > 0))
It is however a work in progress.
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 a class in sympy.assumptions holding known predicates.
See documentation for the logic module for a complete list of valid boolean expressions.
You can also define global assumptions so you don’t have to pass that argument each time to function ask(). This is done by using the global_assumptions object from module sympy.assumptions. You can then clear global assumptions with global_assumptions.clear():
>>> from sympy import *
>>> x = Symbol('x')
>>> global_assumptions.add(Q.positive(x))
>>> ask(Q.positive(x))
True
>>> global_assumptions.clear()
Test that a function is bounded with respect to its variables. For example, sin(x) is a bounded functions, but exp(x) is not.
Examples:
>>> from sympy import *
>>> x = Symbol('x')
>>> ask(Q.bounded(exp(x)), ~Q.bounded(x))
False
>>> ask(Q.bounded(exp(x)) , Q.bounded(x))
True
>>> ask(Q.bounded(sin(x)), ~Q.bounded(x))
True
Test that objects are commutative. By default, symbols in SymPy are considered commutative except otherwise stated.
Examples:
>>> from sympy import *
>>> x, y = symbols('x,y')
>>> ask(Q.commutative(x))
True
>>> ask(Q.commutative(x), ~Q.commutative(x))
False
>>> ask(Q.commutative(x*y), ~Q.commutative(x))
False
Test that expression belongs to the field of complex numbers.
Examples:
>>> from sympy import *
>>> ask(Q.complex(2))
True
>>> ask(Q.complex(I))
True
>>> x, y = symbols('x,y')
>>> ask(Q.complex(x+I*y), Q.real(x) & Q.real(y))
True
Test that expression represents an even number, that is, an number that can be written in the form 2*n, n integer.
Examples:
>>> from sympy import *
>>> ask(Q.even(2))
True
>>> n = Symbol('n')
>>> ask(Q.even(2*n), Q.integer(n))
True
Test that an expression belongs to the field of extended real numbers, that is, real numbers union {Infinity, -Infinity}.
Examples:
>>> from sympy import *
>>> ask(Q.extended_real(oo))
True
>>> ask(Q.extended_real(2))
True
>>> ask(Q.extended_real(x), Q.real(x))
True
Examples:
>>> from sympy import *
>>> ask(Q.imaginary(2*I))
True
>>> x = Symbol('x')
>>> ask(Q.imaginary(x*I), Q.real(x))
True
Test that an expression is equivalent to an infinitesimal number.
Examples:
>>> from sympy import *
>>> ask(Q.infinitesimal(1/oo))
True
>>> x, y = symbols('x,y')
>>> ask(Q.infinitesimal(2*x), Q.infinitesimal(x))
True
>>> ask(Q.infinitesimal(x*y), Q.infinitesimal(x) & Q.bounded(y))
True
Test that an expression belongs to the set of integer numbers.
Examples:
>>> from sympy import *
>>> ask(Q.integer(2))
True
>>> ask(Q.integer(sqrt(2)))
False
>>> x = Symbol('x')
>>> ask(Q.integer(x/2), Q.even(x))
True
Test that an expression represents an irrational number.
Examples:
>>> from sympy import *
>>> ask(Q.irrational(pi))
True
>>> ask(Q.irrational(sqrt(2)))
True
>>> ask(Q.irrational(x*sqrt(2)), Q.rational(x))
True
Test that an expression represents a rational number.
Examples:
>>> from sympy import *
>>> ask(Q.rational(Rational(3, 4)))
True
>>> x, y = symbols('x,y')
>>> ask(Q.rational(x/2), Q.integer(x))
True
>>> ask(Q.rational(x/y), Q.integer(x) & Q.integer(y))
True
Test that an expression is less (strict) than zero.
Examples:
>>> from sympy import *
>>> ask(Q.negative(0.3))
False
>>> x = Symbol('x')
>>> ask(Q.negative(-x), Q.positive(x))
True
negative numbers are defined as real numbers that are not zero nor positive, so complex numbers (with nontrivial imaginary coefficients) will return False for this predicate. The same applies to Q.positive.
Test that a given expression is greater (strict) than zero.
Examples:
>>> from sympy import *
>>> ask(Q.positive(0.3))
True
>>> x = Symbol('x')
>>> ask(Q.positive(-x), Q.negative(x))
True
see Remarks for negative
Test that an expression represents a prime number.
Examples:
>>> from sympy import *
>>> ask(Q.prime(13))
True
Remarks: Use sympy.ntheory.isprime to test numeric values efficiently.
Test that an expression belongs to the field of real numbers.
Examples:
>>> from sympy import *
>>> ask(Q.real(sqrt(2)))
True
>>> x, y = symbols('x,y')
>>> ask(Q.real(x*y), Q.real(x) & Q.real(y))
True
Test that an expression represents an odd number.
Examples:
>>> from sympy import *
>>> ask(Q.odd(3))
True
>>> n = Symbol('n')
>>> ask(Q.odd(2*n + 1), Q.integer(n))
True
Test that an expression is not zero.
Examples:
>>> from sympy import *
>>> x = Symbol('x')
>>> ask(Q.nonzero(x), Q.positive(x) | Q.negative(x))
True
Each time ask is called, the appropriate Handler for the current key is called. This is always a subclass of sympy.assumptions.AskHandler. It’s classmethods have the name’s of the classes it supports. For example, a (simplified) AskHandler for the ask ‘positive’ would look like this:
class AskPositiveHandler(CommonHandler):
def Mul(self):
# return True if all argument's in self.expr.args are positive
...
def Add(self):
for arg in self.expr.args:
if not ask(arg, positive, self.assumptions):
break
else:
# if all argument's are positive
return True
...
The .Mul() method is called when self.expr is an instance of Mul, the Add method would be called when self.expr is an instance of Add and so on.
Register a handler in the ask system. key must be a string and handler a class inheriting from AskHandler:
>>> from sympy.assumptions import register_handler, ask, Q
>>> from sympy.assumptions.handlers import AskHandler
>>> class MersenneHandler(AskHandler):
... # Mersenne numbers are in the form 2**n + 1, n integer
... @staticmethod
... def Integer(expr, assumptions):
... import math
... return ask(Q.integer(math.log(expr + 1, 2)))
>>> register_handler('mersenne', MersenneHandler)
>>> ask(Q.mersenne(7))
True
You can undo this operation by calling remove_handler.
Removes a handler from the ask system. Same syntax as register_handler
You can support new types [1] by adding a handler to an existing key. In the following example, we will create a new type MyType and extend the key ‘prime’ to accept this type (and return True)
>>> from sympy.core import Basic
>>> from sympy.assumptions import register_handler
>>> from sympy.assumptions.handlers import AskHandler
>>> class MyType(Basic):
... pass
>>> class MyAskHandler(AskHandler):
... @staticmethod
... def MyType(expr, assumptions):
... return True
>>> a = MyType()
>>> register_handler('prime', MyAskHandler)
>>> ask(Q.prime(a))
True
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 (http://en.wikipedia.org/wiki/Truth_maintenance_system) could be implemented.