Beispiel #1
0
def all_monomials_of_degree_d(d, variables):
    """
    Return monomials of degree d in the given variables.
    """
    variables = Monomial(variables)
    variables = list(variables.variables())
    if not variables:
        assert d == 0
        return BooleConstant(1)
    ring = variables[0].ring()
    if d > len(variables):
        return Polynomial(0, ring)
    if d < 0:
        return Polynomial(1, ring)

    deg_variables = variables[-d:]
    # this ensures sorting by indices
    res = Monomial(deg_variables)

    for i in range(1, len(variables) - d + 1):
        deg_variables = variables[-d - i:-i]
        res = Polynomial(res)
        nav = res.navigation()
        navs = []
        while not nav.constant():
            navs.append(BooleSet(nav, ring))
            nav = nav.then_branch()
        acc = Polynomial(1, ring)
        for (nav, v) in reversed(zip(navs, deg_variables)):
            acc = if_then_else(v, acc, nav)
        res = acc
    return res.set()
Beispiel #2
0
def combine(reductors, p, reduce=None):
    p_nav = p.navigation()
    assert p_nav.value() < reductors.navigation().value()
    p_else = BooleSet(p_nav.else_branch(), p.ring())
    if reduce:
        p_else = reduce(p_else, reductors)
    return if_then_else(p_nav.value(), reductors, p_else)
Beispiel #3
0
def power_set(variables):
    if not variables:
        return BooleConstant(1)
    variables = sorted(set(variables), reverse=True, key=top_index)
    res = Polynomial(1, variables[0].ring()).set()
    for v in variables:
        res = if_then_else(v, res, res)
    return res
Beispiel #4
0
def from_fast_pickable(l, r):
    r"""
    Undo the operation :func:`to_fast_pickable`.

    The first argument is an object created by :func:`to_fast_pickable`.

    For the specified format, see the documentation of :func:`to_fast_pickable`.
    The second argument is ring, in which this polynomial should be created.

    INPUT:

    See OUTPUT of :func:`to_fast_pickable`

    OUTPUT:

    a list of Boolean polynomials

    EXAMPLES::

        sage: from sage.rings.polynomial.pbori import Ring
        sage: from sage.rings.polynomial.pbori.parallel import from_fast_pickable
        sage: r = Ring(1000)
        sage: x = r.variable
        sage: from_fast_pickable([[1], []], r)
        [1]
        sage: from_fast_pickable([[0], []], r)
        [0]
        sage: from_fast_pickable([[2], [(0, 1, 0)]], r)
        [x(0)]
        sage: from_fast_pickable([[2], [(1, 1, 0)]], r)
        [x(1)]
        sage: from_fast_pickable([[2], [(0, 1, 1)]], r)
        [x(0) + 1]
        sage: from_fast_pickable([[2], [(0, 3, 0), (1, 1, 0)]], r)
        [x(0)*x(1)]
        sage: from_fast_pickable([[2], [(0, 3, 3), (1, 1, 0)]], r)
        [x(0)*x(1) + x(1)]
        sage: from_fast_pickable([[2], [(0, 3, 4), (1, 1, 0), (2, 1, 0)]], r)
        [x(0)*x(1) + x(2)]
        sage: from_fast_pickable([[2, 0, 1, 4], [(0, 3, 0), (1, 1, 0), (3, 1, 0)]], r)
        [x(0)*x(1), 0, 1, x(3)]
    """
    i2poly = {0: r.zero(), 1: r.one()}
    (indices, terms) = l

    for i in reversed(range(len(terms))):
        (v, t, e) = terms[i]
        t = i2poly[t]
        e = i2poly[e]
        terms[i] = if_then_else(v, t, e)
        i2poly[i + 2] = terms[i]
    return [Polynomial(i2poly[i]) for i in indices]