Skip to content

Commit c898228

Browse files
author
Release Manager
committed
Trac #34930: a few pep8 details in modules
mostly about spaces URL: https://trac.sagemath.org/34930 Reported by: chapoton Ticket author(s): Frédéric Chapoton Reviewer(s): Matthias Koeppe
2 parents f0a7ebb + 705606a commit c898228

File tree

7 files changed

+48
-49
lines changed

7 files changed

+48
-49
lines changed

src/sage/modules/filtered_vector_space.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@
9999
in Vector space of dimension 3 over Algebraic Field
100100
"""
101101

102-
#*****************************************************************************
102+
# ***************************************************************************
103103
# Copyright (C) 2013 Volker Braun <[email protected]>
104104
#
105105
# Distributed under the terms of the GNU General Public License (GPL)
106106
# as published by the Free Software Foundation; either version 2 of
107107
# the License, or (at your option) any later version.
108-
# http://www.gnu.org/licenses/
109-
#*****************************************************************************
108+
# https://www.gnu.org/licenses/
109+
# ***************************************************************************
110110

111111
from sage.rings.all import QQ, ZZ, RDF, RR, Integer
112112
from sage.rings.infinity import InfinityRing, infinity, minus_infinity
@@ -275,7 +275,7 @@ def construct_from_dim_degree(dim, max_degree, base_ring, check):
275275
dim = ZZ(dim)
276276
from sage.matrix.constructor import identity_matrix
277277
generators = identity_matrix(base_ring, dim).columns()
278-
filtration = dict()
278+
filtration = {}
279279
if max_degree is None:
280280
max_degree = infinity
281281
filtration[normalize_degree(max_degree)] = range(dim)
@@ -314,7 +314,7 @@ def normalize_gen(v):
314314
generators = tuple(sorted(generators))
315315

316316
# normalize filtration data
317-
normalized = dict()
317+
normalized = {}
318318
for deg, gens_deg in filtration.items():
319319
indices = [generators.index(normalize_gen(v)) for v in gens_deg]
320320
normalized[deg] = tuple(indices)
@@ -373,13 +373,13 @@ def construct_from_generators_indices(generators, filtration, base_ring, check):
373373
v.set_immutable()
374374

375375
# normalize filtration data
376-
normalized = dict()
376+
normalized = {}
377377
for deg, gens in filtration.items():
378378
deg = normalize_degree(deg)
379-
gens = [ZZ(i) for i in gens]
380-
if any(i < 0 or i >= len(generators) for i in gens):
379+
gens = tuple(sorted(ZZ(i) for i in gens))
380+
if gens and (gens[0] < 0 or gens[-1] >= len(generators)):
381381
raise ValueError('generator index out of bounds')
382-
normalized[deg] = tuple(sorted(gens))
382+
normalized[deg] = gens
383383
try:
384384
del normalized[minus_infinity]
385385
except KeyError:
@@ -389,8 +389,6 @@ def construct_from_generators_indices(generators, filtration, base_ring, check):
389389
return FilteredVectorSpace_class(base_ring, dim, generators, filtration, check=check)
390390

391391

392-
393-
394392
class FilteredVectorSpace_class(FreeModule_ambient_field):
395393

396394
def __init__(self, base_ring, dim, generators, filtration, check=True):
@@ -734,7 +732,7 @@ def graded(self, d):
734732
Basis matrix:
735733
[1 1]
736734
"""
737-
return self.get_degree(d).quotient(self.get_degree(d+1))
735+
return self.get_degree(d).quotient(self.get_degree(d + 1))
738736

739737
def presentation(self):
740738
"""
@@ -760,7 +758,7 @@ def presentation(self):
760758
generators.update(V.echelonized_basis())
761759
generators = tuple(sorted(generators))
762760

763-
filtration = dict()
761+
filtration = {}
764762
for d, V in filt:
765763
indices = [ZZ(generators.index(v)) for v in V.echelonized_basis()]
766764
filtration[d] = tuple(indices)
@@ -770,6 +768,8 @@ def _repr_field_name(self):
770768
"""
771769
Return an abbreviated field name as string
772770
771+
.. NOTE: This should rather be a method of fields and rings.
772+
773773
RAISES:
774774
775775
``NotImplementedError``: The field does not have an
@@ -1001,15 +1001,15 @@ def direct_sum(self, other):
10011001
self_gens, self_filt = self.presentation()
10021002
other_gens, other_filt = other.presentation()
10031003
generators = \
1004-
[ list(v) + [base_ring.zero()]*other.dimension() for v in self_gens ] + \
1005-
[ [base_ring.zero()]*self.dimension() + list(v) for v in other_gens ]
1004+
[list(v) + [base_ring.zero()] * other.dimension() for v in self_gens] + \
1005+
[[base_ring.zero()] * self.dimension() + list(v) for v in other_gens]
10061006

10071007
# construct the filtration dictionary
10081008
def join_indices(self_indices, other_indices):
10091009
self_indices = tuple(self_indices)
10101010
other_indices = tuple(i + len(self_gens) for i in other_indices)
10111011
return self_indices + other_indices
1012-
filtration = dict()
1012+
filtration = {}
10131013
self_indices = set()
10141014
other_indices = set()
10151015
degrees = list(self_filt) + list(other_filt)
@@ -1071,7 +1071,7 @@ def tensor_product(self, other):
10711071
W_coll = VectorCollection(W_generators, base_ring, W.dimension())
10721072
T = TensorOperation([V_coll, W_coll], 'product')
10731073

1074-
filtration = dict()
1074+
filtration = {}
10751075
for V_deg in V.support():
10761076
for W_deg in W.support():
10771077
deg = V_deg + W_deg
@@ -1112,7 +1112,7 @@ def _power_operation(self, n, operation):
11121112
T = TensorOperation([V] * n, operation)
11131113

11141114
iters = [self.support()] * n
1115-
filtration = dict()
1115+
filtration = {}
11161116
from sage.categories.cartesian_product import cartesian_product
11171117
for degrees in cartesian_product(iters):
11181118
deg = sum(degrees)
@@ -1124,7 +1124,6 @@ def _power_operation(self, n, operation):
11241124
filtration[deg] = filt_deg
11251125
return FilteredVectorSpace(T.vectors(), filtration, base_ring=self.base_ring())
11261126

1127-
11281127
def exterior_power(self, n):
11291128
"""
11301129
Return the `n`-th graded exterior power.
@@ -1204,7 +1203,7 @@ def dual(self):
12041203
sage: F.dual().support()
12051204
(-2, 0)
12061205
"""
1207-
filtration = dict()
1206+
filtration = {}
12081207
prev_deg = minus_infinity
12091208
for deg, V in self._filt[1:]:
12101209
filtration[-prev_deg] = V.complement().echelonized_basis()
@@ -1226,7 +1225,7 @@ def shift(self, deg):
12261225
(-5, -3)
12271226
"""
12281227
generators, filtration = self.presentation()
1229-
shifted = dict()
1228+
shifted = {}
12301229
for d, indices in filtration.items():
12311230
shifted[d + deg] = indices
12321231
return FilteredVectorSpace(generators, shifted, base_ring=self.base_ring())
@@ -1269,7 +1268,7 @@ def random_deformation(self, epsilon=None):
12691268
R = self.base_ring()
12701269
if epsilon is None:
12711270
epsilon = R.one()
1272-
filtration = dict()
1271+
filtration = {}
12731272
for deg, filt in self._filt[1:]:
12741273
generators = [v + epsilon * random_vector(R, self.rank())
12751274
for v in filt.echelonized_basis()]

src/sage/modules/free_module.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4611,7 +4611,7 @@ def span_of_basis(self, basis, base_ring=None, check=True, already_echelonized=F
46114611
M = self.change_ring(base_ring)
46124612
except TypeError:
46134613
raise ValueError("Argument base_ring (= %s) is not compatible with the base field (= %s)." % (
4614-
base_ring, self.base_field() ))
4614+
base_ring, self.base_field()))
46154615
try:
46164616
return M.span_of_basis(basis)
46174617
except TypeError:
@@ -5653,7 +5653,7 @@ def basis(self):
56535653
"""
56545654
try:
56555655
return self.__basis
5656-
except AttributeError:
5656+
except AttributeError:
56575657
ZERO = self(0)
56585658
one = self.coordinate_ring().one()
56595659
w = []
@@ -6600,7 +6600,7 @@ def _echelon_matrix_richcmp(self, other, op):
66006600
lx = self.ambient_vector_space()
66016601
rx = other.ambient_vector_space()
66026602
if lx != rx:
6603-
return lx._echelon_matrix_richcmp( rx, op)
6603+
return lx._echelon_matrix_richcmp(rx, op)
66046604

66056605
lx = self.dimension()
66066606
rx = other.dimension()

src/sage/modules/free_quadratic_module_integer_symmetric.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def IntegralLatticeDirectSum(Lattices, return_embeddings=False):
349349
basis = [matrix.block(1, 3, [matrix.zero(dims[i], sum_degree[i]),
350350
Lattices[i].basis_matrix(),
351351
matrix.zero(dims[i], sum_degree[-1] - sum_degree[i+1])
352-
]) for i in range(N)]
352+
]) for i in range(N)]
353353
basis_matrix = matrix.block(N, 1, basis)
354354
ipm = ambient.inner_product_matrix()
355355
direct_sum = FreeQuadraticModule_integer_symmetric(ambient=ambient,
@@ -363,6 +363,7 @@ def IntegralLatticeDirectSum(Lattices, return_embeddings=False):
363363
for i in range(N)]
364364
return [direct_sum, phi]
365365

366+
366367
def IntegralLatticeGluing(Lattices, glue, return_embeddings=False):
367368
r"""
368369
Return an overlattice of the direct sum as defined by ``glue``.
@@ -1092,7 +1093,7 @@ def maximal_overlattice(self, p=None):
10921093
for t in D:
10931094
if t != 0 and t.q() == 0:
10941095
break
1095-
if t.q() != 0 :
1096+
if t.q() != 0:
10961097
# no isotropic vector left
10971098
break
10981099
L = L.overlattice([t.lift()])

src/sage/modules/matrix_morphism.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ def is_identity(self):
12361236
# testing for the identity matrix will only work for
12371237
# endomorphisms which have the same basis for domain and codomain
12381238
# so we test equality on a basis, which is sufficient
1239-
return all( self(u) == u for u in self.domain().basis() )
1239+
return all(self(u) == u for u in self.domain().basis())
12401240

12411241
def is_zero(self):
12421242
r"""
@@ -1369,7 +1369,7 @@ def is_equal_function(self, other):
13691369
if self.codomain() != other.codomain():
13701370
return False
13711371
# check agreement on any basis of the domain
1372-
return all( self(u) == other(u) for u in self.domain().basis() )
1372+
return all(self(u) == other(u) for u in self.domain().basis())
13731373

13741374
def restrict_domain(self, sub):
13751375
"""

src/sage/modules/torsion_quadratic_module.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def _mul_(self, other):
144144
1/4
145145
"""
146146
value_module = self.parent().value_module()
147-
return value_module( self.lift().inner_product(other.lift()) )
147+
return value_module(self.lift().inner_product(other.lift()))
148148

149149
inner_product = _mul_
150150
b = _mul_
@@ -296,7 +296,6 @@ def __init__(self, V, W, gens, modulus, modulus_qf):
296296
self._modulus = modulus
297297
self._modulus_qf = modulus_qf
298298

299-
300299
def _repr_(self):
301300
r"""
302301
Return a string representation of ``self``.
@@ -313,10 +312,10 @@ def _repr_(self):
313312
[0 0 0]
314313
[0 0 0]
315314
"""
316-
return ( "Finite quadratic module over %s with invariants %s\n"
317-
% (self.base_ring(), self.invariants()) +
318-
"Gram matrix of the quadratic form with values in %r:\n%r"
319-
% (self.value_module_qf(), self.gram_matrix_quadratic()))
315+
return ("Finite quadratic module over %s with invariants %s\n"
316+
% (self.base_ring(), self.invariants()) +
317+
"Gram matrix of the quadratic form with values in %r:\n%r"
318+
% (self.value_module_qf(), self.gram_matrix_quadratic()))
320319

321320
def _module_constructor(self, V, W, check=False):
322321
r"""

src/sage/modules/with_basis/cell_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def _acted_upon_(self, scalar, self_on_left=False):
357357
# Temporary needed by coercion (see Polynomial/FractionField tests).
358358
if not P._algebra.has_coerce_map_from(scalar.parent()):
359359
return None
360-
scalar = P._algebra( scalar )
360+
scalar = P._algebra(scalar)
361361

362362
if self_on_left:
363363
raise NotImplementedError

src/sage/modules/with_basis/morphism.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -403,18 +403,19 @@ def __call__(self, *args):
403403
mc = x.monomial_coefficients(copy=False)
404404
if self._is_module_with_basis_over_same_base_ring:
405405
return self.codomain().linear_combination(
406-
(self._on_basis(*(before+(index,)+after)), coeff )
407-
for (index, coeff) in mc.items())
406+
(self._on_basis(*(before + (index,) + after)), coeff)
407+
for (index, coeff) in mc.items())
408408
else:
409-
return sum((coeff * self._on_basis(*(before+(index,)+after))
410-
for (index, coeff) in mc.items()), self._zero)
409+
return sum((coeff * self._on_basis(*(before + (index,) + after))
410+
for (index, coeff) in mc.items()), self._zero)
411411

412412
# As per the specs of Map, we should in fact implement _call_.
413413
# However we currently need to abuse Map.__call__ (which strict
414414
# type checking) for multi-parameter module morphisms
415415
# To be cleaned up
416416
_call_ = __call__
417417

418+
418419
class TriangularModuleMorphism(ModuleMorphism):
419420
r"""
420421
An abstract class for triangular module morphisms
@@ -683,10 +684,9 @@ def __init__(self, triangular="upper", unitriangular=False,
683684
self._inverse = inverse
684685

685686
if inverse_on_support == "compute":
686-
inverse_on_support = {
687-
self._dominant_item(on_basis(i))[0] : i
688-
for i in self.domain().basis().keys()
689-
}.get
687+
inverse_on_support = {self._dominant_item(on_basis(i))[0]: i
688+
for i in self.domain().basis().keys()
689+
}.get
690690

691691
self._inverse_on_support = inverse_on_support
692692

@@ -883,7 +883,7 @@ def _invert_on_basis(self, i):
883883
sage: phi._invert_on_basis(2)
884884
B[2] - B[3]
885885
"""
886-
return self.preimage( self.codomain().monomial(i) )
886+
return self.preimage(self.codomain().monomial(i))
887887

888888
def preimage(self, f):
889889
r"""
@@ -1351,13 +1351,13 @@ def __init__(self, domain, matrix, codomain=None, category=None, side="left"):
13511351
matrix = matrix.transpose()
13521352
if matrix.nrows() != len(indices):
13531353
raise ValueError("The dimension of the matrix (%s) does not match with the dimension of the domain (%s)"
1354-
%(matrix.nrows(), len(indices)))
1354+
% (matrix.nrows(), len(indices)))
13551355
if matrix.ncols() != codomain.dimension():
13561356
raise ValueError("The dimension of the matrix (%s) does not match with the dimension of the codomain (%s)"
1357-
%(matrix.ncols(), codomain.dimension()))
1357+
% (matrix.ncols(), codomain.dimension()))
13581358
self._matrix = matrix
1359-
d = { xt: codomain.from_vector(matrix.row(rank_domain(xt)))
1360-
for xt in domain.basis().keys() }
1359+
d = {xt: codomain.from_vector(matrix.row(rank_domain(xt)))
1360+
for xt in domain.basis().keys()}
13611361

13621362
ModuleMorphismByLinearity.__init__(self, on_basis=d.__getitem__,
13631363
domain=domain, codomain=codomain,

0 commit comments

Comments
 (0)