def truthtable2expr(tt, conj=False): """Convert a truth table into an expression.""" if conj: outer, inner = (And, Or) nums = tt.pcdata.iter_zeros() else: outer, inner = (Or, And) nums = tt.pcdata.iter_ones() inputs = [exprvar(v.names, v.indices) for v in tt.inputs] terms = [boolfunc.num2term(num, inputs, conj) for num in nums] return outer(*[inner(*term) for term in terms])
def decode(self): r"""Return a :math:`N \rightarrow 2^N` decoder. Example Truth Table for a 2:4 decoder: .. csv-table:: :header: :math:`A_1`, :math:`A_0`, :math:`D_3`, :math:`D_2`, :math:`D_1`, :math:`D_0` :stub-columns: 2 0, 0, 0, 0, 0, 1 0, 1, 0, 0, 1, 0 1, 0, 0, 1, 0, 0 1, 1, 1, 0, 0, 0 """ items = [reduce(operator.and_, boolfunc.num2term(i, self._items), self.ftype.box(1)) for i in range(2 ** self.size)] return self.__class__(items, ftype=self.ftype)
def test_num2term(): assert num2term(0x0, [a, b, c, d], conj=False) == (~a, ~b, ~c, ~d) assert num2term(0x1, [a, b, c, d], conj=False) == ( a, ~b, ~c, ~d) assert num2term(0x2, [a, b, c, d], conj=False) == (~a, b, ~c, ~d) assert num2term(0x3, [a, b, c, d], conj=False) == ( a, b, ~c, ~d) assert num2term(0x4, [a, b, c, d], conj=False) == (~a, ~b, c, ~d) assert num2term(0x5, [a, b, c, d], conj=False) == ( a, ~b, c, ~d) assert num2term(0x6, [a, b, c, d], conj=False) == (~a, b, c, ~d) assert num2term(0x7, [a, b, c, d], conj=False) == ( a, b, c, ~d) assert num2term(0x8, [a, b, c, d], conj=False) == (~a, ~b, ~c, d) assert num2term(0x9, [a, b, c, d], conj=False) == ( a, ~b, ~c, d) assert num2term(0xA, [a, b, c, d], conj=False) == (~a, b, ~c, d) assert num2term(0xB, [a, b, c, d], conj=False) == ( a, b, ~c, d) assert num2term(0xC, [a, b, c, d], conj=False) == (~a, ~b, c, d) assert num2term(0xD, [a, b, c, d], conj=False) == ( a, ~b, c, d) assert num2term(0xE, [a, b, c, d], conj=False) == (~a, b, c, d) assert num2term(0xF, [a, b, c, d], conj=False) == ( a, b, c, d) assert num2term(0x0, [a, b, c, d], conj=True) == ( a, b, c, d) assert num2term(0x1, [a, b, c, d], conj=True) == (~a, b, c, d) assert num2term(0x2, [a, b, c, d], conj=True) == ( a, ~b, c, d) assert num2term(0x3, [a, b, c, d], conj=True) == (~a, ~b, c, d) assert num2term(0x4, [a, b, c, d], conj=True) == ( a, b, ~c, d) assert num2term(0x5, [a, b, c, d], conj=True) == (~a, b, ~c, d) assert num2term(0x6, [a, b, c, d], conj=True) == ( a, ~b, ~c, d) assert num2term(0x7, [a, b, c, d], conj=True) == (~a, ~b, ~c, d) assert num2term(0x8, [a, b, c, d], conj=True) == ( a, b, c, ~d) assert num2term(0x9, [a, b, c, d], conj=True) == (~a, b, c, ~d) assert num2term(0xA, [a, b, c, d], conj=True) == ( a, ~b, c, ~d) assert num2term(0xB, [a, b, c, d], conj=True) == (~a, ~b, c, ~d) assert num2term(0xC, [a, b, c, d], conj=True) == ( a, b, ~c, ~d) assert num2term(0xD, [a, b, c, d], conj=True) == (~a, b, ~c, ~d) assert num2term(0xE, [a, b, c, d], conj=True) == ( a, ~b, ~c, ~d) assert num2term(0xF, [a, b, c, d], conj=True) == (~a, ~b, ~c, ~d)
def test_num2term(): assert_raises(TypeError, num2term, 1.0, [a, b]) assert_raises(ValueError, num2term, -1, [a, b]) assert_raises(ValueError, num2term, 4, [a, b]) assert num2term(0x0, [a, b, c, d], conj=False) == (~a, ~b, ~c, ~d) assert num2term(0x1, [a, b, c, d], conj=False) == ( a, ~b, ~c, ~d) assert num2term(0x2, [a, b, c, d], conj=False) == (~a, b, ~c, ~d) assert num2term(0x3, [a, b, c, d], conj=False) == ( a, b, ~c, ~d) assert num2term(0x4, [a, b, c, d], conj=False) == (~a, ~b, c, ~d) assert num2term(0x5, [a, b, c, d], conj=False) == ( a, ~b, c, ~d) assert num2term(0x6, [a, b, c, d], conj=False) == (~a, b, c, ~d) assert num2term(0x7, [a, b, c, d], conj=False) == ( a, b, c, ~d) assert num2term(0x8, [a, b, c, d], conj=False) == (~a, ~b, ~c, d) assert num2term(0x9, [a, b, c, d], conj=False) == ( a, ~b, ~c, d) assert num2term(0xA, [a, b, c, d], conj=False) == (~a, b, ~c, d) assert num2term(0xB, [a, b, c, d], conj=False) == ( a, b, ~c, d) assert num2term(0xC, [a, b, c, d], conj=False) == (~a, ~b, c, d) assert num2term(0xD, [a, b, c, d], conj=False) == ( a, ~b, c, d) assert num2term(0xE, [a, b, c, d], conj=False) == (~a, b, c, d) assert num2term(0xF, [a, b, c, d], conj=False) == ( a, b, c, d) assert num2term(0x0, [a, b, c, d], conj=True) == ( a, b, c, d) assert num2term(0x1, [a, b, c, d], conj=True) == (~a, b, c, d) assert num2term(0x2, [a, b, c, d], conj=True) == ( a, ~b, c, d) assert num2term(0x3, [a, b, c, d], conj=True) == (~a, ~b, c, d) assert num2term(0x4, [a, b, c, d], conj=True) == ( a, b, ~c, d) assert num2term(0x5, [a, b, c, d], conj=True) == (~a, b, ~c, d) assert num2term(0x6, [a, b, c, d], conj=True) == ( a, ~b, ~c, d) assert num2term(0x7, [a, b, c, d], conj=True) == (~a, ~b, ~c, d) assert num2term(0x8, [a, b, c, d], conj=True) == ( a, b, c, ~d) assert num2term(0x9, [a, b, c, d], conj=True) == (~a, b, c, ~d) assert num2term(0xA, [a, b, c, d], conj=True) == ( a, ~b, c, ~d) assert num2term(0xB, [a, b, c, d], conj=True) == (~a, ~b, c, ~d) assert num2term(0xC, [a, b, c, d], conj=True) == ( a, b, ~c, ~d) assert num2term(0xD, [a, b, c, d], conj=True) == (~a, b, ~c, ~d) assert num2term(0xE, [a, b, c, d], conj=True) == ( a, ~b, ~c, ~d) assert num2term(0xF, [a, b, c, d], conj=True) == (~a, ~b, ~c, ~d)