def all_monomials_of_degree_d(d, 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 xrange(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()
def all_monomials_of_degree_d(d, 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 xrange(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()
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
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
def from_fast_pickable(l, r): """from_fast_pickable(l, ring) undoes the operation to_fast_pickable. The first argument is an object created by to_fast_pickable. For the specified format, see the documentation of to_fast_pickable. The second argument is ring, in which this polynomial should be created. INPUT: see OUTPUT of to_fast_pickable OUTPUT: a list of Boolean polynomials EXAMPLES: >>> from polybori.PyPolyBoRi import Ring >>> r=Ring(1000) >>> x = r.variable >>> from_fast_pickable([[1], []], r) [1] >>> from_fast_pickable([[0], []], r) [0] >>> from_fast_pickable([[2], [(0, 1, 0)]], r) [x(0)] >>> from_fast_pickable([[2], [(1, 1, 0)]], r) [x(1)] >>> from_fast_pickable([[2], [(0, 1, 1)]], r) [x(0) + 1] >>> from_fast_pickable([[2], [(0, 3, 0), (1, 1, 0)]], r) [x(0)*x(1)] >>> from_fast_pickable([[2], [(0, 3, 3), (1, 1, 0)]], r) [x(0)*x(1) + x(1)] >>> from_fast_pickable([[2], [(0, 3, 4), (1, 1, 0), (2, 1, 0)]], r) [x(0)*x(1) + x(2)] >>> 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(xrange(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]
def from_fast_pickable(l,r): """from_fast_pickable(l, ring) undoes the operation to_fast_pickable. The first argument is an object created by to_fast_pickable. For the specified format, see the documentation of to_fast_pickable. The second argument is ring, in which this polynomial should be created. INPUT: see OUTPUT of to_fast_pickable OUTPUT: a list of Boolean polynomials EXAMPLES: >>> from polybori.PyPolyBoRi import Ring >>> r=Ring(1000) >>> x = r.variable >>> from_fast_pickable([[1], []], r) [1] >>> from_fast_pickable([[0], []], r) [0] >>> from_fast_pickable([[2], [(0, 1, 0)]], r) [x(0)] >>> from_fast_pickable([[2], [(1, 1, 0)]], r) [x(1)] >>> from_fast_pickable([[2], [(0, 1, 1)]], r) [x(0) + 1] >>> from_fast_pickable([[2], [(0, 3, 0), (1, 1, 0)]], r) [x(0)*x(1)] >>> from_fast_pickable([[2], [(0, 3, 3), (1, 1, 0)]], r) [x(0)*x(1) + x(1)] >>> from_fast_pickable([[2], [(0, 3, 4), (1, 1, 0), (2, 1, 0)]], r) [x(0)*x(1) + x(2)] >>> 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(xrange(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]