Skip to content

Commit

Permalink
gh-35392: partial cython linting in algebras/
Browse files Browse the repository at this point in the history
    
using --ignore=E501,E731,E741

<!-- Please provide a concise, informative and self-explanatory title.
-->
<!-- Don't put issue numbers in the title. Put it in the Description
below. -->
<!-- For example, instead of "Fixes #12345", use "Add a new method to
multiply two integers" -->

### 📚 Description

Making use of the `cython-lint` command, and fixing manually the issues.
Done here in `algebras` outside of `quatalg`

<!-- Describe your changes here in detail. -->
<!-- Why is this change required? What problem does it solve? -->
<!-- If this PR resolves an open issue, please link to it here. For
example "Fixes #12345". -->
<!-- If your change requires a documentation PR, please link it
appropriately. -->

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x
]`. -->

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on
- #12345: short description why this is a dependency
- #34567: ...
-->

<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: #35392
Reported by: Frédéric Chapoton
Reviewer(s): David Coudert
  • Loading branch information
Release Manager committed Apr 12, 2023
2 parents 54c1a2b + 4700c30 commit cc251ac
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 128 deletions.
2 changes: 0 additions & 2 deletions src/sage/algebras/clifford_algebra_element.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Clifford algebra elements
"""

from sage.modules.with_basis.indexed_element cimport IndexedFreeModuleElement
from sage.data_structures.bitset cimport FrozenBitset

Expand All @@ -14,4 +13,3 @@ cdef class ExteriorAlgebraElement(CliffordAlgebraElement):

cdef class CohomologyRAAGElement(CliffordAlgebraElement):
pass

53 changes: 26 additions & 27 deletions src/sage/algebras/clifford_algebra_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ AUTHORS:
- Travis Scrimshaw (2013-09-06): Initial version
- Trevor Karn (2022-07-10): Rewrite multiplication using bitsets
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2022 Trevor K. Karn <karnx018 at umn.edu>
# (C) 2022 Travis Scrimshaw <tcscrims at gmail.com>
#
Expand All @@ -16,8 +15,7 @@ AUTHORS:
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************

# ****************************************************************************
from sage.structure.parent cimport Parent
from sage.data_structures.bitset cimport Bitset
from sage.algebras.weyl_algebra import repr_from_monomials
Expand Down Expand Up @@ -125,7 +123,7 @@ cdef class CliffordAlgebraElement(IndexedFreeModuleElement):

for ml, cl in self:
# Distribute the current term ``cl`` * ``ml`` over ``other``.
cur = copy(other._monomial_coefficients) # The current distribution of the term
cur = copy(other._monomial_coefficients) # The current distribution of the term
for i in reversed(ml):
# Distribute the current factor ``e[i]`` (the ``i``-th
# element of the standard basis).
Expand All @@ -142,10 +140,10 @@ cdef class CliffordAlgebraElement(IndexedFreeModuleElement):
break
# Add the additional term from the commutation
# get a non-frozen bitset to manipulate
t = Bitset(mr) # a mutable copy
t = Bitset(mr) # a mutable copy
t.discard(j)
t = FrozenBitset(t)
next_level[t] = next_level.get(t, zero) + cr * Q[i,j]
next_level[t] = next_level.get(t, zero) + cr * Q[i, j]
# Note: ``Q[i,j] == Q(e[i]+e[j]) - Q(e[i]) - Q(e[j])`` for
# ``i != j``, where ``e[k]`` is the ``k``-th standard
# basis vector.
Expand All @@ -154,16 +152,16 @@ cdef class CliffordAlgebraElement(IndexedFreeModuleElement):
del next_level[t]

# Check to see if we have a squared term or not
mr = Bitset(mr) # temporarily mutable
mr = Bitset(mr) # temporarily mutable
if i in mr:
mr.discard(i)
cr *= Q[i,i]
cr *= Q[i, i]
# Note: ``Q[i,i] == Q(e[i])`` where ``e[i]`` is the
# ``i``-th standard basis vector.
else:
# mr is implicitly sorted
mr.add(i)
mr = FrozenBitset(mr) # refreeze it
mr = FrozenBitset(mr) # refreeze it
next_level[mr] = next_level.get(mr, zero) + cr
if next_level[mr] == zero:
del next_level[mr]
Expand Down Expand Up @@ -213,11 +211,6 @@ cdef class CliffordAlgebraElement(IndexedFreeModuleElement):
sage: r * 2 # indirect doctest
2*x*y*z + 2*x*y + 2*x*z + 2*y*z + 2*x + 2*y + 2*z + 2
"""
cdef dict d
cdef list to_remove
cdef Py_ssize_t num_cross, tot_cross, i, j
cdef FrozenBitset ml

if supp.isempty(): # Multiplication by a base ring element
if coeff == self._parent._base.one():
return self
Expand Down Expand Up @@ -315,7 +308,8 @@ cdef class CliffordAlgebraElement(IndexedFreeModuleElement):
sage: all(x.reflection().reflection() == x for x in Cl.basis())
True
"""
return self.__class__(self._parent, {m: (-1)**len(m) * c for m,c in self})
return self.__class__(self._parent,
{m: (-1)**len(m) * c for m, c in self})

degree_negation = reflection

Expand Down Expand Up @@ -361,7 +355,7 @@ cdef class CliffordAlgebraElement(IndexedFreeModuleElement):
if not self._monomial_coefficients:
return P.zero()
g = P.gens()
return P.sum(c * P.prod(g[i] for i in reversed(m)) for m,c in self)
return P.sum(c * P.prod(g[i] for i in reversed(m)) for m, c in self)

def conjugate(self):
r"""
Expand Down Expand Up @@ -475,7 +469,7 @@ cdef class ExteriorAlgebraElement(CliffordAlgebraElement):
ml = FrozenBitset()
if ml in self._monomial_coefficients:
const_coeff = self._monomial_coefficients[ml]
d = dict(rhs._monomial_coefficients) # Make a shallow copy
d = dict(rhs._monomial_coefficients) # Make a shallow copy
to_remove = []
if const_coeff != P._base.one():
for k in d:
Expand All @@ -488,10 +482,12 @@ cdef class ExteriorAlgebraElement(CliffordAlgebraElement):
d = {}

n = P.ngens()
for ml, cl in self._monomial_coefficients.items(): # ml for "monomial on the left"
for ml, cl in self._monomial_coefficients.items():
# ml for "monomial on the left"
if ml.isempty(): # We already handled the trivial element
continue
for mr,cr in rhs._monomial_coefficients.items(): # mr for "monomial on the right"
for mr, cr in rhs._monomial_coefficients.items():
# mr for "monomial on the right"
if mr.isempty():
t = ml
else:
Expand All @@ -502,7 +498,7 @@ cdef class ExteriorAlgebraElement(CliffordAlgebraElement):
it = iter(mr)
j = next(it)

num_cross = 0 # keep track of the number of signs
num_cross = 0 # keep track of the number of signs
tot_cross = 0
for i in ml:
while i > j:
Expand Down Expand Up @@ -576,15 +572,16 @@ cdef class ExteriorAlgebraElement(CliffordAlgebraElement):

n = self._parent.ngens()
d = {}
for ml, cl in self._monomial_coefficients.items(): # ml for "monomial on the left"
for ml, cl in self._monomial_coefficients.items():
# ml for "monomial on the left"
if not ml.isdisjoint(supp):
# if they intersect nontrivially, move along.
continue
t = <FrozenBitset> ml._union(supp)
it = iter(supp)
j = next(it)

num_cross = 0 # keep track of the number of signs
num_cross = 0 # keep track of the number of signs
tot_cross = 0
for i in ml:
while i > j:
Expand Down Expand Up @@ -668,15 +665,16 @@ cdef class ExteriorAlgebraElement(CliffordAlgebraElement):
const_coeff = self._monomial_coefficients.pop(mr)
d[supp] = const_coeff

for mr, cr in self._monomial_coefficients.items(): # mr for "monomial on the right"
for mr, cr in self._monomial_coefficients.items():
# mr for "monomial on the right"
if not supp.isdisjoint(mr):
# if they intersect nontrivially, move along.
continue
t = <FrozenBitset> supp._union(mr)
it = iter(mr)
j = next(it) # We assume mr is non-empty here

num_cross = 0 # keep track of the number of signs
num_cross = 0 # keep track of the number of signs
tot_cross = 0
for i in supp:
while i > j:
Expand Down Expand Up @@ -839,7 +837,7 @@ cdef class ExteriorAlgebraElement(CliffordAlgebraElement):
"""
P = self._parent
return P.sum([c * cx * P.interior_product_on_basis(m, mx)
for m,c in self for mx,cx in x])
for m, c in self for mx, cx in x])

antiderivation = interior_product

Expand Down Expand Up @@ -968,7 +966,8 @@ cdef class CohomologyRAAGElement(CliffordAlgebraElement):
tp = tuple(sorted(mr + ml))
if any(tp[i] == tp[i+1] for i in range(len(tp)-1)): # e_i ^ e_i = 0
continue
if tp not in I: # not an independent set, so this term is also 0
if tp not in I:
# not an independent set, so this term is also 0
continue

t = list(mr)
Expand Down
2 changes: 0 additions & 2 deletions src/sage/algebras/exterior_algebra_groebner.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Exterior algebras Gröbner bases
"""

from sage.data_structures.bitset cimport FrozenBitset
from sage.rings.integer cimport Integer
from sage.algebras.clifford_algebra_element cimport CliffordAlgebraElement
Expand Down Expand Up @@ -53,4 +52,3 @@ cdef class GroebnerStrategyDegRevLex(GroebnerStrategy):

cdef class GroebnerStrategyDegLex(GroebnerStrategy):
pass

41 changes: 15 additions & 26 deletions src/sage/algebras/exterior_algebra_groebner.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ AUTHORS:
# ****************************************************************************

from cysignals.signals cimport sig_check
from sage.libs.gmp.mpz cimport mpz_sizeinbase, mpz_setbit, mpz_tstbit, mpz_cmp_si, mpz_sgn
from sage.data_structures.bitset_base cimport (bitset_t, bitset_init, bitset_first,
from sage.libs.gmp.mpz cimport mpz_sizeinbase, mpz_setbit, mpz_tstbit, mpz_sgn
from sage.data_structures.bitset_base cimport (bitset_init, bitset_first,
bitset_next, bitset_set_to, bitset_len)
from sage.structure.parent cimport Parent
from sage.structure.richcmp cimport richcmp, rich_to_bool
Expand Down Expand Up @@ -175,8 +175,8 @@ cdef class GroebnerStrategy:
Convert ``f`` into a ``GBElement``.
"""
cdef dict mc = <dict> f._monomial_coefficients
#if not mc:
# return GBElement(f, FrozenBitset(), -1)
# if not mc:
# return GBElement(f, FrozenBitset(), -1)
cdef Integer r = <Integer> max(self.bitset_to_int(k) for k in mc)
return GBElement(f, self.int_to_bitset(r), r)

Expand Down Expand Up @@ -232,7 +232,7 @@ cdef class GroebnerStrategy:
if self.build_S_poly(f0, f1):
L.add(self.partial_S_poly_right(f0, f1))
L.add(self.partial_S_poly_right(f1, f0))
else: # We compute a left Gröbner basis for two-sided ideals
else: # We compute a left Gröbner basis for two-sided ideals
for f0, f1 in P:
if self.build_S_poly(f0, f1):
L.add(self.partial_S_poly_left(f0, f1))
Expand Down Expand Up @@ -272,14 +272,14 @@ cdef class GroebnerStrategy:
cdef set L = self.preprocessing(P, G)
cdef Py_ssize_t i
from sage.matrix.constructor import matrix
cdef Integer r = Integer(2) ** self.rank - Integer(1) # r for "rank" or "reverso"
cdef Integer r = Integer(2) ** self.rank - Integer(1) # r for "rank" or "reverso"
M = matrix({(i, r - self.bitset_to_int(<FrozenBitset> m)): c
for i,f in enumerate(L)
for m,c in (<GBElement> f).elt._monomial_coefficients.items()},
for i, f in enumerate(L)
for m, c in (<GBElement> f).elt._monomial_coefficients.items()},
sparse=True)
M.echelonize() # Do this in place
lead_supports = set((<GBElement> f).lsi for f in L)
return [GBElement(self.E.element_class(self.E, {self.int_to_bitset(r - Integer(j)): c for j,c in M[i].iteritems()}),
return [GBElement(self.E.element_class(self.E, {self.int_to_bitset(r - Integer(j)): c for j, c in M[i].iteritems()}),
self.int_to_bitset(Integer(r - p)),
Integer(r - p))
for i, p in enumerate(M.pivots())
Expand Down Expand Up @@ -309,8 +309,7 @@ cdef class GroebnerStrategy:
"""
cdef FrozenBitset p0, p1
cdef long deg
cdef Py_ssize_t i, j, k
cdef set additions
cdef Py_ssize_t i, j
cdef GBElement f0, f1
cdef list G = [], Gp
cdef dict constructed = {}
Expand All @@ -321,7 +320,7 @@ cdef class GroebnerStrategy:
continue
f0 = self.build_elt(f)
if f0.lsi in constructed:
if f0 in constructed[f0.lsi]: # Already there
if f0 in constructed[f0.lsi]: # Already there
continue
constructed[f0.lsi].add(f0)
else:
Expand All @@ -338,7 +337,7 @@ cdef class GroebnerStrategy:
continue
f1 = self.build_elt(f)
if f1.lsi in constructed:
if f1 in constructed[f1.lsi]: # Already there
if f1 in constructed[f1.lsi]: # Already there
continue
constructed[f1.lsi].add(f1)
else:
Expand Down Expand Up @@ -367,7 +366,7 @@ cdef class GroebnerStrategy:
# Add the elements Gp to G when a new element is found
for f0 in Gp:
if f0.lsi in constructed:
if f0 in constructed[f0.lsi]: # Already there
if f0 in constructed[f0.lsi]: # Already there
continue
constructed[f0.lsi].add(f0)
else:
Expand Down Expand Up @@ -402,20 +401,16 @@ cdef class GroebnerStrategy:
cdef GBElement f0, f1

# Now that we have a Gröbner basis, we make this into a reduced Gröbner basis
cdef tuple supp
cdef bint did_reduction
cdef FrozenBitset lm, s
cdef Integer r
cdef Py_ssize_t num_zeros = 0
cdef Py_ssize_t n = len(G)
cdef set pairs = set((i, j) for i in range(n) for j in range(n) if i != j)

while pairs:
sig_check()
i,j = pairs.pop()
i, j = pairs.pop()
f0 = <GBElement> G[i]
f1 = <GBElement> G[j]
assert f0.elt._monomial_coefficients is not f1.elt._monomial_coefficients, (i,j)
assert f0.elt._monomial_coefficients is not f1.elt._monomial_coefficients, (i, j)
# We perform the classical reduction algorithm here on each pair
# TODO: Make this faster by using the previous technique?
if self.reduce_single(f0.elt, f1.elt):
Expand Down Expand Up @@ -500,7 +495,6 @@ cdef class GroebnerStrategy:
"""
cdef FrozenBitset lm = self.leading_support(g), s, t
cdef bint did_reduction = True, was_reduced=False
cdef tuple supp
cdef CliffordAlgebraElement gp

one = self.E._base.one()
Expand All @@ -521,7 +515,6 @@ cdef class GroebnerStrategy:
iaxpy(-coeff, gp._monomial_coefficients, f._monomial_coefficients)
return was_reduced


cdef Integer bitset_to_int(self, FrozenBitset X):
raise NotImplementedError

Expand Down Expand Up @@ -658,8 +651,6 @@ cdef class GroebnerStrategyDegRevLex(GroebnerStrategy):
"""
Convert a nonnegative integer ``n`` to a :class:`FrozenBitset`.
"""
cdef size_t i

if mpz_sgn(n.value) == 0:
return FrozenBitset()

Expand Down Expand Up @@ -701,8 +692,6 @@ cdef class GroebnerStrategyDegLex(GroebnerStrategy):
"""
Convert a nonnegative integer ``n`` to a :class:`FrozenBitset`.
"""
cdef size_t i

if mpz_sgn(n.value) == 0:
return FrozenBitset()

Expand Down
Loading

0 comments on commit cc251ac

Please sign in to comment.