简体   繁体   English

SOP 中的布尔表达式

[英]Boolean expression in SOP

I'm new to boolean expressions.我是布尔表达式的新手。

I've been given the task to simplify F(w,x,y,z) = xy' + x'z' + wxz + wx'y by using K map.我的任务是使用 K 映射简化F(w,x,y,z) = xy' + x'z' + wxz + wx'y

I've done it and the result is wx'+w'y'+xyz .我已经完成了,结果是wx'+w'y'+xyz

Now I have to "Write it in a standard SOP form. You need to provide the steps through which you get the standard SOP".现在我必须“以标准 SOP 形式编写它。您需要提供获得标准 SOP 的步骤”。

And i have no idea how to do it.我不知道该怎么做。 I thought result after k map is sop.我认为 k 地图后的结果是 sop。

Yes, you already have it in SOP form. 是的,您已经以SOP形式获得它。 But the second question is about Standard (aka canonical) SOP form. 但是第二个问题是关于标准(又称规范)SOP形式的。 That's much simpler to find than having to use K-maps (but it's often long), it's just the sum of minterms. 这比必须使用K-maps要容易得多(但通常很长),这只是最小项的总和。

I think your solution does not cover all ones . 我认为您的解决方案不能解决所有问题 These Karnaugh maps show the original expression, the simplified version (minimal SOP) and the canonical SOP, where every product contains all literals (all given variables or their negation). 这些卡诺图显示原始表达式,简化版本(最小SOP)和规范SOP,其中每个乘积都包含所有文字(所有给定变量或它们的取反)。

原始,简化和完整SOP的K图

The original expression is 原始表达是

F(w,x,y,z) = x·¬y + ¬x·¬z + w·x·z + w·¬x·y

– there are two fours and two pairs circled in the corresponding (first one) K-map. –在相应的(第一个)K映射中圈出两个四和两对。

The original expression simplified using K-map (shown in the second one): 原始表达式使用K-map简化(如第二个所示):

F(w,x,y,z) = x·¬y + ¬x·¬z + w·y·z

is different than yours, but you can check for example with wolframalpha online tool, that it is the simplified original expression. 与您的工具有所不同,但是您可以使用Wolframalpha在线工具检查其是否为简化的原始表示形式。

It is also the minimal DNF, but not a sum of minterms (where the output is equal to 1), because there are not all variables in every product of the sum. 它也是最小DNF,但不是最小项的总和(输出等于1),因为总和的每个乘积中都没有变量。

The third K-map shows ten minterms circled. 第三张K-map显示了十个最小项。 They form the canonical DNF: 它们形成规范的DNF:

F(w,x,y,z) = m0 + m2 + m4 + m5 + m8 + m10 + m11 + m12 + m13 + m15 =
           = ¬w·¬x·¬y·¬z + ¬w·¬x·y·¬z + ¬w·x·¬y·¬z + ¬w·x·¬y·z + w·¬x·¬y·¬z 
             + w·¬x·y·¬z + w·¬x·y·z + w·x·¬y·¬z + w·x·¬y·z + w·x·y·z

I checked your simplified expression, but there are not all ones covered (even if there were some useful do not care states (marked X)). 我检查你的简化表达,但也有不是所有的人覆盖(即使有一些有用的无关状态(标记X))。 Maybe you made a typo. 也许你打错字了。 Or could there be a typo in the original expression? 或原始表达形式中可能有错字?

We can implement the K-Map algorithm in python for 4 variables, as shown below.我们可以在python实现4个变量的K-Map算法,如下图。 The function accepts the Boolean function in SOP (sum of products) form and the names of the variables and returns a simplified reduced representation.该函数接受 SOP(乘积总和)形式的布尔函数和变量名称,并返回简化的简化表示。 Basically you need to create rectangular groups containing total terms in power of two like 8, 4, 2 and try to cover as many elements as you can in one group.基本上,您需要创建包含 8、4、2 等 2 次幂的总项的矩形组,并尝试在一组中包含尽可能多的元素。

For example, the function can be represented F(w,x,y,z) = xy' + x'z' + wxz + wx'y in SOP form as f(w,x,y,z)=∑(0,2,4,5,8,10,11,12,13,15), as can be seen from the below table:例如,该函数可以表示为 F(w,x,y,z) = xy' + x'z' + wxz + wx'y 以 SOP 形式表示为 f(w,x,y,z)=∑(0 ,2,4,5,8,10,11,12,13,15),如下表所示:

在此处输入图片说明

As can be seen from the output of the next code snippet, the program outputs the simplified form x¬y + ¬x¬z + wyz , where negation of a boolean variable x is represented as ¬x in the code.从下一段代码的输出可以看出,程序输出了简化形式x¬y + ¬x¬z + wyz ,其中布尔变量x否定在代码中表示为¬x

from collections import defaultdict
from itertools import permutations, product
    
def kv_map(sop, vars):
    
    sop = set(sop)
    not_covered = sop.copy()
    sop_covered = set([])
    
    mts = [] # minterms
    
    # check for minterms with 1 variable
    all_3 = [''.join(x) for x in product('01', repeat=3)]
    for i in range(4):
        for v_i in [0,1]:
                if len(not_covered) == 0: continue
                mt = ('' if v_i else '¬') + vars[i]
                s = [x[:i]+str(v_i)+x[i:] for x in all_3]
                sop1 = set(map(lambda x: int(x,2), s))
                if len(sop1 & sop) == 8 and len(sop_covered & sop1) < 8: # if not already covered
                    mts.append(mt)
                    sop_covered |= sop1
                    not_covered = not_covered - sop1
        if len(not_covered) == 0:
           return mts
    
    # check for minterms with 2 variables
    all_2 = [''.join(x) for x in product('01', repeat=2)]
    for i in range(4):
        for j in range(i+1, 4):
            for v_i in [0,1]:
                for v_j in [0,1]:
                    if len(not_covered) == 0: continue
                    mt = ('' if v_i else '¬') + vars[i] + ('' if v_j else '¬') + vars[j]
                    s = [x[:i]+str(v_i)+x[i:] for x in all_2]
                    s = [x[:j]+str(v_j)+x[j:] for x in s]
                    sop1 = set(map(lambda x: int(x,2), s))
                    if len(sop1 & sop) == 4 and len(sop_covered & sop1) < 4: # if not already covered
                        mts.append(mt)
                        sop_covered |= sop1
                        not_covered = not_covered - sop1
    if len(not_covered) == 0:
        return mts

    # check for minterms with 3 variables similarly (code omitted)
    # ... ... ...
    
    return mts
    
mts = kv_map([0,2,4,5,8,10,11,12,13,15], ['w', 'x', 'y', 'z'])
mts
# ['x¬y', '¬x¬z', 'wyz']

The following animation shows how the above code (greedily) simplifies the Boolean function given in SOP form (the basic goal is to cover all the 1s with minimum number of power-2 blocks).下面的动画展示了上面的代码如何(贪婪地)简化以 SOP 形式给出的布尔函数(基本目标是用最少的 2 次幂块覆盖所有 1)。 Since the algorithm is greedy it may get stuck to some local minimum, that we need to be careful about.由于算法是贪婪的,它可能会卡在某个局部最小值,我们需要小心。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM