Exemplo n.º 1
0
def test_iter_terms():
    assert list(set(term)
                for term in iter_terms([a, b], conj=False)) == [{~a, ~b},
                                                                {a,
                                                                 ~b}, {~a, b},
                                                                {a, b}]
    assert list(set(term)
                for term in iter_terms([a, b], conj=True)) == [{a, b}, {~a, b},
                                                               {a, ~b},
                                                               {~a, ~b}]
Exemplo n.º 2
0
def Mux(fs, sel, simplify=True):
    """
    Return an expression that multiplexes a sequence of input functions over a
    sequence of select functions.
    """
    # convert Mux([a, b], x) to Mux([a, b], [x])
    if isinstance(sel, Expression):
        sel = [sel]

    if len(sel) < clog2(len(fs)):
        fstr = "expected at least {} select bits, got {}"
        raise ValueError(fstr.format(clog2(len(fs)), len(sel)))

    it = boolfunc.iter_terms(sel)
    y = exprnode.or_(*[exprnode.and_(f.node, *[lit.node for lit in next(it)])
                       for f in fs])
    if simplify:
        y = y.simplify()
    return _expr(y)
Exemplo n.º 3
0
def Mux(fs, sel, simplify=True):
    """
    Return an expression that multiplexes a sequence of input functions over a
    sequence of select functions.
    """
    # convert Mux([a, b], x) to Mux([a, b], [x])
    if isinstance(sel, Expression):
        sel = [sel]

    if len(sel) < clog2(len(fs)):
        fstr = "expected at least {} select bits, got {}"
        raise ValueError(fstr.format(clog2(len(fs)), len(sel)))

    it = boolfunc.iter_terms(sel)
    y = exprnode.or_(
        *[exprnode.and_(f.node, *[lit.node for lit in next(it)]) for f in fs])
    if simplify:
        y = y.simplify()
    return _expr(y)
Exemplo n.º 4
0
def _filtdim(items, shape, dim, nsl):
    """Return items, shape filtered by a dimension slice."""
    normshape = tuple(stop - start for start, stop in shape)
    nsl_type = type(nsl)
    newitems = list()
    # Number of groups
    num = reduce(operator.mul, normshape[:dim + 1])
    # Size of each group
    size = len(items) // num
    # Size of the dimension
    n = normshape[dim]
    if nsl_type is int:
        for i in range(num):
            if i % n == nsl:
                newitems += items[size * i:size * (i + 1)]
        # Collapse dimension
        newshape = shape[:dim] + shape[dim + 1:]
    elif nsl_type is slice:
        for i in range(num):
            if nsl.start <= (i % n) < nsl.stop:
                newitems += items[size * i:size * (i + 1)]
        # Reshape dimension
        offset = shape[dim][0]
        redim = (offset + nsl.start, offset + nsl.stop)
        newshape = shape[:dim] + (redim, ) + shape[dim + 1:]
    # farray
    else:
        if nsl.size < clog2(n):
            fstr = "expected dim {} select to have >= {} bits, got {}"
            raise ValueError(fstr.format(dim, clog2(n), nsl.size))
        groups = [list() for _ in range(n)]
        for i in range(num):
            groups[i % n] += items[size * i:size * (i + 1)]
        for muxins in zip(*groups):
            it = boolfunc.iter_terms(nsl._items)
            xs = [
                reduce(operator.and_, (muxin, ) + next(it)) for muxin in muxins
            ]
            newitems.append(reduce(operator.or_, xs))
        # Collapse dimension
        newshape = shape[:dim] + shape[dim + 1:]
    return newitems, newshape
Exemplo n.º 5
0
def _filtdim(items, shape, dim, nsl):
    """Return items, shape filtered by a dimension slice."""
    normshape = tuple(stop - start for start, stop in shape)
    nsl_type = type(nsl)
    newitems = list()
    # Number of groups
    num = reduce(operator.mul, normshape[:dim+1])
    # Size of each group
    size = len(items) // num
    # Size of the dimension
    N = normshape[dim]
    if nsl_type is int:
        for i in range(num):
            if i % N == nsl:
                newitems += items[size*i:size*(i+1)]
        # Collapse dimension
        newshape = shape[:dim] + shape[dim+1:]
    elif nsl_type is slice:
        for i in range(num):
            if nsl.start <= (i % N) < nsl.stop:
                newitems += items[size*i:size*(i+1)]
        # Reshape dimension
        offset = shape[dim][0]
        redim = (offset + nsl.start, offset + nsl.stop)
        newshape = shape[:dim] + (redim, ) + shape[dim+1:]
    # farray
    else:
        if nsl.size < clog2(N):
            fstr = "expected dim {} select to have >= {} bits, got {}"
            raise ValueError(fstr.format(dim, clog2(N), nsl.size))
        groups = [list() for _ in range(N)]
        for i in range(num):
            groups[i % N] += items[size*i:size*(i+1)]
        for muxins in zip(*groups):
            it = boolfunc.iter_terms(nsl.items)
            args = [reduce(operator.and_, (muxin, ) + next(it))
                    for muxin in muxins]
            newitems.append(reduce(operator.or_, args))
        # Collapse dimension
        newshape = shape[:dim] + shape[dim+1:]
    return newitems, newshape
Exemplo n.º 6
0
def test_iter_terms():
    assert list(sorted(term) for term in iter_terms([a, b], conj=False)) == [[~a, ~b], [a, ~b], [~a, b], [a, b]]
    assert list(sorted(term) for term in iter_terms([a, b], conj=True)) == [[a, b], [~a, b], [a, ~b], [~a, ~b]]
Exemplo n.º 7
0
def test_iter_terms():
    assert list(set(term) for term in iter_terms([a, b], conj=False)) == [{~a, ~b}, {a, ~b}, {~a, b}, {a, b}]
    assert list(set(term) for term in iter_terms([a, b], conj=True)) == [{a, b}, {~a, b}, {a, ~b}, {~a, ~b}]
Exemplo n.º 8
0
def test_iter_terms():
    assert list(sorted(term) for term in iter_terms([a, b], conj=False)) == [[~a, ~b], [a, ~b], [~a, b], [a, b]]
    assert list(sorted(term) for term in iter_terms([a, b], conj=True)) == [[a, b], [~a, b], [a, ~b], [~a, ~b]]