Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
some cleanup in pbori
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Nov 2, 2020
1 parent 2c87dc1 commit c53d261
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 79 deletions.
28 changes: 14 additions & 14 deletions src/sage/rings/polynomial/pbori/cnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def __init__(self, r, random_seed=16):
def zero_blocks(self, f):
r"""
Divide the zero set of f into blocks.
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: r = declare_ring(["x", "y", "z"], dict())
sage: from sage.rings.polynomial.pbori.cnf import CNFEncoder
Expand Down Expand Up @@ -79,9 +79,9 @@ def get_val(var):

def clauses(self, f):
r"""
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: r = declare_ring(["x", "y", "z"], dict())
sage: from sage.rings.polynomial.pbori.cnf import CNFEncoder
Expand All @@ -99,9 +99,9 @@ def clauses(self, f):

def polynomial_clauses(self, f):
r"""
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: r = declare_ring(["x", "y", "z"], dict())
sage: from sage.rings.polynomial.pbori.cnf import CNFEncoder
Expand Down Expand Up @@ -142,9 +142,9 @@ def get_sign(value):

def dimacs_encode_polynomial(self, p):
r"""
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: d=dict()
sage: r = declare_ring(["x", "y", "z"], d)
Expand All @@ -161,9 +161,9 @@ def dimacs_encode_polynomial(self, p):

def dimacs_cnf(self, polynomial_system):
r"""
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: r = declare_ring(["x", "y", "z"], dict())
sage: from sage.rings.polynomial.pbori.cnf import CNFEncoder
Expand Down Expand Up @@ -191,9 +191,9 @@ class CryptoMiniSatEncoder(CNFEncoder):

def dimacs_encode_polynomial(self, p):
r"""
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: d=dict()
sage: r = declare_ring(["x", "y", "z"], d)
Expand Down Expand Up @@ -229,9 +229,9 @@ def dimacs_encode_polynomial(self, p):

def dimacs_cnf(self, polynomial_system):
r"""
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: r = declare_ring(["x", "y", "z"], dict())
sage: from sage.rings.polynomial.pbori.cnf import CryptoMiniSatEncoder
Expand Down
14 changes: 7 additions & 7 deletions src/sage/rings/polynomial/pbori/fglm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def fglm(I, from_ring, to_ring):
It acts independent of the global ring, which is restored at the end of the
computation.
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: from sage.rings.polynomial.pbori.PyPolyBoRi import OrderCode
sage: dp_asc = OrderCode.dp_asc
Expand All @@ -40,9 +40,9 @@ def fglm(I, from_ring, to_ring):
def vars_real_divisors(monomial, monomial_set):
r"""
Returns all elements of of monomial_set, which result multiplied by a variable in monomial.
TESTS::
sage: from sage.rings.polynomial.pbori.pbori import *
sage: from sage.rings.polynomial.pbori.PyPolyBoRi import OrderCode
sage: dp_asc = OrderCode.dp_asc
Expand All @@ -59,12 +59,12 @@ def vars_real_divisors(monomial, monomial_set):


def m_k_plus_one(completed_elements, variables):
r"""
r"""
Calculates $m_{k+1}$ from the FGLM algorithm as described in Wichmanns diploma thesis
It would be nice to be able to efficiently extract the smallest term of a polynomial.
TESTS::
sage: from sage.rings.polynomial.pbori.pbori import *
sage: from sage.rings.polynomial.pbori.PyPolyBoRi import OrderCode
sage: dp_asc = OrderCode.dp_asc
Expand Down
2 changes: 2 additions & 0 deletions src/sage/rings/polynomial/pbori/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def block_scheme_names(blocks):

return list(context.keys())


ipbname = 'ipbori'


Expand All @@ -62,6 +63,7 @@ def declare_ring(blocks, context=None):
print(ipbname + """ -- The interactive command line tool of PolyBoRi/BRiAL %s
""" % global_context.get("polybori_version", ''))


# Here come the defaults
r = Ring(10000)
x = VariableFactory(r)
15 changes: 7 additions & 8 deletions src/sage/rings/polynomial/pbori/gbcore.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from .nf import *
from .PyPolyBoRi import *
from .nf import GeneratorLimitExceeded, symmGB_F2_C, symmGB_F2_python
from .PyPolyBoRi import (Monomial, Polynomial,
GroebnerStrategy, OrderCode, ll_red_nf_redsb)
from .ll import eliminate, ll_encode
from copy import copy
from itertools import chain
Expand Down Expand Up @@ -43,11 +43,10 @@ def filter_newstyle_options(func, **options):


def owns_one_constant(I):
"""Determines whether I contains the constant one polynomial."""
for p in I:
if p.is_one():
return True
return False
"""
Determine whether I contains the constant one polynomial.
"""
return any(p.is_one() for p in I)


def want_interpolation_gb(G):
Expand Down
5 changes: 4 additions & 1 deletion src/sage/rings/polynomial/pbori/ll.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from sage.rings.polynomial.pbori.pbori import (top_index, if_then_else,
substitute_variables)
from .PyPolyBoRi import *
from .PyPolyBoRi import (BooleSet, Polynomial, Monomial, Ring,
BoolePolynomialVector,
ll_red_nf_redsb, ll_red_nf_noredsb,
ll_red_nf_noredsb_single_recursive_call)
from .statistics import used_vars_set
from .rank import rank

Expand Down
4 changes: 3 additions & 1 deletion src/sage/rings/polynomial/pbori/nf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from sage.rings.polynomial.pbori.pbori import mod_mon_set
from .PyPolyBoRi import *
from .PyPolyBoRi import (BooleSet, Monomial, Polynomial, Variable,
GroebnerStrategy, ReductionStrategy, parallel_reduce,
easy_linear_factors, BoolePolynomialVector)
from .easy_polynomials import (easy_linear_polynomials as
easy_linear_polynomials_func)
from .statistics import used_vars_set
Expand Down
35 changes: 20 additions & 15 deletions src/sage/rings/polynomial/pbori/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def to_fast_pickable(l):
Convert a list of polynomials into a builtin Python value, which is fast pickable and compact.
INPUT:
- a list of Boolean polynomials
OUTPUT:
It is converted to a tuple consisting of
- codes referring to the polynomials
- list of conversions of nodes.
Expand Down Expand Up @@ -108,15 +108,15 @@ def from_fast_pickable(l, r):
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::
sage: from sage.rings.polynomial.pbori.frontend import *
sage: from sage.rings.polynomial.pbori.parallel import from_fast_pickable
sage: r=Ring(1000)
Expand Down Expand Up @@ -172,24 +172,28 @@ def _encode_polynomial(poly):
def pickle_polynomial(self):
return (_decode_polynomial, (_encode_polynomial(self), ))


copyreg.pickle(Polynomial, pickle_polynomial)


def pickle_bset(self):
return (BooleSet, (Polynomial(self), ))


copyreg.pickle(BooleSet, pickle_bset)


def pickle_monom(self):
return (Monomial, ([var for var in self.variables()], ))


copyreg.pickle(Monomial, pickle_monom)


def pickle_var(self):
return (Variable, (self.index(), self.ring()))


copyreg.pickle(Variable, pickle_var)


Expand Down Expand Up @@ -242,8 +246,8 @@ def _encode_ring(ring):
else:
nvars = ring.n_variables()
data = (nvars, ring.get_order_code())
varnames = '\n'.join([str(ring.variable(idx)) for idx in range(nvars)
])
varnames = '\n'.join(str(ring.variable(idx))
for idx in range(nvars))
blocks = list(ring.blocks())
code = (identifier, data, compress(varnames), blocks[:-1])
_polybori_parallel_rings[identifier] = (WeakRingRef(ring), code)
Expand All @@ -254,24 +258,25 @@ def _encode_ring(ring):
def pickle_ring(self):
return (_decode_ring, (_encode_ring(self), ))


copyreg.pickle(Ring, pickle_ring)


def groebner_basis_first_finished(I, *l):
r"""
INPUT:
- ``I`` -- ideal
- ``l`` -- keyword dictionaries, which will be keyword arguments to groebner_basis.
OUTPUT:
- tries to compute ``groebner_basis(I, **kwd)`` for kwd in l
- returns the result of the first terminated computation
EXAMPLES::
sage: from sage.rings.polynomial.pbori.PyPolyBoRi import Ring
sage: r=Ring(1000)
sage: ideal = [r.variable(1)*r.variable(2)+r.variable(2)+r.variable(1)]
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/polynomial/pbori/pbori.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ cdef class BooleanPolynomialRing(MPolynomialRing_base):
return R

def defining_ideal(self):
"""
r"""
Return `I = <x_i^2 + x_i> \subset R` where ``R =
self.cover_ring()``, and `x_i` any element in the set of
variables of this ring.
Expand Down
39 changes: 14 additions & 25 deletions src/sage/rings/polynomial/pbori/randompoly.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ def helper(samples):
return p


def sparse_random_system(ring, number_of_polynomials,
variables_per_polynomial, degree, random_seed=None):
def sparse_random_system(ring, number_of_polynomials, variables_per_polynomial,
degree, random_seed=None):
r"""
Generates a system, which is sparse in the sense, that each polynomial
contains only a small subset of variables. In each variable that occurrs
in a polynomial it is dense in the terms up to the given degree
Generate a system, which is sparse in the sense, that each polynomial
contains only a small subset of variables. In each variable that occurrs
in a polynomial it is dense in the terms up to the given degree
(every term occurs with probability 1/2).
The system will be satisfiable by at least one solution.
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: r=Ring(10)
sage: from sage.rings.polynomial.pbori import Ring, groebner_basis
sage: r = Ring(10)
sage: from sage.rings.polynomial.pbori.randompoly import sparse_random_system
sage: s=sparse_random_system(r, number_of_polynomials = 20, variables_per_polynomial = 3, degree=2, random_seed=123)
sage: s = sparse_random_system(r, number_of_polynomials=20, variables_per_polynomial=3, degree=2, random_seed=int(123))
sage: [p.deg() for p in s]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
sage: sorted(groebner_basis(s), reverse=True)
Expand Down Expand Up @@ -74,31 +75,19 @@ def sparse_random_system(ring, number_of_polynomials,
return res


def sparse_random_system_data_file_content(
number_of_variables, **kwds):
def sparse_random_system_data_file_content(number_of_variables, **kwds):
r"""
TESTS::
sage: from sage.rings.polynomial.pbori import *
sage: from sage.rings.polynomial.pbori.randompoly import sparse_random_system_data_file_content
sage: sparse_random_system_data_file_content(10, number_of_polynomials = 5, variables_per_polynomial = 3, degree=2, random_seed=123)
sage: sparse_random_system_data_file_content(10, number_of_polynomials=5, variables_per_polynomial=3, degree=2, random_seed=int(123))
"declare_ring(['x'+str(i) for in range(10)])\nideal=\\\n[...]\n\n"
"""
dummy_dict = dict()
r = declare_ring(['x' + str(i) for i in range(number_of_variables)],
dummy_dict)
dummy_dict)
polynomials = sparse_random_system(r, **kwds)
polynomials = pformat(polynomials)
res = "declare_ring(['x'+str(i) for in range(%s)])\nideal=\\\n%s\n\n" % (
number_of_variables, polynomials)
return res


def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()
9 changes: 2 additions & 7 deletions src/sage/rings/polynomial/pbori/specialsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
from .PyPolyBoRi import (BooleSet, Polynomial,
Monomial, BooleConstant, Variable)

#def all_monomials_of_degree_d(d,variables):
# res=all_monomials_of_degree_d_new(d, variables)
# ref=all_monomials_of_degree_d_old(d, variables)
# assert res==ref, (d, variables)
# return res


def all_monomials_of_degree_d_old(d, variables):

Expand Down Expand Up @@ -44,7 +38,7 @@ def all_monomials_of_degree_d(d, variables):
return Polynomial(1, ring)

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

for i in range(1, len(variables) - d + 1):
Expand All @@ -71,6 +65,7 @@ def power_set(variables):
res = if_then_else(v, res, res)
return res


if __name__ == '__main__':
from .blocks import declare_ring, Block
r = declare_ring([Block("x", 10000)], globals())
Expand Down

0 comments on commit c53d261

Please sign in to comment.