Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/sage/rings/polynomial/pbori/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
Electronic Proceedings of the MEGA 2007 - Effective Methods in Algebraic Geometry, Strobl, Austria, June 2007.
http://www.ricam.oeaw.ac.at/mega2007/electronic/electronic.html
"""
from sage.misc.lazy_import import lazy_import
from .PyPolyBoRi import Ring, Polynomial, Monomial, Variable

# Get all-inclusive groebner routine
Expand Down
111 changes: 81 additions & 30 deletions src/sage/rings/polynomial/pbori/blocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from itertools import chain, islice

from sage.rings.polynomial.pbori.pbori import (
Expand Down Expand Up @@ -157,7 +156,7 @@ class HigherOrderBlock:
r"""
HigherOrderBlocks are multidimensional blocks of variables.

For each dimension a separate start_index and size can be specified.
For each dimension a separate ``start_index`` and ``size`` can be specified.

var_name : variables will be called <var_name>(multiindex), where
multiindex is a tuple of the size <size_tuple>
Expand Down Expand Up @@ -353,23 +352,22 @@ def if_then(i, t, supposed_to_be_valid=True):

def declare_ring(blocks, context=None):
r"""
Declare Ring is the preferred function to create a ring and declare a variable scheme,
the number of variables is automatically determined, usually you pass globals() as context
argument to store the ring and the variable mapping.
Declare Ring is the preferred function to create a ring and declare a variable scheme.

The number of variables is automatically determined. Usually you
pass ``globals()`` as context argument to store the ring and the
variable mapping.

EXAMPLES::

sage: from sage.rings.polynomial.pbori import *
sage: declare_ring([Block("x",10),Block("y",5)],globals())
Boolean PolynomialRing in x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, y0, y1, y2, y3, y4

gives a ring with x(0..9),y(0..4) and registers the ring as r, and the variable
blocks x and y in the context dictionary globals(), which consists of the global
variables of the python module
gives a ring with x(0..9),y(0..4) and registers the ring as r, and
the variable blocks x and y in the context dictionary ``globals()``,
which consists of the global variables of the python module
"""
if context is None:
context = sys.modules['__main__'].__dict__

def canonicalize(blocks):
for elt in blocks:
if isinstance(elt, str):
Expand Down Expand Up @@ -408,39 +406,92 @@ def declare_block_scheme(blocks, context):
context["number_of_declared_vars"] = start


def main():
def main_test():
"""
EXAMPLES::

sage: from sage.rings.polynomial.pbori.blocks import main_test
sage: main_test()
x(0)
x(1)
x(2)
x(3)
x(4)
x(5)
x(6)
x(7)
x(8)
x(9)
['a(0)', 'b(0)', 'c(0)', 'a(1)', 'b(1)', ...]
x(0)
x(1)
x(2)
x(3)
x(4)
x(5)
x(6)
x(7)
x(8)
x(9)
x(364) x(367) x(370) x(365) x(368) x(366)
x(99)
x(98)
x(97)
x(96)
x(95)
x(94)
x(93)
x(92)
x(91)
x(90)
x(0) x(1) x(2)
"""
r = Ring(1000)
dic = {"r": r}
dic["internalVariable"] = VariableFactory(r)

# first test
ablock = AlternatingBlock(["a", "b", "c"], 100)
declare_block_scheme([ablock], globals())
declare_block_scheme([ablock], dic)
for i in range(10):
print(r.variable(i))

print(list(ablock))

# second test
declare_block_scheme([Block(var_name="x", size=100),
HigherOrderBlock("y", (3, 4, 11, 2)),
AlternatingBlock(["a", "b", "c"], 100)],
globals())
dic)
x = dic['x']
a, b, c = dic['a'], dic['b'], dic['c']
for i in range(10):
print(x(i))
print(y(0, 0, 0, 0))
print(y(0, 0, 0, 1))
print(y(0, 0, 1, 0))
print(y(0, 0, 1, 1))
# y are currently broken ?
# print(y(0, 0, 0, 0))
# print(y(0, 0, 0, 1))
# print(y(0, 0, 1, 0))
# print(y(0, 0, 1, 1))
print(a(0), a(1), a(2), b(0), b(1), c(0))

# third test
declare_block_scheme([Block(var_name="x", size=100, reverse=True),
HigherOrderBlock("y", (3, 4, 11, 2), reverse=True),
AlternatingBlock(["a", "b", "c"], 100, reverse=True)],
globals())
dic)
x = dic['x']
a, b, c = dic['a'], dic['b'], dic['c']
for i in range(10):
print(x(i))
print(y(0, 0, 0, 0))
print(y(0, 0, 0, 1))
print(y(0, 0, 1, 0))
print(y(0, 0, 1, 1))
print(a(0), a(1), a(2), b(0), b(1), c(0))
declare_block_scheme(["a", "b", "c"], globals())
# y are currently broken ?
# print(y(0, 0, 0, 0))
# print(y(0, 0, 0, 1))
# print(y(0, 0, 1, 0))
# print(y(0, 0, 1, 1))

# a also broken ?
# print(a(0), a(1), a(2), b(0), b(1), c(0))

# fourth test
declare_block_scheme(["a", "b", "c"], dic)
a, b, c = dic['a'], dic['b'], dic['c']
print(a, b, c)


if __name__ == '__main__':
main()
11 changes: 5 additions & 6 deletions src/sage/rings/polynomial/pbori/cnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ def choose(s):
if e.constant() and not e.terminal_one():
indices.append(nav.value())
nav = t
else:
if self.random_generator.randint(0, 1):
indices.append(nav.value())
nav = t
elif self.random_generator.randint(0, 1):
indices.append(nav.value())
nav = t

else:
nav = e
else:
nav = e
assert nav.terminal_one()
res = self.one_set
for i in reversed(indices):
Expand Down
8 changes: 4 additions & 4 deletions src/sage/rings/polynomial/pbori/fglm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def fglm(I, from_ring, to_ring):

sage: from sage.rings.polynomial.pbori import *
sage: from sage.rings.polynomial.pbori.PyPolyBoRi import OrderCode
sage: from sage.rings.polynomial.pbori.fglm import fglm
sage: dp_asc = OrderCode.dp_asc
sage: r=declare_ring(['x','y','z'],dict())
sage: r = declare_ring(['x','y','z'],dict())
sage: old_ring = r
sage: new_ring = old_ring.clone(ordering=dp_asc)
sage: (x,y,z) = [old_ring.variable(i) for i in range(3)]
sage: ideal=[x+z, y+z]# lp Groebner basis
sage: from sage.rings.polynomial.pbori.fglm import fglm
sage: x,y,z = (old_ring.variable(i) for i in range(3))
sage: ideal = [x+z, y+z] # lp Groebner basis
sage: list(fglm(ideal, old_ring, new_ring))
[y + x, z + x]
"""
Expand Down
1 change: 0 additions & 1 deletion src/sage/rings/polynomial/pbori/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"""


from sage.features import FeatureNotPresentError
from sage.rings.polynomial.pbori.blocks import declare_ring as orig_declare_ring
from sage.rings.polynomial.pbori.pbori import VariableFactory
from sage.rings.polynomial.pbori.PyPolyBoRi import Ring
Expand Down
10 changes: 5 additions & 5 deletions src/sage/rings/polynomial/pbori/gbcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


def get_options_from_function(f):
(argnames, varargs, varopts, defaults) = getargspec(f)[:4]
argnames, varargs, varopts, defaults = getargspec(f)[:4]
return dict(zip(argnames[-len(defaults):], defaults))


Expand Down Expand Up @@ -221,8 +221,8 @@ def wrapper(I, **kwds):
print("preprocessing for option:", option)

local_symbols = copy(locals())
(I, state) = pre(**{k: v for (k, v) in local_symbols.items()
if k in pre_args})
I, state = pre(**{k: v for (k, v) in local_symbols.items()
if k in pre_args})
I = f(I, **kwds)
if option_set and post:
post_args = getargspec(post)[0]
Expand Down Expand Up @@ -271,7 +271,7 @@ def invert_all_post(I, state):


def llfirst_pre(I, prot):
(eliminated, llnf, I) = eliminate(I, on_the_fly=False, prot=prot)
eliminated, llnf, I = eliminate(I, on_the_fly=False, prot=prot)
return (I, eliminated)


Expand Down Expand Up @@ -396,7 +396,7 @@ def other_ordering_pre(I, option_set, kwds):


def llfirstonthefly_pre(I, prot):
(eliminated, llnf, I) = eliminate(I, on_the_fly=True)
eliminated, llnf, I = eliminate(I, on_the_fly=True)
return (I, eliminated)


Expand Down
3 changes: 1 addition & 2 deletions src/sage/rings/polynomial/pbori/nf.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ def build_and_print_matrices_deg_colored(v, strat):
polys_in_mat.sort(key=pkey)
global mat_counter
mat_counter = mat_counter + 1
from PIL import Image
from PIL import ImageColor
from PIL import Image, ImageColor

rows = len(polys_in_mat)
cols = len(m2i)
Expand Down
36 changes: 18 additions & 18 deletions src/sage/rings/polynomial/pbori/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Copyright 2008 The PolyBoRi Team
"""
import copyreg
import os
from zlib import compress, decompress

from sage.rings.polynomial.pbori.gbcore import groebner_basis
Expand All @@ -23,9 +24,10 @@
)


def to_fast_pickable(l):
def to_fast_pickable(l) -> list:
r"""
Convert a list of polynomials into a builtin Python value, which is fast pickable and compact.
Convert a list of polynomials into a builtin Python value,
which is fast pickable and compact.

INPUT:

Expand Down Expand Up @@ -98,7 +100,7 @@ def find_navs(nav):

nodes_sorted = sorted(nodes, key=CCuddNavigator.value)
nodes2i = {one: 1, zero: 0}
for (i, n) in enumerate(nodes_sorted):
for i, n in enumerate(nodes_sorted):
nodes2i[n] = i + 2

for i in range(len(nodes_sorted)):
Expand All @@ -110,7 +112,7 @@ def find_navs(nav):
return [[nodes2i[f.set().navigation()] for f in l], nodes_sorted]


def from_fast_pickable(l, r):
def from_fast_pickable(l, r) -> list:
r"""
Undo the operation :func:`to_fast_pickable`.

Expand Down Expand Up @@ -151,10 +153,10 @@ def from_fast_pickable(l, r):
[x(0)*x(1), 0, 1, x(3)]
"""
i2poly = {0: r.zero(), 1: r.one()}
(indices, terms) = l
indices, terms = l

for i in reversed(range(len(terms))):
(v, t, e) = terms[i]
for i in range(len(terms) - 1, -1, - 1):
v, t, e = terms[i]
t = i2poly[t]
e = i2poly[e]
terms[i] = if_then_else(v, t, e)
Expand All @@ -163,7 +165,7 @@ def from_fast_pickable(l, r):


def _calculate_gb_with_keywords(args):
(I, kwds_as_single_arg) = args
I, kwds_as_single_arg = args
return groebner_basis(I, **kwds_as_single_arg)


Expand Down Expand Up @@ -204,18 +206,17 @@ def pickle_var(self):


def _decode_ring(code):
import os
(identifier, data, varnames, blocks) = code
identifier, data, varnames, blocks = code

global _polybori_parallel_rings
try:
_polybori_parallel_rings
except NameError:
_polybori_parallel_rings = {}

for key in [key for key in _polybori_parallel_rings
if not _polybori_parallel_rings[key][0]()]:
del _polybori_parallel_rings[key]
for key in list(_polybori_parallel_rings):
if not _polybori_parallel_rings[key][0]():
del _polybori_parallel_rings[key]

if identifier in _polybori_parallel_rings:
ring = _polybori_parallel_rings[identifier][0]()
Expand All @@ -224,7 +225,7 @@ def _decode_ring(code):

if not ring:
varnames = decompress(varnames).split('\n')
(nvars, ordercode) = data
nvars, ordercode = data
ring = Ring(nvars, ordercode, names=varnames, blocks=blocks)
storage_data = (WeakRingRef(ring), code)
_polybori_parallel_rings[identifier] = storage_data
Expand All @@ -234,7 +235,6 @@ def _decode_ring(code):


def _encode_ring(ring):
import os
identifier = (ring.id(), os.getpid())

global _polybori_parallel_rings
Expand All @@ -243,9 +243,9 @@ def _encode_ring(ring):
except NameError:
_polybori_parallel_rings = {}

for key in [key for key in _polybori_parallel_rings
if not _polybori_parallel_rings[key][0]()]:
del _polybori_parallel_rings[key]
for key in list(_polybori_parallel_rings):
if not _polybori_parallel_rings[key][0]():
del _polybori_parallel_rings[key]

if identifier in _polybori_parallel_rings:
code = _polybori_parallel_rings[identifier][1]
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/polynomial/pbori/specialsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def power_set(variables):


if __name__ == '__main__':
from .blocks import declare_ring, Block
from .blocks import Block, declare_ring
r = declare_ring([Block("x", 10000)], globals())
print(list(all_monomials_of_degree_d(0, [Variable(i) for i in range(100)])))
print(list(all_monomials_of_degree_d(1, [Variable(i) for i in range(10)])))
Expand Down
Loading