簡體   English   中英

如何在 python 中使用並集、交集和否定來簡化集合表達式

[英]How to simplify set expressions with union, intersection, and negation, in python

是否有任何內置的 python 庫可用於生成簡化的集合表達式?

例如,假設我們有一個集合表達式(A∩B)U(A∩C) 這可以簡化為A∩(BUC)

類似地(A∩B)U(A∩!B)可以簡化為A

我希望在 python 中實現這個邏輯。 我發現sympy可以用來簡化像x**2 + 2*x + 1這樣的代數表達式,但我不確定它是否能處理集合表達式。

是的。 集合表達式等價於 boolean 表達式,而sympy 可以簡化這些.

你的例子:

  • (A∩B)U(A∩C)等價於(a & b) | (a & c) (a & b) | (a & c) ,
  • (A∩B)U(A∩!B)等價於(a & b) | (a & ~b) (a & b) | (a & ~b) ,
from sympy import *
a, b, c = symbols('a, b, c')

expr = (a & b) | (a & c)
simplify_logic(expr)  # produces: a∧(b∨c)

expr = (a & b) | (a & ~b)
simplify_logic(expr)  # produces: a

可以在此處找到其他一些示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM