Module for querying SymPy objects about assumptions.

class sympy.assumptions.ask.AssumptionKeys[source]

This class contains all the supported keys by ask.

algebraic

Algebraic number predicate.

Q.algebraic(x) is true iff x belongs to the set of algebraic numbers. x is algebraic if there is some polynomial in p(x)\in \mathbb\{Q\}[x] such that p(x) = 0.

References

Examples

>>> from sympy import ask, Q, sqrt, I, pi
True
True
False

antihermitian

Antihermitian predicate.

Q.antihermitian(x) is true iff x belongs to the field of antihermitian operators, i.e., operators in the form x*I, where x is Hermitian.

References

bounded

See documentation of Q.finite.

commutative

Commutative predicate.

ask(Q.commutative(x)) is true iff x commutes with any other object with respect to multiplication operation.

complex

Complex number predicate.

Q.complex(x) is true iff x belongs to the set of complex numbers. Note that every complex number is finite.

References

Examples

>>> from sympy import Q, Symbol, ask, I, oo
>>> x = Symbol('x')
True
True
False

complex_elements

Complex elements matrix predicate.

Q.complex_elements(x) is true iff all the elements of x are complex numbers.

Examples

>>> from sympy import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
True
True

composite

Composite number predicate.

ask(Q.composite(x)) is true iff x is a positive integer and has at least one positive divisor other than 1 and the number itself.

Examples

>>> from sympy import Q, ask
False
False
False
True

diagonal

Diagonal matrix predicate.

Q.diagonal(x) is true iff x is a diagonal matrix. A diagonal matrix is a matrix in which the entries outside the main diagonal are all zero.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol, ZeroMatrix
>>> X = MatrixSymbol('X', 2, 2)
True
...     Q.upper_triangular(X))
True

even

Even number predicate.

ask(Q.even(x)) is true iff x belongs to the set of even integers.

Examples

>>> from sympy import Q, ask, pi
True
True
False
False

extended_real

Extended real predicate.

Q.extended_real(x) is true iff x is a real number or $$\{-\infty, \infty\}$$.

See documentation of Q.real for more information about related facts.

Examples

>>> from sympy import ask, Q, oo, I
True
False
True

finite

Finite predicate.

Q.finite(x) is true if x is neither an infinity nor a NaN. In other words, ask(Q.finite(x)) is true for all x having a bounded absolute value.

References

Examples

>>> from sympy import Q, ask, Symbol, S, oo, I
>>> x = Symbol('x')
False
False
True
True

fullrank

Fullrank matrix predicate.

Q.fullrank(x) is true iff x is a full rank matrix. A matrix is full rank if all rows and columns of the matrix are linearly independent. A square matrix is full rank iff its determinant is nonzero.

Examples

>>> from sympy import Q, ask, MatrixSymbol, ZeroMatrix, Identity
>>> X = MatrixSymbol('X', 2, 2)
True
False
True

hermitian

Hermitian predicate.

ask(Q.hermitian(x)) is true iff x belongs to the set of Hermitian operators.

References

imaginary

Imaginary number predicate.

Q.imaginary(x) is true iff x can be written as a real number multiplied by the imaginary unit I. Please note that 0 is not considered to be an imaginary number.

References

Examples

>>> from sympy import Q, ask, I
True
False
False

infinite

Infinite number predicate.

Q.infinite(x) is true iff the absolute value of x is infinity.

infinitesimal

See documentation of Q.zero.

infinity

See documentation of Q.infinite.

integer

Integer predicate.

Q.integer(x) is true iff x belongs to the set of integer numbers.

References

Examples

>>> from sympy import Q, ask, S
True
False

integer_elements

Integer elements matrix predicate.

Q.integer_elements(x) is true iff all the elements of x are integers.

Examples

>>> from sympy import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
True

invertible

Invertible matrix predicate.

Q.invertible(x) is true iff x is an invertible matrix. A square matrix is called invertible only if its determinant is 0.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
False
True
True

irrational

Irrational number predicate.

Q.irrational(x) is true iff x is any real number that cannot be expressed as a ratio of integers.

References

Examples

>>> from sympy import ask, Q, pi, S, I
False
False
True
False

is_true

Generic predicate.

ask(Q.is_true(x)) is true iff x is true. This only makes sense if x is a predicate.

Examples

>>> from sympy import ask, Q, symbols
>>> x = symbols('x')
True

lower_triangular

Lower triangular matrix predicate.

A matrix M is called lower triangular matrix if $$a_{ij}=0$$ for $$i>j$$.

References

Examples

>>> from sympy import Q, ask, ZeroMatrix, Identity
True
True

negative

Negative number predicate.

Q.negative(x) is true iff x is a real number and $$x < 0$$, that is, it is in the interval $$(-\infty, 0)$$. Note in particular that negative infinity is not negative.

A few important facts about negative numbers:

• Note that Q.nonnegative and ~Q.negative are not the same thing. ~Q.negative(x) simply means that x is not negative, whereas Q.nonnegative(x) means that x is real and not negative, i.e., Q.nonnegative(x) is logically equivalent to Q.zero(x) | Q.positive(x). So for example, ~Q.negative(I) is true, whereas Q.nonnegative(I) is false.
• See the documentation of Q.real for more information about related facts.

Examples

>>> from sympy import Q, ask, symbols, I
>>> x = symbols('x')
>>> ask(Q.negative(x), Q.real(x) & ~Q.positive(x) & ~Q.zero(x))
True
True
False
True

nonnegative

Nonnegative real number predicate.

ask(Q.nonnegative(x)) is true iff x belongs to the set of positive numbers including zero.

• Note that Q.nonnegative and ~Q.negative are not the same thing. ~Q.negative(x) simply means that x is not negative, whereas Q.nonnegative(x) means that x is real and not negative, i.e., Q.nonnegative(x) is logically equivalent to Q.zero(x) | Q.positive(x). So for example, ~Q.negative(I) is true, whereas Q.nonnegative(I) is false.

Examples

>>> from sympy import Q, ask, I
True
True
False
False
False

nonpositive

Nonpositive real number predicate.

ask(Q.nonpositive(x)) is true iff x belongs to the set of negative numbers including zero.

• Note that Q.nonpositive and ~Q.positive are not the same thing. ~Q.positive(x) simply means that x is not positive, whereas Q.nonpositive(x) means that x is real and not positive, i.e., Q.nonpositive(x) is logically equivalent to $$Q.negative(x) | Q.zero(x)$$. So for example, ~Q.positive(I) is true, whereas Q.nonpositive(I) is false.

Examples

>>> from sympy import Q, ask, I
True
True
False
False
False

nonzero

Nonzero real number predicate.

ask(Q.nonzero(x)) is true iff x is real and x is not zero. Note in particular that Q.nonzero(x) is false if x is not real. Use ~Q.zero(x) if you want the negation of being zero without any real assumptions.

A few important facts about nonzero numbers:

• Q.nonzero is logically equivalent to Q.positive | Q.negative.
• See the documentation of Q.real for more information about related facts.

Examples

>>> from sympy import Q, ask, symbols, I, oo
>>> x = symbols('x')
None
True
False
False
False
True
False

normal

Normal matrix predicate.

A matrix is normal if it commutes with its conjugate transpose.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
True

odd

Odd number predicate.

ask(Q.odd(x)) is true iff x belongs to the set of odd numbers.

Examples

>>> from sympy import Q, ask, pi
False
False
True
False

orthogonal

Orthogonal matrix predicate.

Q.orthogonal(x) is true iff x is an orthogonal matrix. A square matrix M is an orthogonal matrix if it satisfies M^TM = MM^T = I where M^T is the transpose matrix of M and I is an identity matrix. Note that an orthogonal matrix is necessarily invertible.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol, Identity
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
False
True
True
True

positive

Positive real number predicate.

Q.positive(x) is true iff x is real and $$x > 0$$, that is if x is in the interval $$(0, \infty)$$. In particular, infinity is not positive.

A few important facts about positive numbers:

• Note that Q.nonpositive and ~Q.positive are not the same thing. ~Q.positive(x) simply means that x is not positive, whereas Q.nonpositive(x) means that x is real and not positive, i.e., Q.nonpositive(x) is logically equivalent to $$Q.negative(x) | Q.zero(x)$$. So for example, ~Q.positive(I) is true, whereas Q.nonpositive(I) is false.
• See the documentation of Q.real for more information about related facts.

Examples

>>> from sympy import Q, ask, symbols, I
>>> x = symbols('x')
>>> ask(Q.positive(x), Q.real(x) & ~Q.negative(x) & ~Q.zero(x))
True
True
False
True

positive_definite

Positive definite matrix predicate.

If M is a :math:n \times n symmetric real matrix, it is said to be positive definite if $$Z^TMZ$$ is positive for every non-zero column vector Z of n real numbers.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol, Identity
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
False
True
>>> ask(Q.positive_definite(X + Z), Q.positive_definite(X) &
...     Q.positive_definite(Z))
True

prime

Prime number predicate.

ask(Q.prime(x)) is true iff x is a natural number greater than 1 that has no positive divisors other than 1 and the number itself.

Examples

>>> from sympy import Q, ask
False
False
True
False
False

rational

Rational number predicate.

Q.rational(x) is true iff x belongs to the set of rational numbers.

References

https://en.wikipedia.org/wiki/Rational_number

Examples

>>> from sympy import ask, Q, pi, S
True
True
False

real

Real number predicate.

Q.real(x) is true iff x is a real number, i.e., it is in the interval $$(-\infty, \infty)$$. Note that, in particular the infinities are not real. Use Q.extended_real if you want to consider those as well.

A few important facts about reals:

• Every real number is positive, negative, or zero. Furthermore, because these sets are pairwise disjoint, each real number is exactly one of those three.
• Every real number is also complex.
• Every real number is finite.
• Every real number is either rational or irrational.
• Every real number is either algebraic or transcendental.
• The facts Q.negative, Q.zero, Q.positive, Q.nonnegative, Q.nonpositive, Q.nonzero, Q.integer, Q.rational, and Q.irrational all imply Q.real, as do all facts that imply those facts.
• The facts Q.algebraic, and Q.transcendental do not imply Q.real; they imply Q.complex. An algebraic or transcendental number may or may not be real.
• The “non” facts (i.e., Q.nonnegative, Q.nonzero, Q.nonpositive and Q.noninteger) are not equivalent to not the fact, but rather, not the fact and Q.real. For example, Q.nonnegative means ~Q.negative & Q.real. So for example, I is not nonnegative, nonzero, or nonpositive.

References

Examples

>>> from sympy import Q, ask, symbols
>>> x = symbols('x')
True
True

real_elements

Real elements matrix predicate.

Q.real_elements(x) is true iff all the elements of x are real numbers.

Examples

>>> from sympy import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
True

singular

Singular matrix predicate.

A matrix is singular iff the value of its determinant is 0.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
False
True

square

Square matrix predicate.

Q.square(x) is true iff x is a square matrix. A square matrix is a matrix with the same number of rows and columns.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol, ZeroMatrix, Identity
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('X', 2, 3)
True
False
True
True

symmetric

Symmetric matrix predicate.

Q.symmetric(x) is true iff x is a square matrix and is equal to its transpose. Every square diagonal matrix is a symmetric matrix.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
True
>>> ask(Q.symmetric(X + Z), Q.symmetric(X) & Q.symmetric(Z))
True
False

transcendental

Transcedental number predicate.

Q.transcendental(x) is true iff x belongs to the set of transcendental numbers. A transcendental number is a real or complex number that is not algebraic.

triangular

Triangular matrix predicate.

Q.triangular(X) is true if X is one that is either lower triangular or upper triangular.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
True
True

unit_triangular

Unit triangular matrix predicate.

A unit triangular matrix is a triangular matrix with 1s on the diagonal.

Examples

>>> from sympy import Q, ask, MatrixSymbol
>>> X = MatrixSymbol('X', 4, 4)
True

unitary

Unitary matrix predicate.

Q.unitary(x) is true iff x is a unitary matrix. Unitary matrix is an analogue to orthogonal matrix. A square matrix M with complex elements is unitary if :math:M^TM = MM^T= I where :math:M^T is the conjugate transpose matrix of M.

References

Examples

>>> from sympy import Q, ask, MatrixSymbol, Identity
>>> X = MatrixSymbol('X', 2, 2)
>>> Y = MatrixSymbol('Y', 2, 3)
>>> Z = MatrixSymbol('Z', 2, 2)
False
True
True

upper_triangular

Upper triangular matrix predicate.

A matrix M is called upper triangular matrix if $$M_{ij}=0$$ for $$i<j$$.

References

Examples

>>> from sympy import Q, ask, ZeroMatrix, Identity
True
True

zero

Zero number predicate.

ask(Q.zero(x)) is true iff the value of x is zero.

Examples

>>> from sympy import ask, Q, oo, symbols
>>> x, y = symbols('x, y')
True
True
False
False
True

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

Method for inferring properties about objects.

Syntax

where proposition is any boolean expression

Examples

>>> from sympy import ask, Q, pi
>>> from sympy.abc import x, y
False
True
False

Remarks

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.

sympy.assumptions.ask.ask_full_inference(proposition, assumptions, known_facts_cnf)[source]

Method for inferring properties about objects.

sympy.assumptions.ask.compute_known_facts(known_facts, known_facts_keys)[source]

Compute the various forms of knowledge compilation used by the assumptions system.

This function is typically applied to the results of the get_known_facts and get_known_facts_keys functions defined at the bottom of this file.

sympy.assumptions.ask.register_handler(key, handler)[source]

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

sympy.assumptions.ask.remove_handler(key, handler)[source]