Module for querying SymPy objects about assumptions.

class sympy.assumptions.ask.AssumptionKeys[source]

This class contains all the supported keys by ask. It should be accessed via the instance sympy.Q.

property 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.

Examples

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


References

R1

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

property 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

R2

http://mathworld.wolfram.com/HermitianOperator.html

property bounded

See documentation of Q.finite.

property commutative

Commutative predicate.

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

property 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.

Examples

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


References

R3

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

property 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

property 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

property 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.

Examples

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


References

R4

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

property 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

property 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

property 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.

Examples

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


References

R5

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

property 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

property hermitian

Hermitian predicate.

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

References

R6

http://mathworld.wolfram.com/HermitianOperator.html

property 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.

Examples

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


References

R7

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

property infinite

Infinite number predicate.

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

property infinitesimal

See documentation of Q.zero.

property infinity

See documentation of Q.infinite.

property integer

Integer predicate.

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

Examples

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


References

R8

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

property 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

property 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.

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


References

R9

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

property irrational

Irrational number predicate.

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

Examples

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


References

R10

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

property 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

property lower_triangular

Lower triangular matrix predicate.

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

Examples

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


References

R11

http://mathworld.wolfram.com/LowerTriangularMatrix.html

property 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

property 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

property 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

>>> ask(Q.nonpositive(-1))
True
True
False
False
False

property 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

property normal

Normal matrix predicate.

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

Examples

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


References

R12

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

property 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

property 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.

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


References

R13

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

property 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

property 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.

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


References

R14

https://en.wikipedia.org/wiki/Positive-definite_matrix

property 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

property rational

Rational number predicate.

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

Examples

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


References

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

property 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.

Examples

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


References

R15

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

property 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

property singular

Singular matrix predicate.

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

Examples

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


References

R16

http://mathworld.wolfram.com/SingularMatrix.html

property 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.

Examples

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


References

R17

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

property 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.

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


References

R18

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

property 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.

property triangular

Triangular matrix predicate.

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

Examples

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


References

R19

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

property 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

property 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.

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


References

R20

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

property upper_triangular

Upper triangular matrix predicate.

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

Examples

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


References

R21

http://mathworld.wolfram.com/UpperTriangularMatrix.html

property 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={})[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]