diff --git a/src/sage/rings/polynomial/pbori/__init__.py b/src/sage/rings/polynomial/pbori/__init__.py index 325ed646516..c56df0ef5a1 100644 --- a/src/sage/rings/polynomial/pbori/__init__.py +++ b/src/sage/rings/polynomial/pbori/__init__.py @@ -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 diff --git a/src/sage/rings/polynomial/pbori/blocks.py b/src/sage/rings/polynomial/pbori/blocks.py index b4e171627e2..d9b8f46f390 100644 --- a/src/sage/rings/polynomial/pbori/blocks.py +++ b/src/sage/rings/polynomial/pbori/blocks.py @@ -1,4 +1,3 @@ -import sys from itertools import chain, islice from sage.rings.polynomial.pbori.pbori import ( @@ -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 (multiindex), where multiindex is a tuple of the size @@ -353,9 +352,11 @@ 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:: @@ -363,13 +364,10 @@ def declare_ring(blocks, context=None): 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): @@ -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() diff --git a/src/sage/rings/polynomial/pbori/cnf.py b/src/sage/rings/polynomial/pbori/cnf.py index 2100797e2da..216b1d7b13a 100644 --- a/src/sage/rings/polynomial/pbori/cnf.py +++ b/src/sage/rings/polynomial/pbori/cnf.py @@ -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): diff --git a/src/sage/rings/polynomial/pbori/fglm.py b/src/sage/rings/polynomial/pbori/fglm.py index 4a37ab1782b..e12e5458c78 100644 --- a/src/sage/rings/polynomial/pbori/fglm.py +++ b/src/sage/rings/polynomial/pbori/fglm.py @@ -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] """ diff --git a/src/sage/rings/polynomial/pbori/frontend.py b/src/sage/rings/polynomial/pbori/frontend.py index 83e24430b59..c50108e2cda 100644 --- a/src/sage/rings/polynomial/pbori/frontend.py +++ b/src/sage/rings/polynomial/pbori/frontend.py @@ -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 diff --git a/src/sage/rings/polynomial/pbori/gbcore.py b/src/sage/rings/polynomial/pbori/gbcore.py index 7978efd4c20..1900b0e4414 100644 --- a/src/sage/rings/polynomial/pbori/gbcore.py +++ b/src/sage/rings/polynomial/pbori/gbcore.py @@ -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)) @@ -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] @@ -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) @@ -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) diff --git a/src/sage/rings/polynomial/pbori/nf.py b/src/sage/rings/polynomial/pbori/nf.py index 9a45b3c112a..0b8e74cd2dd 100644 --- a/src/sage/rings/polynomial/pbori/nf.py +++ b/src/sage/rings/polynomial/pbori/nf.py @@ -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) diff --git a/src/sage/rings/polynomial/pbori/parallel.py b/src/sage/rings/polynomial/pbori/parallel.py index c3db1b63e49..23f4303918c 100644 --- a/src/sage/rings/polynomial/pbori/parallel.py +++ b/src/sage/rings/polynomial/pbori/parallel.py @@ -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 @@ -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: @@ -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)): @@ -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`. @@ -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) @@ -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) @@ -204,8 +206,7 @@ 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: @@ -213,9 +214,9 @@ def _decode_ring(code): 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]() @@ -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 @@ -234,7 +235,6 @@ def _decode_ring(code): def _encode_ring(ring): - import os identifier = (ring.id(), os.getpid()) global _polybori_parallel_rings @@ -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] diff --git a/src/sage/rings/polynomial/pbori/specialsets.py b/src/sage/rings/polynomial/pbori/specialsets.py index 23c4ed47255..6d77f70f08e 100644 --- a/src/sage/rings/polynomial/pbori/specialsets.py +++ b/src/sage/rings/polynomial/pbori/specialsets.py @@ -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)])))