Skip to content

Commit 451f887

Browse files
author
Release Manager
committed
gh-40860: some care for pbori just a few details ; converting the "main" function of `blocks` into a doctest ### 📝 Checklist - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. URL: #40860 Reported by: Frédéric Chapoton Reviewer(s): Dima Pasechnik
2 parents 36741a5 + e28a88c commit 451f887

File tree

9 files changed

+115
-68
lines changed

9 files changed

+115
-68
lines changed

src/sage/rings/polynomial/pbori/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
Electronic Proceedings of the MEGA 2007 - Effective Methods in Algebraic Geometry, Strobl, Austria, June 2007.
3030
http://www.ricam.oeaw.ac.at/mega2007/electronic/electronic.html
3131
"""
32-
from sage.misc.lazy_import import lazy_import
3332
from .PyPolyBoRi import Ring, Polynomial, Monomial, Variable
3433

3534
# Get all-inclusive groebner routine

src/sage/rings/polynomial/pbori/blocks.py

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from itertools import chain, islice
32

43
from sage.rings.polynomial.pbori.pbori import (
@@ -157,7 +156,7 @@ class HigherOrderBlock:
157156
r"""
158157
HigherOrderBlocks are multidimensional blocks of variables.
159158
160-
For each dimension a separate start_index and size can be specified.
159+
For each dimension a separate ``start_index`` and ``size`` can be specified.
161160
162161
var_name : variables will be called <var_name>(multiindex), where
163162
multiindex is a tuple of the size <size_tuple>
@@ -353,23 +352,22 @@ def if_then(i, t, supposed_to_be_valid=True):
353352

354353
def declare_ring(blocks, context=None):
355354
r"""
356-
Declare Ring is the preferred function to create a ring and declare a variable scheme,
357-
the number of variables is automatically determined, usually you pass globals() as context
358-
argument to store the ring and the variable mapping.
355+
Declare Ring is the preferred function to create a ring and declare a variable scheme.
356+
357+
The number of variables is automatically determined. Usually you
358+
pass ``globals()`` as context argument to store the ring and the
359+
variable mapping.
359360
360361
EXAMPLES::
361362
362363
sage: from sage.rings.polynomial.pbori import *
363364
sage: declare_ring([Block("x",10),Block("y",5)],globals())
364365
Boolean PolynomialRing in x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, y0, y1, y2, y3, y4
365366
366-
gives a ring with x(0..9),y(0..4) and registers the ring as r, and the variable
367-
blocks x and y in the context dictionary globals(), which consists of the global
368-
variables of the python module
367+
gives a ring with x(0..9),y(0..4) and registers the ring as r, and
368+
the variable blocks x and y in the context dictionary ``globals()``,
369+
which consists of the global variables of the python module
369370
"""
370-
if context is None:
371-
context = sys.modules['__main__'].__dict__
372-
373371
def canonicalize(blocks):
374372
for elt in blocks:
375373
if isinstance(elt, str):
@@ -408,39 +406,92 @@ def declare_block_scheme(blocks, context):
408406
context["number_of_declared_vars"] = start
409407

410408

411-
def main():
409+
def main_test():
410+
"""
411+
EXAMPLES::
412+
413+
sage: from sage.rings.polynomial.pbori.blocks import main_test
414+
sage: main_test()
415+
x(0)
416+
x(1)
417+
x(2)
418+
x(3)
419+
x(4)
420+
x(5)
421+
x(6)
422+
x(7)
423+
x(8)
424+
x(9)
425+
['a(0)', 'b(0)', 'c(0)', 'a(1)', 'b(1)', ...]
426+
x(0)
427+
x(1)
428+
x(2)
429+
x(3)
430+
x(4)
431+
x(5)
432+
x(6)
433+
x(7)
434+
x(8)
435+
x(9)
436+
x(364) x(367) x(370) x(365) x(368) x(366)
437+
x(99)
438+
x(98)
439+
x(97)
440+
x(96)
441+
x(95)
442+
x(94)
443+
x(93)
444+
x(92)
445+
x(91)
446+
x(90)
447+
x(0) x(1) x(2)
448+
"""
412449
r = Ring(1000)
450+
dic = {"r": r}
451+
dic["internalVariable"] = VariableFactory(r)
452+
453+
# first test
413454
ablock = AlternatingBlock(["a", "b", "c"], 100)
414-
declare_block_scheme([ablock], globals())
455+
declare_block_scheme([ablock], dic)
415456
for i in range(10):
416457
print(r.variable(i))
417-
418458
print(list(ablock))
459+
460+
# second test
419461
declare_block_scheme([Block(var_name="x", size=100),
420462
HigherOrderBlock("y", (3, 4, 11, 2)),
421463
AlternatingBlock(["a", "b", "c"], 100)],
422-
globals())
464+
dic)
465+
x = dic['x']
466+
a, b, c = dic['a'], dic['b'], dic['c']
423467
for i in range(10):
424468
print(x(i))
425-
print(y(0, 0, 0, 0))
426-
print(y(0, 0, 0, 1))
427-
print(y(0, 0, 1, 0))
428-
print(y(0, 0, 1, 1))
469+
# y are currently broken ?
470+
# print(y(0, 0, 0, 0))
471+
# print(y(0, 0, 0, 1))
472+
# print(y(0, 0, 1, 0))
473+
# print(y(0, 0, 1, 1))
429474
print(a(0), a(1), a(2), b(0), b(1), c(0))
475+
476+
# third test
430477
declare_block_scheme([Block(var_name="x", size=100, reverse=True),
431478
HigherOrderBlock("y", (3, 4, 11, 2), reverse=True),
432479
AlternatingBlock(["a", "b", "c"], 100, reverse=True)],
433-
globals())
480+
dic)
481+
x = dic['x']
482+
a, b, c = dic['a'], dic['b'], dic['c']
434483
for i in range(10):
435484
print(x(i))
436-
print(y(0, 0, 0, 0))
437-
print(y(0, 0, 0, 1))
438-
print(y(0, 0, 1, 0))
439-
print(y(0, 0, 1, 1))
440-
print(a(0), a(1), a(2), b(0), b(1), c(0))
441-
declare_block_scheme(["a", "b", "c"], globals())
485+
# y are currently broken ?
486+
# print(y(0, 0, 0, 0))
487+
# print(y(0, 0, 0, 1))
488+
# print(y(0, 0, 1, 0))
489+
# print(y(0, 0, 1, 1))
490+
491+
# a also broken ?
492+
# print(a(0), a(1), a(2), b(0), b(1), c(0))
493+
494+
# fourth test
495+
declare_block_scheme(["a", "b", "c"], dic)
496+
a, b, c = dic['a'], dic['b'], dic['c']
442497
print(a, b, c)
443-
444-
445-
if __name__ == '__main__':
446-
main()

src/sage/rings/polynomial/pbori/cnf.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ def choose(s):
4545
if e.constant() and not e.terminal_one():
4646
indices.append(nav.value())
4747
nav = t
48-
else:
49-
if self.random_generator.randint(0, 1):
50-
indices.append(nav.value())
51-
nav = t
48+
elif self.random_generator.randint(0, 1):
49+
indices.append(nav.value())
50+
nav = t
5251

53-
else:
54-
nav = e
52+
else:
53+
nav = e
5554
assert nav.terminal_one()
5655
res = self.one_set
5756
for i in reversed(indices):

src/sage/rings/polynomial/pbori/fglm.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ def fglm(I, from_ring, to_ring):
2222
2323
sage: from sage.rings.polynomial.pbori import *
2424
sage: from sage.rings.polynomial.pbori.PyPolyBoRi import OrderCode
25+
sage: from sage.rings.polynomial.pbori.fglm import fglm
2526
sage: dp_asc = OrderCode.dp_asc
26-
sage: r=declare_ring(['x','y','z'],dict())
27+
sage: r = declare_ring(['x','y','z'],dict())
2728
sage: old_ring = r
2829
sage: new_ring = old_ring.clone(ordering=dp_asc)
29-
sage: (x,y,z) = [old_ring.variable(i) for i in range(3)]
30-
sage: ideal=[x+z, y+z]# lp Groebner basis
31-
sage: from sage.rings.polynomial.pbori.fglm import fglm
30+
sage: x,y,z = (old_ring.variable(i) for i in range(3))
31+
sage: ideal = [x+z, y+z] # lp Groebner basis
3232
sage: list(fglm(ideal, old_ring, new_ring))
3333
[y + x, z + x]
3434
"""

src/sage/rings/polynomial/pbori/frontend.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"""
3232

3333

34-
from sage.features import FeatureNotPresentError
3534
from sage.rings.polynomial.pbori.blocks import declare_ring as orig_declare_ring
3635
from sage.rings.polynomial.pbori.pbori import VariableFactory
3736
from sage.rings.polynomial.pbori.PyPolyBoRi import Ring

src/sage/rings/polynomial/pbori/gbcore.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

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

3232

@@ -221,8 +221,8 @@ def wrapper(I, **kwds):
221221
print("preprocessing for option:", option)
222222

223223
local_symbols = copy(locals())
224-
(I, state) = pre(**{k: v for (k, v) in local_symbols.items()
225-
if k in pre_args})
224+
I, state = pre(**{k: v for (k, v) in local_symbols.items()
225+
if k in pre_args})
226226
I = f(I, **kwds)
227227
if option_set and post:
228228
post_args = getargspec(post)[0]
@@ -271,7 +271,7 @@ def invert_all_post(I, state):
271271

272272

273273
def llfirst_pre(I, prot):
274-
(eliminated, llnf, I) = eliminate(I, on_the_fly=False, prot=prot)
274+
eliminated, llnf, I = eliminate(I, on_the_fly=False, prot=prot)
275275
return (I, eliminated)
276276

277277

@@ -396,7 +396,7 @@ def other_ordering_pre(I, option_set, kwds):
396396

397397

398398
def llfirstonthefly_pre(I, prot):
399-
(eliminated, llnf, I) = eliminate(I, on_the_fly=True)
399+
eliminated, llnf, I = eliminate(I, on_the_fly=True)
400400
return (I, eliminated)
401401

402402

src/sage/rings/polynomial/pbori/nf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ def build_and_print_matrices_deg_colored(v, strat):
146146
polys_in_mat.sort(key=pkey)
147147
global mat_counter
148148
mat_counter = mat_counter + 1
149-
from PIL import Image
150-
from PIL import ImageColor
149+
from PIL import Image, ImageColor
151150

152151
rows = len(polys_in_mat)
153152
cols = len(m2i)

src/sage/rings/polynomial/pbori/parallel.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Copyright 2008 The PolyBoRi Team
77
"""
88
import copyreg
9+
import os
910
from zlib import compress, decompress
1011

1112
from sage.rings.polynomial.pbori.gbcore import groebner_basis
@@ -23,9 +24,10 @@
2324
)
2425

2526

26-
def to_fast_pickable(l):
27+
def to_fast_pickable(l) -> list:
2728
r"""
28-
Convert a list of polynomials into a builtin Python value, which is fast pickable and compact.
29+
Convert a list of polynomials into a builtin Python value,
30+
which is fast pickable and compact.
2931
3032
INPUT:
3133
@@ -98,7 +100,7 @@ def find_navs(nav):
98100

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

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

112114

113-
def from_fast_pickable(l, r):
115+
def from_fast_pickable(l, r) -> list:
114116
r"""
115117
Undo the operation :func:`to_fast_pickable`.
116118
@@ -151,10 +153,10 @@ def from_fast_pickable(l, r):
151153
[x(0)*x(1), 0, 1, x(3)]
152154
"""
153155
i2poly = {0: r.zero(), 1: r.one()}
154-
(indices, terms) = l
156+
indices, terms = l
155157

156-
for i in reversed(range(len(terms))):
157-
(v, t, e) = terms[i]
158+
for i in range(len(terms) - 1, -1, - 1):
159+
v, t, e = terms[i]
158160
t = i2poly[t]
159161
e = i2poly[e]
160162
terms[i] = if_then_else(v, t, e)
@@ -163,7 +165,7 @@ def from_fast_pickable(l, r):
163165

164166

165167
def _calculate_gb_with_keywords(args):
166-
(I, kwds_as_single_arg) = args
168+
I, kwds_as_single_arg = args
167169
return groebner_basis(I, **kwds_as_single_arg)
168170

169171

@@ -204,18 +206,17 @@ def pickle_var(self):
204206

205207

206208
def _decode_ring(code):
207-
import os
208-
(identifier, data, varnames, blocks) = code
209+
identifier, data, varnames, blocks = code
209210

210211
global _polybori_parallel_rings
211212
try:
212213
_polybori_parallel_rings
213214
except NameError:
214215
_polybori_parallel_rings = {}
215216

216-
for key in [key for key in _polybori_parallel_rings
217-
if not _polybori_parallel_rings[key][0]()]:
218-
del _polybori_parallel_rings[key]
217+
for key in list(_polybori_parallel_rings):
218+
if not _polybori_parallel_rings[key][0]():
219+
del _polybori_parallel_rings[key]
219220

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

225226
if not ring:
226227
varnames = decompress(varnames).split('\n')
227-
(nvars, ordercode) = data
228+
nvars, ordercode = data
228229
ring = Ring(nvars, ordercode, names=varnames, blocks=blocks)
229230
storage_data = (WeakRingRef(ring), code)
230231
_polybori_parallel_rings[identifier] = storage_data
@@ -234,7 +235,6 @@ def _decode_ring(code):
234235

235236

236237
def _encode_ring(ring):
237-
import os
238238
identifier = (ring.id(), os.getpid())
239239

240240
global _polybori_parallel_rings
@@ -243,9 +243,9 @@ def _encode_ring(ring):
243243
except NameError:
244244
_polybori_parallel_rings = {}
245245

246-
for key in [key for key in _polybori_parallel_rings
247-
if not _polybori_parallel_rings[key][0]()]:
248-
del _polybori_parallel_rings[key]
246+
for key in list(_polybori_parallel_rings):
247+
if not _polybori_parallel_rings[key][0]():
248+
del _polybori_parallel_rings[key]
249249

250250
if identifier in _polybori_parallel_rings:
251251
code = _polybori_parallel_rings[identifier][1]

src/sage/rings/polynomial/pbori/specialsets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def power_set(variables):
8383

8484

8585
if __name__ == '__main__':
86-
from .blocks import declare_ring, Block
86+
from .blocks import Block, declare_ring
8787
r = declare_ring([Block("x", 10000)], globals())
8888
print(list(all_monomials_of_degree_d(0, [Variable(i) for i in range(100)])))
8989
print(list(all_monomials_of_degree_d(1, [Variable(i) for i in range(10)])))

0 commit comments

Comments
 (0)