SymPy - Querying


Advertisements

The assumptions module in SymPy package contains tools for extracting information about expressions. The module defines ask() function for this purpose.

sympy.assumptions.ask(property)

Following properties provide useful information about an expression −

algebraic(x)

To be algebraic, a number must be a root of a non-zero polynomial equation with rational coefficients. √2 because √2 is a solution to x2 − 2 = 0, so it is algebraic.

complex(x)

Complex number predicate. It is true if and only if x belongs to the set of complex numbers.

composite(x)

Composite number predicate returned by ask(Q.composite(x)) is true if and only if x is a positive integer and has at least one positive divisor other than 1 and the number itself.

even, odd

The ask() returns true of x is in the set of even numbers and set of odd numbers respectively.

imaginary

This property represents Imaginary number predicate. It is true if x can be written as a real number multiplied by the imaginary unit I.

integer

This property returned by Q.integer(x) returns true of x belong to set of even numbers.

rational, irrational

Q.irrational(x) is true if and only if x is any real number that cannot be expressed as a ratio of integers. For example, pi is an irrational number.

positive, negative

Predicates to check if number is positive or negative

zero, nonzero

Predicates to heck if a number is zero or not

>>> from sympy import * 
>>> x=Symbol('x') 
>>> x=10 
>>> ask(Q.algebraic(pi))
False
>>> ask(Q.complex(5-4*I)), ask( Q.complex(100))
(True, True)
>>> x,y=symbols("x y") 
>>> x,y=5,10 
>>> ask(Q.composite(x)), ask(Q.composite(y))
(False, True)
>>> ask(Q.even(x)), ask(Q.even(y))
(False, True)
>>> x,y= 2*I, 4+5*I 
>>> ask(Q.imaginary(x)), ask(Q.imaginary(y))
(True, False)
>>> x,y=5,10 
>>> ask(Q.even(x)), ask(Q.even(y)), ask(Q.odd(x)), ask(Q.odd(y))
(False, True, True, False)
>>> x,y=5,-5 
>>> ask(Q.positive(x)), ask(Q.negative(y)), ask(Q.positive(x)), ask(Q.negative(y))
(True, True, True, True)
>>> ask(Q.rational(pi)), ask(Q.irrational(S(2)/3))
(False, False)
>>> ask(Q.zero(oo)), ask(Q.nonzero(I))
(False, False)
Advertisements