首页 > 解决方案 > 带有 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 看起来可以互换的原因

标签: dictionarybooleanpython-3.5sympy

解决方案


推荐阅读