gpt4 book ai didi

python - 将公式转换为cnf python

转载 作者:行者123 更新时间:2023-12-04 17:13:01 27 4
gpt4 key购买 nike

我想将公式转换为 CNF。有图书馆可以做到这一点吗?这是我的代码。我创建了许多函数来将任何(a 或 b)转换为 CNF 格式。

但是如果有很多命题,那就很难像 (a > b) & (c & d) or not(f) ..

operator="&|>=~"

def isOperand(c):
return c >= 'a' and c <= 'z'

operators ="&|>=~"
def isOperator(c): #it cheak if the the given c is on the operators
return c in operators
def dblimplique(a):
if(a=='a = b' ):
a='(a > b) & (b > a)'
return a
def limplique(a):
if(a=='a > b' ):
a='~a | b'
return a
def nonAouB(a):
if(a=='~(a | b)' ):
a='~a & ~b'
return a
def nonAetB(a):
if (a == '~(a & b)'):
a = '~a | ~b'
return a
def doublenon(a):
if (a == '~~a'):
a = 'a'
return a
def doublenon(a):
if (a == '~~a'):
a = 'a'
return a
def AetBouc(a):
if (a == 'a & (b | c)'):
a = '(a & b) | (a & c)'
return a


def AouBetc(a):
if (a == 'a | (b & c)'):
a = '(a | b) & (a | c)'
return a


def FNC():
input_string = input("Entrer votre formule:")
if(input_string == 'a = b'):
return dblimplique(input_string)

elif(input_string=='a > b'):
return limplique(input_string)

elif(input_string=='~(a | b)'):
return nonAouB(input_string)

elif(input_string == '~(a & b)'):
return nonAetB(input_string)

elif(input_string =='~~a'):
return doublenon(input_string)
elif(input_string =='a & (b | c)'):
return AetBouc(input_string)
elif(input_string=='a | (b & c)'):
return AouBetc(input_string)
elif(input_string=='a | b' or 'a | b | c' or 'a | b | c | d' or 'a | b | c | d'):
return input_string
else:
for i in input_string:
if(i == 'a > b' or 'c > d' or 'e > f'):
return limplique(i)


print(FNC())

最佳答案

使用此模块 pip install sympy

from sympy.logic.boolalg import to_cnf
from sympy.abc import A, B, D
to_cnf(~(A | B) | D)

这是一个很好的图书馆: https://docs.sympy.org/latest/modules/logic.html

这是两个代码:
https://github.com/ldkrsi/cnf_py

https://github.com/omkarkarande/CNF-Converter

关于python - 将公式转换为cnf python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53795696/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com