Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some pep8 fixes in homology/ #35908

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/sage/homology/algebraic_topological_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .chain_homotopy import ChainContraction
from sage.rings.rational_field import QQ


def algebraic_topological_model(K, base_ring=None):
r"""
Algebraic topological model for cell complex ``K``
Expand Down Expand Up @@ -336,6 +337,7 @@ def algebraic_topological_model(K, base_ring=None):
phi = ChainContraction(phi_data, pi, iota)
return phi, M


def algebraic_topological_model_delta_complex(K, base_ring=None):
r"""
Algebraic topological model for cell complex ``K``
Expand Down Expand Up @@ -486,7 +488,7 @@ def conditionally_sparse(m):
iota_cols = {}
pi_cols_old = pi_cols
pi_cols = []
phi_old = MatrixSpace(base_ring, rank, old_rank, sparse=(base_ring==QQ)).zero()
phi_old = MatrixSpace(base_ring, rank, old_rank, sparse=(base_ring == QQ)).zero()
phi_old_cols = phi_old.columns()
phi_old = conditionally_sparse(phi_old)
to_be_deleted = []
Expand Down Expand Up @@ -543,8 +545,8 @@ def conditionally_sparse(m):
# The matrices involved have many zero entries. For
# such matrices, using sparse matrices is faster over
# the rationals, slower over finite fields.
phi_old = matrix(base_ring, phi_old_cols, sparse=(base_ring==QQ)).transpose()
keep = vector(base_ring, pi_nrows, {i:1 for i in range(pi_nrows)
phi_old = matrix(base_ring, phi_old_cols, sparse=(base_ring == QQ)).transpose()
keep = vector(base_ring, pi_nrows, {i: 1 for i in range(pi_nrows)
if i not in to_be_deleted})
cols = [v.pairwise_product(keep) for v in pi_cols_old]
pi_old = MS_pi_t.matrix(cols).transpose()
Expand Down
2 changes: 1 addition & 1 deletion src/sage/homology/cell_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
import sage.topology.cell_complex

GenericCellComplex = deprecated_function_alias(31925,
sage.topology.cell_complex.GenericCellComplex)
sage.topology.cell_complex.GenericCellComplex)
41 changes: 21 additions & 20 deletions src/sage/homology/chain_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
from sage.misc.superseded import deprecation
from sage.rings.fast_arith import prime_range
from sage.homology.homology_group import HomologyGroup
from sage.misc.persist import register_unpickle_override


def _latex_module(R, m):
Expand Down Expand Up @@ -248,7 +249,7 @@ def ChainComplex(data=None, base_ring=None, grading_group=None,
data_dict = {}
elif isinstance(data, dict): # data is dictionary
data_dict = data
else: # data is list/tuple/iterable
else: # data is list/tuple/iterable
data_matrices = [x for x in data if isinstance(x, Matrix)]
if degree != 1:
raise ValueError('degree must be +1 if the data argument is a list or tuple')
Expand Down Expand Up @@ -667,8 +668,8 @@ def __init__(self, grading_group, degree_of_differential, base_ring, differentia
sage: TestSuite(C).run()
"""
if any(d.base_ring() != base_ring or not d.is_immutable() or
(d.ncols(), d.nrows()) == (0, 0)
for d in differentials.values()):
(d.ncols(), d.nrows()) == (0, 0)
for d in differentials.values()):
raise ValueError('invalid differentials')
if degree_of_differential.parent() is not grading_group:
raise ValueError('the degree_of_differential.parent() must be grading_group')
Expand Down Expand Up @@ -1349,15 +1350,15 @@ def change_ring(X):
d_out_nullity = d_out.ncols() - d_out_rank

if d_in.is_zero():
if generators: #Include the generators of the nullspace
return [(HomologyGroup(1, base_ring), self({deg:gen}))
if generators: # Include the generators of the nullspace
return [(HomologyGroup(1, base_ring), self({deg: gen}))
for gen in d_out.right_kernel().basis()]
else:
return HomologyGroup(d_out_nullity, base_ring)

if generators:
orders, gens = self._homology_generators_snf(d_in, d_out, d_out_rank)
answer = [(HomologyGroup(1, base_ring, [order]), self({deg:gen}))
answer = [(HomologyGroup(1, base_ring, [order]), self({deg: gen}))
for order, gen in zip(orders, gens)]
else:
if base_ring.is_field():
Expand Down Expand Up @@ -1552,7 +1553,7 @@ def torsion_list(self, max_prime, min_prime=2):
for i in diff_dict:
if diff_dict[i] != 0:
differences.append(i)
answer.append((p,differences))
answer.append((p, differences))
return answer

def _Hom_(self, other, category=None):
Expand Down Expand Up @@ -1690,7 +1691,7 @@ def _chomp_repr_(self):
deprecation(33777, "the CHomP interface is deprecated; hence so is this function")
deg = self.degree_of_differential()
if (self.grading_group() != ZZ or
(deg != 1 and deg != -1)):
(deg != 1 and deg != -1)):
raise ValueError('CHomP only works on Z-graded chain complexes with '
'differential of degree 1 or -1')
base_ring = self.base_ring()
Expand All @@ -1702,7 +1703,7 @@ def _chomp_repr_(self):
diffs = self._flip_().differential()

if len(diffs) == 0:
diffs = {0: matrix(ZZ, 0,0)}
diffs = {0: matrix(ZZ, 0, 0)}

maxdim = max(diffs)
mindim = min(diffs)
Expand Down Expand Up @@ -2017,7 +2018,7 @@ def cartesian_product(self, *factors, **kwds):
diffs = [D.differential() for D in factors]
keys = reduce(lambda X, d: X.union(d.keys()), diffs, set())
ret = {k: matrix.block_diagonal([d.get(k, zero) for d in diffs],
subdivide=subdivide)
subdivide=subdivide)
for k in keys}
return ChainComplex(ret, degree_of_differential=deg_diff,
grading_group=self._grading_group)
Expand Down Expand Up @@ -2182,32 +2183,32 @@ def scalar(a):
# Our choice for tensor products will be x # y = x1 * y + x2 * y + ...

# Generate the data for the differential
for a,r in deg:
for b,s in degD:
for a, r in deg:
for b, s in degD:
rp = d[a].nrows()
sp = dD[b].nrows()
if a+b not in diff:
diff[a+b] = {}
mor = diff[a+b]
cur = {}
cur[(a+deg_diff,b)] = []
cur[(a,b+deg_diff)] = []
cur[(a+deg_diff, b)] = []
cur[(a, b+deg_diff)] = []

for i in range(r):
for j in range(s):
# \partial x_i \otimes y_j
vec = [zero]*(rp*s)
for k,val in enumerate(d[a].column(i)):
for k, val in enumerate(d[a].column(i)):
vec[s*k+j] += val
cur[(a+deg_diff,b)].append(vec)
cur[(a+deg_diff, b)].append(vec)

# (-1)^a x_i \otimes \partial y_j
vec = [zero]*(r*sp)
for k,val in enumerate(dD[b].column(j)):
for k, val in enumerate(dD[b].column(j)):
vec[sp*i+k] += scalar(a) * val
cur[(a,b+deg_diff)].append(vec)
cur[(a, b+deg_diff)].append(vec)

mor[a,b] = cur
mor[a, b] = cur

# Parse the data into matrices
to_delete = []
Expand Down Expand Up @@ -2250,5 +2251,5 @@ def scalar(a):

return ret

from sage.misc.persist import register_unpickle_override

register_unpickle_override('sage.homology.chain_complex', 'ChainComplex', ChainComplex_class)
4 changes: 2 additions & 2 deletions src/sage/homology/chain_complex_homspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2009 D. Benjamin Antieau <d.ben.antieau@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
Expand All @@ -100,7 +100,7 @@
#
# https://www.gnu.org/licenses/
#
#*****************************************************************************
# ****************************************************************************

import sage.categories.homset
from sage.homology.chain_complex_morphism import ChainComplexMorphism
Expand Down
42 changes: 21 additions & 21 deletions src/sage/homology/chain_complex_morphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
[0 0 0]}
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2009 D. Benjamin Antieau <d.ben.antieau@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
Expand All @@ -49,7 +49,7 @@
#
# https://www.gnu.org/licenses/
#
#*****************************************************************************
# ****************************************************************************

from sage.matrix.constructor import block_diagonal_matrix, zero_matrix
from sage.categories.morphism import Morphism
Expand Down Expand Up @@ -165,8 +165,8 @@ def __init__(self, matrices, C, D, check=True):
# Check sizes of matrices.
for i in matrices:
if (matrices[i].nrows() != D.free_module_rank(i) or
matrices[i].ncols() != C.free_module_rank(i)):
raise ValueError('matrix in degree {} is not the right size'.format(i))
matrices[i].ncols() != C.free_module_rank(i)):
raise ValueError(f'matrix in degree {i} is not the right size')
# Check commutativity.
for i in degrees:
if i - d not in degrees:
Expand All @@ -187,7 +187,7 @@ def __init__(self, matrices, C, D, check=True):
# Use immutable matrices because they're hashable.
m.set_immutable()
self._matrix_dictionary[i] = m
Morphism.__init__(self, Hom(C,D, ChainComplexes(C.base_ring())))
Morphism.__init__(self, Hom(C, D, ChainComplexes(C.base_ring())))

def in_degree(self, n):
"""
Expand Down Expand Up @@ -324,7 +324,7 @@ def __neg__(self):
f[i] = -self._matrix_dictionary[i]
return ChainComplexMorphism(f, self.domain(), self.codomain())

def __add__(self,x):
def __add__(self, x):
"""
Return ``self + x``.

Expand Down Expand Up @@ -431,21 +431,21 @@ def __mul__(self, x):
sage: g._matrix_dictionary
{1: [], 2: []}
"""
if not isinstance(x,ChainComplexMorphism) or self.domain() != x.codomain():
if not isinstance(x, ChainComplexMorphism) or self.domain() != x.codomain():
try:
y = self.domain().base_ring()(x)
except TypeError:
raise TypeError("multiplication is not defined")
f = dict()
for i in self._matrix_dictionary:
f[i] = self._matrix_dictionary[i] * y
return ChainComplexMorphism(f,self.domain(),self.codomain())
f = dict()
return ChainComplexMorphism(f, self.domain(), self.codomain())
f = {}
for i in self._matrix_dictionary:
f[i] = self._matrix_dictionary[i]*x.in_degree(i)
return ChainComplexMorphism(f,x.domain(),self.codomain())
return ChainComplexMorphism(f, x.domain(), self.codomain())

def __rmul__(self,x):
def __rmul__(self, x):
"""
Return ``x * self`` if ``x`` is an element of the base ring.

Expand All @@ -465,11 +465,11 @@ def __rmul__(self,x):
except TypeError:
raise TypeError("multiplication is not defined")
f = dict()
for i in self._matrix_dictionary.keys():
for i in self._matrix_dictionary:
f[i] = y * self._matrix_dictionary[i]
return ChainComplexMorphism(f,self.domain(),self.codomain())
return ChainComplexMorphism(f, self.domain(), self.codomain())

def __sub__(self,x):
def __sub__(self, x):
"""
Return ``self - x``.

Expand Down Expand Up @@ -498,7 +498,7 @@ def __sub__(self,x):
"""
return self + (-x)

def __eq__(self,x):
def __eq__(self, x):
"""
Return ``True`` if and only if ``self == x``.

Expand All @@ -518,12 +518,12 @@ def __eq__(self,x):
sage: x == y # optional - sage.graphs
True
"""
return isinstance(x,ChainComplexMorphism) \
and self.codomain() == x.codomain() \
and self.domain() == x.domain() \
and self._matrix_dictionary == x._matrix_dictionary
return isinstance(x, ChainComplexMorphism) \
and self.codomain() == x.codomain() \
and self.domain() == x.domain() \
and self._matrix_dictionary == x._matrix_dictionary

def is_identity(self):
def is_identity(self) -> bool:
"""
Return ``True`` if this is the identity map.

Expand All @@ -538,7 +538,7 @@ def is_identity(self):
"""
return self.to_matrix().is_one()

def is_surjective(self):
def is_surjective(self) -> bool:
"""
Return ``True`` if this map is surjective.

Expand Down
1 change: 1 addition & 0 deletions src/sage/homology/chain_homotopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def _repr_(self):
s += '\n and {}'.format('\n '.join(self._g._repr_().split('\n')))
return s


class ChainContraction(ChainHomotopy):
r"""
A chain contraction.
Expand Down
16 changes: 8 additions & 8 deletions src/sage/homology/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
complex.
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2016 Volker Braun <vbraun.name@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************


from sage.combinat.free_module import CombinatorialFreeModule
Expand Down Expand Up @@ -672,9 +672,9 @@ def cup_product(self, cochain):
accumulator = codomain.zero()
for cell in codomain.indices():
for (coeff, left_cell, right_cell) in cx.alexander_whitney(cell, left_deg):
if not coeff:
continue
left = left_chains(left_cell)
right = right_chains(right_cell)
accumulator += codomain(cell) * coeff * self.eval(left) * cochain.eval(right)
if not coeff:
continue
left = left_chains(left_cell)
right = right_chains(right_cell)
accumulator += codomain(cell) * coeff * self.eval(left) * cochain.eval(right)
return accumulator
6 changes: 3 additions & 3 deletions src/sage/homology/cubical_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

Cube = deprecated_function_alias(31925, sage.topology.cubical_complex.Cube)
CubicalComplex = deprecated_function_alias(31925,
sage.topology.cubical_complex.CubicalComplex)
sage.topology.cubical_complex.CubicalComplex)
CubicalComplexExamples = deprecated_function_alias(31925,
sage.topology.cubical_complex.CubicalComplexExamples)
sage.topology.cubical_complex.CubicalComplexExamples)
cubical_complexes = deprecated_function_alias(31925,
sage.topology.cubical_complex.cubical_complexes)
sage.topology.cubical_complex.cubical_complexes)
6 changes: 3 additions & 3 deletions src/sage/homology/delta_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import sage.topology.delta_complex

DeltaComplex = deprecated_function_alias(31925,
sage.topology.delta_complex.DeltaComplex)
sage.topology.delta_complex.DeltaComplex)
DeltaComplexExamples = deprecated_function_alias(31925,
sage.topology.delta_complex.DeltaComplexExamples)
sage.topology.delta_complex.DeltaComplexExamples)
delta_complexes = deprecated_function_alias(31925,
sage.topology.delta_complex.delta_complexes)
sage.topology.delta_complex.delta_complexes)
Loading