Ask¶
Module for querying SymPy objects about assumptions.
-
class
sympy.assumptions.ask.AssumptionKeys[source]¶ This class contains all the supported keys by
ask.-
property
algebraic¶ Algebraic number predicate.
Q.algebraic(x)is true iffxbelongs to the set of algebraic numbers.xis algebraic if there is some polynomial inp(x)\in \mathbb\{Q\}[x]such thatp(x) = 0.References
Examples
>>> from sympy import ask, Q, sqrt, I, pi >>> ask(Q.algebraic(sqrt(2))) True >>> ask(Q.algebraic(I)) True >>> ask(Q.algebraic(pi)) False
-
property
antihermitian¶ Antihermitian predicate.
Q.antihermitian(x)is true iffxbelongs to the field of antihermitian operators, i.e., operators in the formx*I, wherexis Hermitian.References
-
property
bounded¶ See documentation of
Q.finite.
-
property
commutative¶ Commutative predicate.
ask(Q.commutative(x))is true iffxcommutes with any other object with respect to multiplication operation.
-
property
complex¶ Complex number predicate.
Q.complex(x)is true iffxbelongs 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') >>> ask(Q.complex(0)) True >>> ask(Q.complex(2 + 3*I)) True >>> ask(Q.complex(oo)) False
-
property
complex_elements¶ Complex elements matrix predicate.
Q.complex_elements(x)is true iff all the elements ofxare complex numbers.Examples
>>> from sympy import Q, ask, MatrixSymbol >>> X = MatrixSymbol('X', 4, 4) >>> ask(Q.complex(X[1, 2]), Q.complex_elements(X)) True >>> ask(Q.complex_elements(X), Q.integer_elements(X)) True
-
property
composite¶ Composite number predicate.
ask(Q.composite(x))is true iffxis a positive integer and has at least one positive divisor other than1and the number itself.Examples
>>> from sympy import Q, ask >>> ask(Q.composite(0)) False >>> ask(Q.composite(1)) False >>> ask(Q.composite(2)) False >>> ask(Q.composite(20)) True
-
property
diagonal¶ Diagonal matrix predicate.
Q.diagonal(x)is true iffxis 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) >>> ask(Q.diagonal(ZeroMatrix(3, 3))) True >>> ask(Q.diagonal(X), Q.lower_triangular(X) & ... Q.upper_triangular(X)) True
-
property
even¶ Even number predicate.
ask(Q.even(x))is true iffxbelongs to the set of even integers.Examples
>>> from sympy import Q, ask, pi >>> ask(Q.even(0)) True >>> ask(Q.even(2)) True >>> ask(Q.even(3)) False >>> ask(Q.even(pi)) False
-
property
extended_real¶ Extended real predicate.
Q.extended_real(x)is true iffxis a real number or \(\{-\infty, \infty\}\).See documentation of
Q.realfor more information about related facts.Examples
>>> from sympy import ask, Q, oo, I >>> ask(Q.extended_real(1)) True >>> ask(Q.extended_real(I)) False >>> ask(Q.extended_real(oo)) True
-
property
finite¶ Finite predicate.
Q.finite(x)is true ifxis neither an infinity nor aNaN. In other words,ask(Q.finite(x))is true for allxhaving a bounded absolute value.References
Examples
>>> from sympy import Q, ask, Symbol, S, oo, I >>> x = Symbol('x') >>> ask(Q.finite(S.NaN)) False >>> ask(Q.finite(oo)) False >>> ask(Q.finite(1)) True >>> ask(Q.finite(2 + 3*I)) True
-
property
fullrank¶ Fullrank matrix predicate.
Q.fullrank(x)is true iffxis 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) >>> ask(Q.fullrank(X.T), Q.fullrank(X)) True >>> ask(Q.fullrank(ZeroMatrix(3, 3))) False >>> ask(Q.fullrank(Identity(3))) True
-
property
hermitian¶ Hermitian predicate.
ask(Q.hermitian(x))is true iffxbelongs to the set of Hermitian operators.References
-
property
imaginary¶ Imaginary number predicate.
Q.imaginary(x)is true iffxcan be written as a real number multiplied by the imaginary unitI. Please note that0is not considered to be an imaginary number.References
Examples
>>> from sympy import Q, ask, I >>> ask(Q.imaginary(3*I)) True >>> ask(Q.imaginary(2 + 3*I)) False >>> ask(Q.imaginary(0)) False
-
property
infinite¶ Infinite number predicate.
Q.infinite(x)is true iff the absolute value ofxis infinity.
-
property
infinitesimal¶ See documentation of
Q.zero.
-
property
infinity¶ See documentation of
Q.infinite.
-
property
integer¶ Integer predicate.
Q.integer(x)is true iffxbelongs to the set of integer numbers.References
Examples
>>> from sympy import Q, ask, S >>> ask(Q.integer(5)) True >>> ask(Q.integer(S(1)/2)) False
-
property
integer_elements¶ Integer elements matrix predicate.
Q.integer_elements(x)is true iff all the elements ofxare integers.Examples
>>> from sympy import Q, ask, MatrixSymbol >>> X = MatrixSymbol('X', 4, 4) >>> ask(Q.integer(X[1, 2]), Q.integer_elements(X)) True
-
property
invertible¶ Invertible matrix predicate.
Q.invertible(x)is true iffxis 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) >>> ask(Q.invertible(X*Y), Q.invertible(X)) False >>> ask(Q.invertible(X*Z), Q.invertible(X) & Q.invertible(Z)) True >>> ask(Q.invertible(X), Q.fullrank(X) & Q.square(X)) True
-
property
irrational¶ Irrational number predicate.
Q.irrational(x)is true iffxis any real number that cannot be expressed as a ratio of integers.References
Examples
>>> from sympy import ask, Q, pi, S, I >>> ask(Q.irrational(0)) False >>> ask(Q.irrational(S(1)/2)) False >>> ask(Q.irrational(pi)) True >>> ask(Q.irrational(I)) False
-
property
is_true¶ Generic predicate.
ask(Q.is_true(x))is true iffxis true. This only makes sense ifxis a predicate.Examples
>>> from sympy import ask, Q, symbols >>> x = symbols('x') >>> ask(Q.is_true(True)) True
-
property
lower_triangular¶ Lower triangular matrix predicate.
A matrix
Mis called lower triangular matrix if \(a_{ij}=0\) for \(i>j\).References
Examples
>>> from sympy import Q, ask, ZeroMatrix, Identity >>> ask(Q.lower_triangular(Identity(3))) True >>> ask(Q.lower_triangular(ZeroMatrix(3, 3))) True
-
property
negative¶ Negative number predicate.
Q.negative(x)is true iffxis 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.nonnegativeand~Q.negativeare not the same thing.~Q.negative(x)simply means thatxis not negative, whereasQ.nonnegative(x)means thatxis real and not negative, i.e.,Q.nonnegative(x)is logically equivalent toQ.zero(x) | Q.positive(x). So for example,~Q.negative(I)is true, whereasQ.nonnegative(I)is false.See the documentation of
Q.realfor 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 >>> ask(Q.negative(-1)) True >>> ask(Q.nonnegative(I)) False >>> ask(~Q.negative(I)) True
-
property
nonnegative¶ Nonnegative real number predicate.
ask(Q.nonnegative(x))is true iffxbelongs to the set of positive numbers including zero.Note that
Q.nonnegativeand~Q.negativeare not the same thing.~Q.negative(x)simply means thatxis not negative, whereasQ.nonnegative(x)means thatxis real and not negative, i.e.,Q.nonnegative(x)is logically equivalent toQ.zero(x) | Q.positive(x). So for example,~Q.negative(I)is true, whereasQ.nonnegative(I)is false.
Examples
>>> from sympy import Q, ask, I >>> ask(Q.nonnegative(1)) True >>> ask(Q.nonnegative(0)) True >>> ask(Q.nonnegative(-1)) False >>> ask(Q.nonnegative(I)) False >>> ask(Q.nonnegative(-I)) False
-
property
nonpositive¶ Nonpositive real number predicate.
ask(Q.nonpositive(x))is true iffxbelongs to the set of negative numbers including zero.Note that
Q.nonpositiveand~Q.positiveare not the same thing.~Q.positive(x)simply means thatxis not positive, whereasQ.nonpositive(x)means thatxis 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, whereasQ.nonpositive(I)is false.
Examples
>>> from sympy import Q, ask, I >>> ask(Q.nonpositive(-1)) True >>> ask(Q.nonpositive(0)) True >>> ask(Q.nonpositive(1)) False >>> ask(Q.nonpositive(I)) False >>> ask(Q.nonpositive(-I)) False
-
property
nonzero¶ Nonzero real number predicate.
ask(Q.nonzero(x))is true iffxis real andxis not zero. Note in particular thatQ.nonzero(x)is false ifxis 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.nonzerois logically equivalent toQ.positive | Q.negative.See the documentation of
Q.realfor more information about related facts.
Examples
>>> from sympy import Q, ask, symbols, I, oo >>> x = symbols('x') >>> print(ask(Q.nonzero(x), ~Q.zero(x))) None >>> ask(Q.nonzero(x), Q.positive(x)) True >>> ask(Q.nonzero(x), Q.zero(x)) False >>> ask(Q.nonzero(0)) False >>> ask(Q.nonzero(I)) False >>> ask(~Q.zero(I)) True >>> ask(Q.nonzero(oo)) False
-
property
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) >>> ask(Q.normal(X), Q.unitary(X)) True
-
property
odd¶ Odd number predicate.
ask(Q.odd(x))is true iffxbelongs to the set of odd numbers.Examples
>>> from sympy import Q, ask, pi >>> ask(Q.odd(0)) False >>> ask(Q.odd(2)) False >>> ask(Q.odd(3)) True >>> ask(Q.odd(pi)) False
-
property
orthogonal¶ Orthogonal matrix predicate.
Q.orthogonal(x)is true iffxis an orthogonal matrix. A square matrixMis an orthogonal matrix if it satisfiesM^TM = MM^T = IwhereM^Tis the transpose matrix ofMandIis 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) >>> ask(Q.orthogonal(Y)) False >>> ask(Q.orthogonal(X*Z*X), Q.orthogonal(X) & Q.orthogonal(Z)) True >>> ask(Q.orthogonal(Identity(3))) True >>> ask(Q.invertible(X), Q.orthogonal(X)) True
-
property
positive¶ Positive real number predicate.
Q.positive(x)is true iffxis real and \(x > 0\), that is ifxis in the interval \((0, \infty)\). In particular, infinity is not positive.A few important facts about positive numbers:
Note that
Q.nonpositiveand~Q.positiveare not the same thing.~Q.positive(x)simply means thatxis not positive, whereasQ.nonpositive(x)means thatxis 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, whereasQ.nonpositive(I)is false.See the documentation of
Q.realfor 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 >>> ask(Q.positive(1)) True >>> ask(Q.nonpositive(I)) False >>> ask(~Q.positive(I)) True
-
property
positive_definite¶ Positive definite matrix predicate.
If
Mis a :math:n \times nsymmetric real matrix, it is said to be positive definite if \(Z^TMZ\) is positive for every non-zero column vectorZofnreal 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) >>> ask(Q.positive_definite(Y)) False >>> ask(Q.positive_definite(Identity(3))) True >>> ask(Q.positive_definite(X + Z), Q.positive_definite(X) & ... Q.positive_definite(Z)) True
-
property
prime¶ Prime number predicate.
ask(Q.prime(x))is true iffxis a natural number greater than 1 that has no positive divisors other than1and the number itself.Examples
>>> from sympy import Q, ask >>> ask(Q.prime(0)) False >>> ask(Q.prime(1)) False >>> ask(Q.prime(2)) True >>> ask(Q.prime(20)) False >>> ask(Q.prime(-3)) False
-
property
rational¶ Rational number predicate.
Q.rational(x)is true iffxbelongs to the set of rational numbers.References
https://en.wikipedia.org/wiki/Rational_number
Examples
>>> from sympy import ask, Q, pi, S >>> ask(Q.rational(0)) True >>> ask(Q.rational(S(1)/2)) True >>> ask(Q.rational(pi)) False
-
property
real¶ Real number predicate.
Q.real(x)is true iffxis a real number, i.e., it is in the interval \((-\infty, \infty)\). Note that, in particular the infinities are not real. UseQ.extended_realif 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, andQ.irrationalall implyQ.real, as do all facts that imply those facts.The facts
Q.algebraic, andQ.transcendentaldo not implyQ.real; they implyQ.complex. An algebraic or transcendental number may or may not be real.The “non” facts (i.e.,
Q.nonnegative,Q.nonzero,Q.nonpositiveandQ.noninteger) are not equivalent to not the fact, but rather, not the fact andQ.real. For example,Q.nonnegativemeans~Q.negative & Q.real. So for example,Iis not nonnegative, nonzero, or nonpositive.
References
Examples
>>> from sympy import Q, ask, symbols >>> x = symbols('x') >>> ask(Q.real(x), Q.positive(x)) True >>> ask(Q.real(0)) True
-
property
real_elements¶ Real elements matrix predicate.
Q.real_elements(x)is true iff all the elements ofxare real numbers.Examples
>>> from sympy import Q, ask, MatrixSymbol >>> X = MatrixSymbol('X', 4, 4) >>> ask(Q.real(X[1, 2]), Q.real_elements(X)) True
-
property
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) >>> ask(Q.singular(X), Q.invertible(X)) False >>> ask(Q.singular(X), ~Q.invertible(X)) True
-
property
square¶ Square matrix predicate.
Q.square(x)is true iffxis 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) >>> ask(Q.square(X)) True >>> ask(Q.square(Y)) False >>> ask(Q.square(ZeroMatrix(3, 3))) True >>> ask(Q.square(Identity(3))) True
-
property
symmetric¶ Symmetric matrix predicate.
Q.symmetric(x)is true iffxis 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) >>> ask(Q.symmetric(X*Z), Q.symmetric(X) & Q.symmetric(Z)) True >>> ask(Q.symmetric(X + Z), Q.symmetric(X) & Q.symmetric(Z)) True >>> ask(Q.symmetric(Y)) False
-
property
transcendental¶ Transcedental number predicate.
Q.transcendental(x)is true iffxbelongs 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 ifXis one that is either lower triangular or upper triangular.References
Examples
>>> from sympy import Q, ask, MatrixSymbol >>> X = MatrixSymbol('X', 4, 4) >>> ask(Q.triangular(X), Q.upper_triangular(X)) True >>> ask(Q.triangular(X), Q.lower_triangular(X)) True
-
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) >>> ask(Q.triangular(X), Q.unit_triangular(X)) True
-
property
unitary¶ Unitary matrix predicate.
Q.unitary(x)is true iffxis a unitary matrix. Unitary matrix is an analogue to orthogonal matrix. A square matrixMwith complex elements is unitary if :math:M^TM = MM^T= Iwhere :math:M^Tis the conjugate transpose matrix ofM.References
Examples
>>> from sympy import Q, ask, MatrixSymbol, Identity >>> X = MatrixSymbol('X', 2, 2) >>> Y = MatrixSymbol('Y', 2, 3) >>> Z = MatrixSymbol('Z', 2, 2) >>> ask(Q.unitary(Y)) False >>> ask(Q.unitary(X*Z*X), Q.unitary(X) & Q.unitary(Z)) True >>> ask(Q.unitary(Identity(3))) True
-
property
upper_triangular¶ Upper triangular matrix predicate.
A matrix
Mis called upper triangular matrix if \(M_{ij}=0\) for \(i<j\).References
Examples
>>> from sympy import Q, ask, ZeroMatrix, Identity >>> ask(Q.upper_triangular(Identity(3))) True >>> ask(Q.upper_triangular(ZeroMatrix(3, 3))) True
-
property
zero¶ Zero number predicate.
ask(Q.zero(x))is true iff the value ofxis zero.Examples
>>> from sympy import ask, Q, oo, symbols >>> x, y = symbols('x, y') >>> ask(Q.zero(0)) True >>> ask(Q.zero(1/oo)) True >>> ask(Q.zero(0*oo)) False >>> ask(Q.zero(1)) False >>> ask(Q.zero(x*y), Q.zero(x) | Q.zero(y)) True
-
property
-
sympy.assumptions.ask.ask(proposition, assumptions=True, context={})[source]¶ Method for inferring properties about objects.
Syntax
ask(proposition)
ask(proposition, assumptions)
where
propositionis 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(4*x), Q.integer(x)) 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_factsandget_known_facts_keysfunctions 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 >>> 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): ... from sympy import log ... return ask(Q.integer(log(expr + 1, 2))) >>> register_handler('mersenne', MersenneHandler) >>> ask(Q.mersenne(7)) True