Sympy 简明教程
SymPy - Logical Expressions
布尔函数在 sympy.basic.booleanarg module 中定义。可以使用标准 python 运算符 &(And)、|(Or)、~(Not)以及 >> 和 << 来构建布尔表达式。布尔表达式继承自 SymPy 核心模块中定义的 Basic 类。
Boolean functions are defined in sympy.basic.booleanarg module. It is possible to build Boolean expressions with the standard python operators & (And), | (Or), ~ (Not) as well as with >> and <<. Boolean expressions inherit from Basic class defined in SymPy’s core module.
BooleanTrue function
此函数等效于核心 Python 中的 True。它返回一个单例,可以通过 S.true 检索。
This function is equivalent of True as in core Python. It returns a singleton that can be retrieved by S.true.
>>> from sympy import *
>>> x=sympify(true)
>>> x, S.true
上面的代码段给出了以下输出:
The above code snippet gives the following output −
BooleanFalse function
类似地,此函数等效于 Python 中的布尔 False,可以通过 S.false 访问。
Similarly, this function is equivalent to Boolean False in Python and can be accessed by S.false
>>> from sympy import *
>>> x=sympify(false)
>>> x, S.false
上面的代码段给出了以下输出:
The above code snippet gives the following output −
And function
逻辑 AND 函数对其两个参数求值,如果其中任何一个为 False,则返回 False。该函数模拟 & 运算符。
A logical AND function evaluates its two arguments and returns False if either of them is False. The function emulates & operator.
>>> from sympy import *
>>> from sympy.logic.boolalg import And
>>> x,y=symbols('x y')
>>> x=True
>>> y=True
>>> And(x,y), x"&"y
上面的代码段给出了以下输出:
The above code snippet gives the following output −
Or function
此函数对两个布尔参数求值,如果其中任何一个为 True,则返回 True。| 运算符方便地模拟其行为。
This function evaluates two Boolean arguments and returns True if either of them is True. The | operator conveniently emulates its behaviour.
>>> from sympy import *
>>> from sympy.logic.boolalg import Or
>>> x,y=symbols('x y')
>>> x=True
>>> y=False
>>> Or(x,y), x|y
上面的代码段给出了以下输出:
The above code snippet gives the following output −
Not Function
逻辑 Not 函数对布尔参数求反。如果其参数为 False,则返回 True;如果为 True,则返回 False。~ 运算符执行与 Not 函数类似的操作。如下例所示:
A Logical Not function results in negation of the Boolean argument. It returns True if its argument is False and returns False if True. The ~ operator performs the operation similar to Not function. It is shown in the example below −
>>> from sympy import *
>>> from sympy.logic.boolalg import Or, And, Not
>>> x,y=symbols('x y')
>>> x=True
>>> y=False
>>> Not(x), Not(y)
上面的代码段给出了以下输出:
The above code snippet gives the following output −
Xor Function
逻辑 XOR(异或)函数在奇数个参数为 True、其余参数为 False 时返回 True,在偶数个参数为 True、其余参数为 False 时返回 False。^ 运算符执行类似的操作。
The Logical XOR (exclusive OR) function returns True if an odd number of the arguments are True and the rest are False and returns False if an even number of the arguments are True and the rest are False. Similar operation is performed by ^ operator.
>>> from sympy import *
>>> from sympy.logic.boolalg import Xor
>>> x,y=symbols('x y')
>>> x=True
>>> y=False
>>> Xor(x,y), x^y
上面的代码段给出了以下输出:
The above code snippet gives the following output −
(True, True)
>>> a,b,c,d,e=symbols('a b c d e')
>>> a,b,c,d,e=(True, False, True, True, False)
>>> Xor(a,b,c,d,e)
上面的代码段给出了以下输出:
The above code snippet gives the following output −
True
在上述情况下,三个(奇数)参数为 True,因此 Xor 返回 true。但是,如果 True 参数的数量为偶数,则结果为 False,如下所示:
In above case, three (odd number) arguments are True, hence Xor returns true. However, if number of True arguments is even, it results in False, as shown below −
>>> a,b,c,d,e=(True, False, False, True, False)
>>> Xor(a,b,c,d,e)
上面的代码段给出了以下输出:
The above code snippet gives the following output −
Nand Function
此函数执行逻辑 NAND 操作。它对其参数求值,如果其中任何一个为 False,则返回 True,如果所有参数都为 True,则返回 False。
This function performs Logical NAND operation. It evaluates its arguments and returns True if any of them are False, and False if they are all True.
>>> from sympy import *
>>> from sympy.logic.boolalg import Nand
>>> a,b,c=symbols('a b c')
>>> a,b,c=(True, False, True)
>>> Nand(a,b,c), Nand(a,c)
上面的代码段给出了以下输出:
The above code snippet gives the following output −
Nor Function
此函数执行逻辑 NOR 操作。它对其参数求值,如果其中任何一个为 True,则返回 False,如果所有参数都为 False,则返回 True。
This function performs Logical NOR operation. It evaluates its arguments and returns False if any of them are True, and True if they are all False.
>>> from sympy import *
>>> from sympy.logic.boolalg import Nor
>>> a,b,c=symbols('a b c')
>>> a,b,c=(True, False, True)
>>> Nor(a,b,c), Nor(a,c)
上面的代码段给出了以下输出:
The above code snippet gives the following output −
(False, False)
需要注意的是,即使 SymPy 提供了 ^ 运算符用于 Xor,~ 用于 Not,| 用于 Or,& 用于 And 作为一种便利,但它们在 Python 中的常规用法是作为按位运算符。因此,如果操作数是整数,则结果不同。
Note that even though SymPy provides ^ operator for Xor, ~ for Not, | for Or and & for And functions as convenience, their normal use in Python is as bitwise operators. Hence, if operands are integers, results would be different.
Equivalent function
此函数返回等价关系。当且仅当 A 和 B 都是 True 或都是 False 时,Equivalent(A, B) 为 True。如果所有参数在逻辑上等效,则该函数返回 True。否则返回 False。
This function returns equivalence relation. Equivalent(A, B) is True if and only if A and B are both True or both False. The function returns True if all of the arguments are logically equivalent. Returns False otherwise.
>>> from sympy import *
>>> from sympy.logic.boolalg import Equivalent
>>> a,b,c=symbols('a b c')
>>> a,b,c=(True, False, True)
>>> Equivalent(a,b), Equivalent(a,c)
上面的代码段给出了以下输出:
The above code snippet gives the following output −
ITE function
此函数充当编程语言中的 If then else 子句。ITE(A, B, C) 求值并返回 B 的结果(如果 A 为真),否则返回 C 的结果。所有参数必须为布尔值。
This function acts as If then else clause in a programming language.ITE(A, B, C) evaluates and returns the result of B if A is true else it returns the result of C. All args must be Booleans.
>>> from sympy import * >>> from sympy.logic.boolalg import ITE >>> a,b,c=symbols('a b c') >>> a,b,c=(True, False, True)
>>> ITE(a,b,c), ITE(a,c,b)
上面的代码段给出了以下输出:
The above code snippet gives the following output −