Sympy 简明教程
SymPy - Querying
SymPy 程序包中的假设模块包含用于提取有关表达式的信息的工具。该模块为此目的定义了 ask() 函数。
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)
algebraic(x)
为了成为代数数,一个数必须是有理系数的非零多项式方程的根。√2 是代数数,因为 √2 是 x2 − 2 = 0 的解。
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(x)
复数谓词。当且仅当 x 属于复数集时,它为真。
Complex number predicate. It is true if and only if x belongs to the set of complex numbers.
composite(x)
composite(x)
ask(Q.composite(x)) 返回的复合数谓词当且仅当 x 为正整数,并且除了 1 和该数本身外,至少有一个正除数时为真。
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
even, odd
ask() 分别返回 x 在偶数集中和奇数集中的真值。
The ask() returns true of x is in the set of even numbers and set of odd numbers respectively.
imaginary
imaginary
此属性表示虚数谓词。当 x 可以表示为一个实数乘以虚数单位 I 时,它为真。
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
integer
Q.integer(x) 返回的此属性返回 x 属于偶数集的真值。
This property returned by Q.integer(x) returns true of x belong to set of even numbers.
rational, irrational
rational, irrational
Q.irrational(x)成立当且仅当 x 是任何不能表示为整数比的实数。例如,π 是一个无理数。
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
positive, negative
检查数字是正还是负的谓词
Predicates to check if number is positive or negative
zero, nonzero
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)