class SingularPredicate(Predicate): """ 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) >>> ask(Q.singular(X), Q.invertible(X)) False >>> ask(Q.singular(X), ~Q.invertible(X)) True References ========== .. [1] http://mathworld.wolfram.com/SingularMatrix.html """ name = "singular" handler = Dispatcher("SingularHandler", doc="Predicate fore key 'singular'.")
class AlgebraicPredicate(Predicate): r""" Algebraic number predicate. Explanation =========== ``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 >>> ask(Q.algebraic(sqrt(2))) True >>> ask(Q.algebraic(I)) True >>> ask(Q.algebraic(pi)) False References ========== .. [1] https://en.wikipedia.org/wiki/Algebraic_number """ name = 'algebraic' AlgebraicHandler = Dispatcher( "AlgebraicHandler", doc="""Handler for Q.algebraic key.""" )
class InvertiblePredicate(Predicate): """ Invertible matrix predicate. Explanation =========== ``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) >>> 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 References ========== .. [1] https://en.wikipedia.org/wiki/Invertible_matrix """ name = 'invertible' handler = Dispatcher("InvertibleHandler", doc="Handler for Q.invertible.")
class SquarePredicate(Predicate): """ Square matrix predicate. Explanation =========== ``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) >>> ask(Q.square(X)) True >>> ask(Q.square(Y)) False >>> ask(Q.square(ZeroMatrix(3, 3))) True >>> ask(Q.square(Identity(3))) True References ========== .. [1] https://en.wikipedia.org/wiki/Square_matrix """ name = 'square' handler = Dispatcher("SquareHandler", doc="Handler for Q.square.")
class ComplexPredicate(Predicate): """ Complex number predicate. Explanation =========== ``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') >>> ask(Q.complex(0)) True >>> ask(Q.complex(2 + 3*I)) True >>> ask(Q.complex(oo)) False References ========== .. [1] https://en.wikipedia.org/wiki/Complex_number """ name = 'complex' handler = Dispatcher( "ComplexHandler", doc=("Handler for Q.complex.\n\n" "Test that an expression belongs to the field of complex numbers.") )
class ImaginaryPredicate(Predicate): """ Imaginary number predicate. Explanation =========== ``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 >>> ask(Q.imaginary(3*I)) True >>> ask(Q.imaginary(2 + 3*I)) False >>> ask(Q.imaginary(0)) False References ========== .. [1] https://en.wikipedia.org/wiki/Imaginary_number """ name = 'imaginary' handler = Dispatcher( "ImaginaryHandler", doc=("Handler for Q.imaginary.\n\n" "Test that an expression belongs to the field of imaginary numbers,\n" "that is, numbers in the form x*I, where x is real.") )
class SymmetricPredicate(Predicate): """ Symmetric matrix predicate. Explanation =========== ``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) >>> 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 References ========== .. [1] https://en.wikipedia.org/wiki/Symmetric_matrix """ # TODO: Add handlers to make these keys work with # actual matrices and add more examples in the docstring. name = 'symmetric' handler = Dispatcher("SymmetricHandler", doc="Handler for Q.symmetric.")
class ExtendedRealPredicate(Predicate): r""" Extended real predicate. Explanation =========== ``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 >>> ask(Q.extended_real(1)) True >>> ask(Q.extended_real(I)) False >>> ask(Q.extended_real(oo)) True """ name = 'extended_real' handler = Dispatcher( "ExtendedRealHandler", doc=("Handler for Q.extended_real.\n\n" "Test that an expression belongs to the field of extended real\n" "numbers, that is real numbers union {Infinity, -Infinity}.") )
class OddPredicate(Predicate): """ Odd number predicate. Explanation =========== ``ask(Q.odd(x))`` is true iff ``x`` belongs 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 """ name = 'odd' handler = Dispatcher( "OddHandler", doc=("Handler for key 'odd'. Test that an expression represents an odd" " number.") )
class TriangularPredicate(Predicate): """ Triangular matrix predicate. Explanation =========== ``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) >>> ask(Q.triangular(X), Q.upper_triangular(X)) True >>> ask(Q.triangular(X), Q.lower_triangular(X)) True References ========== .. [1] https://en.wikipedia.org/wiki/Triangular_matrix """ name = "triangular" handler = Dispatcher("TriangularHandler", doc="Predicate fore key 'triangular'.")
class EvenPredicate(Predicate): """ Even number predicate. Explanation =========== ``ask(Q.even(x))`` is true iff ``x`` belongs 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 """ name = 'even' handler = Dispatcher("EvenHandler", doc="Handler for key 'even'.")
class CompositePredicate(Predicate): """ Composite number predicate. Explanation =========== ``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 >>> ask(Q.composite(0)) False >>> ask(Q.composite(1)) False >>> ask(Q.composite(2)) False >>> ask(Q.composite(20)) True """ name = 'composite' handler = Dispatcher("CompositeHandler", doc="Handler for key 'composite'.")
class PrimePredicate(Predicate): """ Prime number predicate. Explanation =========== ``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 >>> 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 """ name = 'prime' handler = Dispatcher( "PrimeHandler", doc=("Handler for key 'prime'. Test that an expression represents a prime" " number. When the expression is an exact number, the result (when True)" " is subject to the limitations of isprime() which is used to return the " "result.") )
class DiagonalPredicate(Predicate): """ Diagonal matrix predicate. Explanation =========== ``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) >>> ask(Q.diagonal(ZeroMatrix(3, 3))) True >>> ask(Q.diagonal(X), Q.lower_triangular(X) & ... Q.upper_triangular(X)) True References ========== .. [1] https://en.wikipedia.org/wiki/Diagonal_matrix """ name = "diagonal" handler = Dispatcher("DiagonalHandler", doc="Handler for key 'diagonal'.")
class AntihermitianPredicate(Predicate): """ Antihermitian predicate. Explanation =========== ``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 ========== .. [1] http://mathworld.wolfram.com/HermitianOperator.html """ # TODO: Add examples name = 'antihermitian' handler = Dispatcher( "AntiHermitianHandler", doc=("Handler for Q.antihermitian.\n\n" "Test that an expression belongs to the field of anti-Hermitian\n" "operators, that is, operators in the form x*I, where x is Hermitian.") )
class LowerTriangularPredicate(Predicate): """ Lower triangular matrix predicate. Explanation =========== A matrix $M$ is called lower triangular matrix if :math:`M_{ij}=0` for :math:`i>j`. Examples ======== >>> from sympy import Q, ask, ZeroMatrix, Identity >>> ask(Q.lower_triangular(Identity(3))) True >>> ask(Q.lower_triangular(ZeroMatrix(3, 3))) True References ========== .. [1] http://mathworld.wolfram.com/LowerTriangularMatrix.html """ name = "lower_triangular" handler = Dispatcher("LowerTriangularHandler", doc="Handler for key 'lower_triangular'.")
class PositiveDefinitePredicate(Predicate): r""" Positive definite matrix predicate. Explanation =========== If $M$ is a :math:`n \times n` symmetric real matrix, it is said to be positive definite if :math:`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) >>> 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 References ========== .. [1] https://en.wikipedia.org/wiki/Positive-definite_matrix """ name = "positive_definite" handler = Dispatcher("PositiveDefiniteHandler", doc="Handler for key 'positive_definite'.")
class RationalPredicate(Predicate): """ Rational number predicate. Explanation =========== ``Q.rational(x)`` is true iff ``x`` belongs to the set of rational numbers. 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 References ========== .. [1] https://en.wikipedia.org/wiki/Rational_number """ name = 'rational' handler = Dispatcher( "RationalHandler", doc=("Handler for Q.rational.\n\n" "Test that an expression belongs to the field of rational numbers.") )
class UnitaryPredicate(Predicate): """ Unitary matrix predicate. Explanation =========== ``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) >>> ask(Q.unitary(Y)) False >>> ask(Q.unitary(X*Z*X), Q.unitary(X) & Q.unitary(Z)) True >>> ask(Q.unitary(Identity(3))) True References ========== .. [1] https://en.wikipedia.org/wiki/Unitary_matrix """ name = 'unitary' handler = Dispatcher("UnitaryHandler", doc="Handler for key 'unitary'.")
class FullRankPredicate(Predicate): """ Fullrank matrix predicate. Explanation =========== ``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) >>> ask(Q.fullrank(X.T), Q.fullrank(X)) True >>> ask(Q.fullrank(ZeroMatrix(3, 3))) False >>> ask(Q.fullrank(Identity(3))) True """ name = 'fullrank' handler = Dispatcher("FullRankHandler", doc="Handler for key 'fullrank'.")
class ExtendedNonNegativePredicate(Predicate): """ Nonnegative extended real number predicate. Explanation =========== ``ask(Q.extended_nonnegative(x))`` is true iff ``x`` is extended real and ``x`` is not negative. Examples ======== >>> from sympy import ask, I, oo, Q >>> ask(Q.extended_nonnegative(-1)) False >>> ask(Q.extended_nonnegative(oo)) True >>> ask(Q.extended_nonnegative(0)) True >>> ask(Q.extended_nonnegative(I)) False """ name = 'extended_nonnegative' handler = Dispatcher("ExtendedNonNegativeHandler")
class ZeroPredicate(Predicate): """ Zero number predicate. Explanation =========== ``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') >>> ask(Q.zero(0)) True >>> ask(Q.zero(1/oo)) True >>> print(ask(Q.zero(0*oo))) None >>> ask(Q.zero(1)) False >>> ask(Q.zero(x*y), Q.zero(x) | Q.zero(y)) True """ name = 'zero' handler = Dispatcher("ZeroHandler", doc="Handler for key 'zero'.")
class IrrationalPredicate(Predicate): """ Irrational number predicate. Explanation =========== ``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 >>> ask(Q.irrational(0)) False >>> ask(Q.irrational(S(1)/2)) False >>> ask(Q.irrational(pi)) True >>> ask(Q.irrational(I)) False References ========== .. [1] https://en.wikipedia.org/wiki/Irrational_number """ name = 'irrational' handler = Dispatcher( "IrrationalHandler", doc=("Handler for Q.irrational.\n\n" "Test that an expression is irrational numbers.") )
class IntegerPredicate(Predicate): """ Integer predicate. Explanation =========== ``Q.integer(x)`` is true iff ``x`` belongs to the set of integer numbers. Examples ======== >>> from sympy import Q, ask, S >>> ask(Q.integer(5)) True >>> ask(Q.integer(S(1)/2)) False References ========== .. [1] https://en.wikipedia.org/wiki/Integer """ name = 'integer' handler = Dispatcher( "IntegerHandler", doc=("Handler for Q.integer.\n\n" "Test that an expression belongs to the field of integer numbers.") )
class NegativeInfinitePredicate(Predicate): """ Negative infinity predicate. ``Q.negative_infinite(x)`` is true iff ``x`` is negative infinity ``-oo``. """ name = 'negative_infinite' handler = Dispatcher("NegativeInfiniteHandler")
class PositiveInfinitePredicate(Predicate): """ Positive infinity predicate. ``Q.positive_infinite(x)`` is true iff ``x`` is positive infinity ``oo``. """ name = 'positive_infinite' handler = Dispatcher("PositiveInfiniteHandler")
class IsTruePredicate(Predicate): """ Generic predicate. Explanation =========== ``ask(Q.is_true(x))`` is true iff ``x`` is true. This only makes sense if ``x`` is a boolean object. Examples ======== >>> from sympy import ask, Q >>> from sympy.abc import x, y >>> ask(Q.is_true(True)) True Wrapping another applied predicate just returns the applied predicate. >>> Q.is_true(Q.even(x)) Q.even(x) Wrapping binary relation classes in SymPy core returns applied binary relational predicates. >>> from sympy import Eq, Gt >>> Q.is_true(Eq(x, y)) Q.eq(x, y) >>> Q.is_true(Gt(x, y)) Q.gt(x, y) Notes ===== This class is designed to wrap the boolean objects so that they can behave as if they are applied predicates. Consequently, wrapping another applied predicate is unnecessary and thus it just returns the argument. Also, binary relation classes in SymPy core have binary predicates to represent themselves and thus wrapping them with ``Q.is_true`` converts them to these applied predicates. """ name = 'is_true' handler = Dispatcher( "IsTrueHandler", doc="Wrapper allowing to query the truth value of a boolean expression." ) def __call__(self, arg): # No need to wrap another predicate if isinstance(arg, AppliedPredicate): return arg # Convert relational predicates instead of wrapping them if getattr(arg, "is_Relational", False): pred = binrelpreds[type(arg)] return pred(*arg.args) return super().__call__(arg)
class InfinitePredicate(Predicate): """ Infinite number predicate. ``Q.infinite(x)`` is true iff the absolute value of ``x`` is infinity. """ # TODO: Add examples name = 'infinite' handler = Dispatcher("InfiniteHandler", doc="""Handler for Q.infinite key.""")
class CommutativePredicate(Predicate): """ Commutative predicate. Explanation =========== ``ask(Q.commutative(x))`` is true iff ``x`` commutes with any other object with respect to multiplication operation. """ # TODO: Add examples name = 'commutative' handler = Dispatcher("CommutativeHandler", doc="Handler for key 'commutative'.")
class TranscendentalPredicate(Predicate): """ Transcedental number predicate. Explanation =========== ``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. """ # TODO: Add examples name = 'transcendental' handler = Dispatcher("Transcendental", doc="""Handler for Q.transcendental key.""")