dictionary - 带有 xor/xnor 的 Python Sympy bool_map 错误?
问题描述
Python3.5 Sympy 似乎认为 Xor 和 Xnor 是等价的?
python3.5中的这段代码:
from sympy import *
A1,A2 = symbols('A1,A2')
f1 = Xor(A1,A2)
f2 = ~(Xor(A1,A2))
print(bool_map(f2,f1))
print(bool_map(f1,f2))
输出结果:
((A1 & A2) | (~A1 & ~A2), {A1: A1, A2: A2})
((A1 & ~A2) | (A2 & ~A1), {A1: A1, A2: A2})
所以 f1 和 f2 的简化逻辑明显不同,但 bool_map 仍然返回,它被认为是 2 个布尔方程的有效符号映射?
我究竟做错了什么?
编辑:这确实是一个错误
这是 _finger 5 项指纹中的一个缺陷:
'''
Assign a 5-item fingerprint to each symbol in the equation:
[
# of times it appeared as a Symbol,
# of times it appeared as a Not(symbol),
# of times it appeared as a Symbol in an And or Or,
# of times it appeared as a Not(Symbol) in an And or Or,
sum of the number of arguments with which it appeared,
counting Symbol as 1 and Not(Symbol) as 2
]
'''
from sympy import *
from sympy.logic.boolalg import _finger
from collections import defaultdict
from pprint import pprint
A1,A2 = symbols('A1,A2')
a = _finger((A1 & A2) | (~A1 & ~A2))
b = _finger((A1 & ~A2) | (~A1 & A2))
pprint(a)
pprint(b)
导致 A1 和 A2 的指纹相同:
defaultdict(<class 'list'>, {(0, 0, 1, 1, 6): [A1, A2]})
defaultdict(<class 'list'>, {(0, 0, 1, 1, 6): [A1, A2]})
这就是为什么 A1 和 A2 看起来可以互换的原因
解决方案
推荐阅读
- arduino - 将 HC-05 蓝牙模块连接到 PC 的问题
- python - 将 Python 多处理转换为 PySpark
- python - 无论如何要进行多线程pdf挖掘?
- visual-studio - IIS 未列出与启动 URL 匹配的网站 - 本地主机不是“本地主机”
- javascript - 在 Rails 中为 2FA 渲染 js 部分(链接)时出错
- python - 向 MongoDB 插入 2.71 亿条记录
- python-3.x - 如何更改默认程序以在 Python 中打开文件?
- c++ - 在 c++ 中显示总和,然后添加更多 sum cpp
- docker - 如何连接到 docker 容器内的 localhost?
- javascript - 隐藏后如何取消隐藏一行?