Пример #1
0
def num2term(num, vs, conj=False):
    """Return a tuple of all variables for a given term index.

    Parameters
    ----------
    num: int
    vs: [Variable]
    conj: bool
        conj=False for minterms, conj=True for maxterms

    Examples
    --------

    Table of min/max terms for Boolean space {a, b, c}

    +-----+----------+----------+
    | num |  minterm |  maxterm |
    +=====+==========+==========+
    | 0   | a' b' c' | a  b  c  |
    | 1   | a  b' c' | a' b  c  |
    | 2   | a' b  c' | a  b' c  |
    | 3   | a  b  c' | a' b' c  |
    | 4   | a' b' c  | a  b  c' |
    | 5   | a  b' c  | a' b  c' |
    | 6   | a' b  c  | a  b' c' |
    | 7   | a  b  c  | a' b' c' |
    +-------+----------+----------+
    """
    if conj:
        return tuple(-v if bit_on(num, i) else v for i, v in enumerate(vs))
    else:
        return tuple(v if bit_on(num, i) else -v for i, v in enumerate(vs))
Пример #2
0
    def decode(self):
        """Return symbolic logic for an N-2^N binary decoder.

        Example Truth Table for a 2:4 decoder:

            +===========+=====================+
            | A[1] A[0] | D[3] D[2] D[1] D[0] |
            +===========+=====================+
            |   0    0  |   0    0    0    1  |
            |   0    1  |   0    0    1    0  |
            |   1    0  |   0    1    0    0  |
            |   1    1  |   1    0    0    0  |
            +===========+=====================+

        >>> A = bitvec('a', 2)
        >>> d = A.decode()
        >>> d.vrestrict({A: "00"})
        [1, 0, 0, 0]
        >>> d.vrestrict({A: "10"})
        [0, 1, 0, 0]
        >>> d.vrestrict({A: "01"})
        [0, 0, 1, 0]
        >>> d.vrestrict({A: "11"})
        [0, 0, 0, 1]
        """
        items = [ And(*[ f if bit_on(i, j) else -f
                         for j, f in enumerate(self) ])
                  for i in range(2 ** len(self)) ]
        return self.__class__(items)
Пример #3
0
 def factor(self, conj=False):
     obj = self if self._simplified else self.simplify()
     if obj in B:
         return obj
     elif isinstance(obj, Exclusive):
         outer, inner = (And, Or) if conj else (Or, And)
         args = list()
         for n in range(1 << len(obj.args)):
             if parity(n) == obj.PARITY:
                 term = [arg.factor(conj) if bit_on(n, i) else Not(arg).factor(conj)
                         for i, arg in enumerate(obj.args)]
                 args.append(inner(*term))
         return outer(*args)
     else:
         return obj.factor(conj)
Пример #4
0
def num2point(num, vs):
    """Convert a number to a point in an N-dimensional space."""
    return {v: bit_on(num, i) for i, v in enumerate(vs)}