Esempio n. 1
0
def karnaugh(names, g, counter, combi, ma):

    myid = next(counter)
    myclassname = "karnaugh{}".format(myid)
    myjsonid = json.dumps("." + myclassname)

    j = 0
    l = []
    for k in sorted(ma.keys(), key=tuple_to_int):
        v = ma[k]
        if v: l.append(j)
        j += 1

    #print("calling qm: <pre>ones = {}</pre>".format(json.dumps(l)))
    yield "<!-- calling qm -->"
    starttime = time.time()
    res = [x.zfill(len(names)) for x in qm(ones=l, lennames=len(names))]
    endtime = time.time()
    yield "<!-- qm finished: {} -->".format(endtime-starttime)
    #print("result: <pre>{}</pre>".format(json.dumps(res)))

    parts = list(map(gencp(names), res))
    if len(parts) == 0: parts = ["False"]
    gencode = lambda ps: "lambda " + ", ".join(names) + ": " + ps

    funbody = " or ".join(parts)
    code = gencode(funbody)
    htmlcode = genhtmlcode(parts, myjsonid, names)
    htmlcode = "lambda " + ", ".join(names) + ": " + " or ".join(htmlcode)

    yield "</div><div class='pgblock' style='width: 60%;'>output:\n"
    yield "<pre>{}</pre>\n".format(htmlcode)
    fun = seval(code)
    pair = get_bool_table(fun, len(names))

    funtexts = [gencode(x) for x in parts]
    funs = [seval(x) for x in funtexts]
    yield "<div class='{}'>\n".format(myclassname)
    yield from do_table(names,fun,*pair,groups=list(zip(funs,colors.gethsvs(),itertools.count())))
    yield "</div>"

    # schematic
    yield make_inline_svg(ast.parse(funbody), counter, myclassname)

    yield "</div></div>"

    # check equivalence
    res = pair[1]
    for i in ma.keys():
        if not i in res.keys():
            assert False
        if res[i] != ma[i]:
            yield "key", i
            yield "expected", ma[i]
            yield "got", res[i]
            assert False

    assert res == ma
Esempio n. 2
0
def mkgroups(orgfunparts,names):
    if orgfunparts is not None:
        groups = list(zip([seval("lambda {}: {}".format(", ".join(names),i)) for i in orgfunparts],colors.gethsvs(),itertools.count())) # TODO
    else:
        groups = []
    return groups