Skip to content

Commit

Permalink
sagemathgh-37965: some polishing for pbori (ruff and pep8 mostly)
Browse files Browse the repository at this point in the history
    
just a basic polish of the ` pbori` folder, fixing pep8 warnings and
some ` ruff` suggestions

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
    
URL: sagemath#37965
Reported by: Frédéric Chapoton
Reviewer(s): Matthias Köppe
  • Loading branch information
Release Manager committed May 11, 2024
2 parents 67f1638 + 22ed71b commit ee4d24c
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 216 deletions.
59 changes: 30 additions & 29 deletions src/sage/rings/polynomial/pbori/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .PyPolyBoRi import (Ring, Polynomial, VariableFactory, Variable)


class Block():
class Block:
r"""
The block class represents a block of variables
<var_name>(start_index,...,start_index+size-1), it is the preferred
Expand Down Expand Up @@ -37,12 +37,12 @@ def register(self, start, context):
ring = ring_context['r']

var_func = VariableBlock(self.size, self.start_index, start, self.
reverse, ring)
reverse, ring)
var_func.__name__ = self.var_name
context[self.var_name] = var_func


class AlternatingBlock():
class AlternatingBlock:
r"""
The Alternating Block class is used for doing tricky variable
schemes,where base names vary, e.g.
Expand Down Expand Up @@ -74,15 +74,15 @@ def __getitem__(self, i):
def register(self, start, context):
def gen_var_func(var_pos):

class var_factory():
class var_factory:
def __init__(self, ring, index2pos, size):
self.ring = ring
self.index2pos = index2pos
self.size = size

def __call__(self, idx):
return self.ring.variable(self.index2pos[idx] * self.size +
var_pos + start)
var_pos + start)
ring_context = context
while isinstance(ring_context, PrefixedDictProxy):
ring_context = ring_context.wrapped
Expand All @@ -107,7 +107,8 @@ class AdderBlock(AlternatingBlock):
def __init__(self, adder_bits, sums="s", carries="c", input1="a",
input2="b", start_index=0):
AlternatingBlock.__init__(self, (sums, carries, input1, input2),
adder_bits, start_index=start_index, reverse=True)
adder_bits, start_index=start_index,
reverse=True)
self.input1 = input1
self.input2 = input2
self.sums = sums
Expand All @@ -124,13 +125,14 @@ def register(self, start, context):
a = shift(a, self.start_index)
b = shift(b, self.start_index)
carries = [Polynomial(a(0).ring().zero())]
last = carries[0]
for i in range(self.adder_bits):
c = 1 + (1 + a(i) * b(i)) * (1 + carries[-1] * a(i)) * (1 +
carries[-1] * b(i))
c = 1 + (1 + a(i) * b(i)) * (1 + last * a(i)) * (1 + last * b(i))
carries.append(c)
last = c

self.add_results = [a(i) + b(i) + carries[i] for i in range(self.
adder_bits)]
self.add_results = [a(i) + b(i) + carries[i]
for i in range(self.adder_bits)]
self.carries_polys = carries[1:]

# def s(i):
Expand All @@ -146,7 +148,7 @@ def implement(self, equations):
equations.append(self.c(i) + self.carries_polys[i])


class HigherOrderBlock():
class HigherOrderBlock:
r"""
HigherOrderBlocks are multidimensional blocks of variables.
Expand Down Expand Up @@ -192,14 +194,14 @@ def var_func(*indices):
context[self.var_name] = var_func


class InOutBlock():
class InOutBlock:
def __init__(self, out_size, in_size, output="out", input="in",
in_start_index=0, out_start_index=0,
out_reverse=False, in_reverse=False):
self.output = Block(var_name=output, start_index=out_start_index,
size=out_size, reverse=out_reverse)
size=out_size, reverse=out_reverse)
self.input = Block(var_name=input, start_index=in_start_index,
size=in_size, reverse=in_reverse)
size=in_size, reverse=in_reverse)
self.out_start_index = out_start_index

self.in_start_index = in_start_index
Expand All @@ -219,11 +221,11 @@ def register(self, start, context):
self.output.register(start, context)
self.input.register(start + len(self.output), context)
self.out_vars = shift(context[self.output.var_name], self.
out_start_index)
out_start_index)
self.in_vars = shift(context[self.input.var_name], self.in_start_index)


class MultiBlock():
class MultiBlock:
def __init__(self, sizes=None, var_names=["v"],
start_indices=[], reverses=None):
if reverses is None:
Expand All @@ -236,8 +238,9 @@ def __init__(self, sizes=None, var_names=["v"],
sizes += [1] * (len(var_names) - len(sizes))

self.blocks = [Block(var_name=var_names[idx], size=sizes[idx],
start_index=self.start_indices[idx], reverse=reverses[idx]) for
idx in range(len(var_names))]
start_index=self.start_indices[idx],
reverse=reverses[idx])
for idx in range(len(var_names))]

def __iter__(self):
return chain(*self.blocks)
Expand All @@ -247,19 +250,20 @@ def __getitem__(self, i):
# sum([bl.names for bl in self.blocks])[i]

def __len__(self):
return sum((len(bl) for bl in self.blocks))
return sum(len(bl) for bl in self.blocks)

def register(self, start, context):
offset = 0
for bl in self.blocks:
bl.register(start + offset, context)
offset += len(bl)

self.vars = [shift(context[self.blocks[idx].var_name], self.
start_indices[idx]) for idx in range(len(self.blocks))]
self.vars = [shift(context[self.blocks[idx].var_name],
self.start_indices[idx])
for idx in range(len(self.blocks))]


class PrefixedDictProxy():
class PrefixedDictProxy:
"""docstring for PrefixedDictProxy"""

def __init__(self, wrapped, prefix):
Expand All @@ -278,7 +282,7 @@ def __setitem__(self, k, v):
self.wrapped[self.prefix + k] = v


class MacroBlock():
class MacroBlock:
def __init__(self, prefix):

self.prefix = prefix
Expand All @@ -299,7 +303,7 @@ def __getitem__(self, i):
return self.prefix + "_" + next(islice(chain(*self.blocks), i, i + 1))

def __len__(self):
return sum((len(bl) for bl in self.blocks))
return sum(len(bl) for bl in self.blocks)

def resolve(self, localname):
return self.prefix + "_" + localname
Expand All @@ -324,7 +328,7 @@ def implement(self, equations):
equations += self.connections


class IfThen():
class IfThen:
def __init__(self, ifpart, thenpart, supposed_to_be_valid=True):
self.ifpart = [Polynomial(p) for p in ifpart]
self.thenpart = [Polynomial(p) for p in thenpart]
Expand Down Expand Up @@ -369,10 +373,7 @@ def canonicalize(blocks):
n = 0

for b in blocks:
if isinstance(b, str):
n = n + 1
else:
n = n + len(b)
n = n + 1 if isinstance(b, str) else n + len(b)

r = Ring(n, names=canonicalize(blocks))

Expand Down
8 changes: 4 additions & 4 deletions src/sage/rings/polynomial/pbori/cnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .statistics import used_vars_set


class CNFEncoder():
class CNFEncoder:
def __init__(self, r, random_seed=16):
self.random_generator = Random(random_seed)
self.one_set = r.one().set()
Expand Down Expand Up @@ -174,7 +174,7 @@ def dimacs_cnf(self, polynomial_system):
res = ["c cnf generated by PolyBoRi"]
r = polynomial_system[0].ring()
n_variables = r.n_variables()
res.append("p cnf %s %s" % (n_variables, len(clauses_list)))
res.append(f"p cnf {n_variables} {len(clauses_list)}")
res.extend(clauses_list)
return "\n".join(res)

Expand Down Expand Up @@ -213,7 +213,7 @@ def dimacs_encode_polynomial(self, p):
indices.append(0)
res = ["x" + " ".join(str(v) for v in indices)]
self.group_counter = self.group_counter + 1
group_comment = "\nc g %s %s" % (self.group_counter, str(p)[:30])
group_comment = f"\nc g {self.group_counter} {str(p)[:30]}"
return [c + group_comment for c in res]

def dimacs_cnf(self, polynomial_system):
Expand All @@ -234,6 +234,6 @@ def dimacs_cnf(self, polynomial_system):
"""
uv = list(used_vars_set(polynomial_system).variables())
res = super().dimacs_cnf(polynomial_system)
res += "\n" + "\n".join("c v %s %s" % (self.to_dimacs_index(v), v)
res += "\n" + "\n".join(f"c v {self.to_dimacs_index(v)} {v}"
for v in uv)
return res

0 comments on commit ee4d24c

Please sign in to comment.