Skip to content

Commit f7dafc6

Browse files
committed
some cython-lint fixes in matroids/
1 parent 97b45d8 commit f7dafc6

File tree

9 files changed

+37
-49
lines changed

9 files changed

+37
-49
lines changed

src/sage/matroids/basis_exchange_matroid.pyx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ Methods
4141
from .matroid cimport Matroid
4242
from .set_system cimport SetSystem
4343

44-
from copy import copy
45-
from itertools import combinations, permutations
46-
4744
from sage.data_structures.bitset_base cimport *
4845

4946
cdef class BasisExchangeMatroid(Matroid):
@@ -1851,8 +1848,7 @@ cdef class BasisExchangeMatroid(Matroid):
18511848
return NSC
18521849
bitset_clear(self._input)
18531850
bitset_set_first_n(self._input, self._matroid_rank)
1854-
cdef long e, f, corank
1855-
corank = self._groundset_size - self._matroid_rank
1851+
cdef long e, f
18561852
repeat = True
18571853
while repeat:
18581854
if self.__is_independent(self._input):

src/sage/matroids/basis_matroid.pyx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ from .basis_exchange_matroid cimport BasisExchangeMatroid
7979
from .set_system cimport SetSystem
8080
from cpython.object cimport Py_EQ, Py_NE
8181

82-
from sage.arith.misc import binomial
83-
84-
from itertools import permutations, combinations
82+
from itertools import combinations
8583

8684
# class of general matroids, represented by their list of bases
8785

@@ -684,7 +682,6 @@ cdef class BasisMatroid(BasisExchangeMatroid):
684682
if self._bases_invariant_var is not None:
685683
return self._bases_invariant_var
686684
cdef long i, j
687-
cdef bitset_t bb_comp
688685
cdef list bc
689686
cdef dict bi
690687
bc = [0 for i in xrange(len(self))]
@@ -1183,7 +1180,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
11831180
N.rename(getattr(self, '__custom_name'))
11841181
return N
11851182

1186-
def __deepcopy__(self, memo={}):
1183+
def __deepcopy__(self, memo=None):
11871184
"""
11881185
Create a deep copy.
11891186
@@ -1201,6 +1198,8 @@ cdef class BasisMatroid(BasisExchangeMatroid):
12011198
sage: M.groundset() is N.groundset()
12021199
False
12031200
"""
1201+
if memo is None:
1202+
memo = {}
12041203
N = BasisMatroid(M=self)
12051204
N.rename(getattr(self, '__custom_name'))
12061205
return N

src/sage/matroids/circuit_closures_matroid.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ cdef class CircuitClosuresMatroid(Matroid):
517517
N.rename(getattr(self, '__custom_name'))
518518
return N
519519

520-
def __deepcopy__(self, memo={}):
520+
def __deepcopy__(self, memo=None):
521521
"""
522522
Create a deep copy.
523523
@@ -534,6 +534,8 @@ cdef class CircuitClosuresMatroid(Matroid):
534534
sage: M.groundset() is N.groundset()
535535
False
536536
"""
537+
if memo is None:
538+
memo = {}
537539
from copy import deepcopy
538540
# Since matroids are immutable, N cannot reference itself in correct code, so no need to worry about the recursion.
539541
N = CircuitClosuresMatroid(groundset=deepcopy(self._groundset, memo), circuit_closures=deepcopy(self._circuit_closures, memo))

src/sage/matroids/extension.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Methods
3131

3232
from sage.data_structures.bitset_base cimport *
3333
from .basis_matroid cimport BasisMatroid
34-
from sage.arith.misc import binomial
3534

3635

3736
cdef class CutNode:

src/sage/matroids/lean_matrix.pyx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ from cysignals.signals cimport sig_on, sig_off
3838
from sage.data_structures.bitset_base cimport *
3939
from sage.matrix.matrix2 cimport Matrix
4040
from sage.rings.integer_ring import ZZ
41-
from sage.rings.finite_rings.finite_field_constructor import FiniteField
4241
from sage.rings.finite_rings.finite_field_constructor import FiniteField as GF
4342
from sage.rings.rational_field import QQ
4443
from sage.rings.integer cimport Integer
@@ -303,7 +302,7 @@ cdef class LeanMatrix:
303302
This is different from what matroid theorists tend to call a
304303
pivot, as it does not involve a column exchange!
305304
"""
306-
cdef long i, j
305+
cdef long i
307306
self.rescale_row_c(x, self.get_unsafe(x, y) ** (-1), 0)
308307
for i from 0 <= i < self._nrows:
309308
s = self.get_unsafe(i, y)
@@ -491,7 +490,7 @@ cdef class LeanMatrix:
491490
"""
492491
return self.copy()
493492

494-
def __deepcopy__(self, memo={}):
493+
def __deepcopy__(self, memo=None):
495494
"""
496495
Return a deep copy of ``self``.
497496
@@ -782,7 +781,6 @@ cdef class GenericMatrix(LeanMatrix):
782781
Warning: assumes ``M`` is a GenericMatrix instance!
783782
"""
784783
cdef GenericMatrix A
785-
cdef long i, j
786784
A = GenericMatrix(0, 0, ring=self._base_ring)
787785
A._entries = self._entries + ((<GenericMatrix>M)._entries)
788786
A._nrows = self._nrows + M.nrows()
@@ -1145,7 +1143,7 @@ cdef class BinaryMatrix(LeanMatrix):
11451143
"""
11461144
Return the matrix obtained by prepending an identity matrix. Special case of ``augment``.
11471145
"""
1148-
cdef long i, j
1146+
cdef long i
11491147
cdef BinaryMatrix A = BinaryMatrix(self._nrows, self._ncols + self._nrows)
11501148
for i from 0 <= i < self._nrows:
11511149
bitset_lshift(A._M[i], self._M[i], self._nrows)
@@ -1214,7 +1212,7 @@ cdef class BinaryMatrix(LeanMatrix):
12141212
This is different from what matroid theorists tend to call a
12151213
pivot, as it does not involve a column exchange!
12161214
"""
1217-
cdef long i, j
1215+
cdef long i
12181216
for i from 0 <= i < self._nrows:
12191217
if bitset_in(self._M[i], y) and i != x:
12201218
bitset_symmetric_difference(self._M[i], self._M[i], self._M[x])
@@ -1423,7 +1421,7 @@ cdef class BinaryMatrix(LeanMatrix):
14231421
Helper method for isomorphism test.
14241422
"""
14251423
cdef BinaryMatrix Q
1426-
cdef long i, r
1424+
cdef long i
14271425
Q = BinaryMatrix(self._nrows + 1, self._ncols)
14281426
for i from 0 <= i < self._nrows:
14291427
bitset_copy(Q._M[i], self._M[i])
@@ -1795,7 +1793,7 @@ cdef class TernaryMatrix(LeanMatrix):
17951793
17961794
Special case of ``augment``.
17971795
"""
1798-
cdef long i, j
1796+
cdef long i
17991797
cdef TernaryMatrix A = TernaryMatrix(self._nrows, self._ncols + self._nrows)
18001798
for i from 0 <= i < self._nrows:
18011799
bitset_lshift(A._M0[i], self._M0[i], self._nrows)
@@ -1935,7 +1933,7 @@ cdef class TernaryMatrix(LeanMatrix):
19351933
This is different from what matroid theorists tend to call a
19361934
pivot, as it does not involve a column exchange!
19371935
"""
1938-
cdef long i, j
1936+
cdef long i
19391937
if self._is_negative(x, y):
19401938
self._row_negate(x)
19411939
for i from 0 <= i < self._nrows:
@@ -2396,7 +2394,7 @@ cdef class QuaternaryMatrix(LeanMatrix):
23962394
Return the matrix obtained by prepending an identity matrix. Special
23972395
case of ``augment``.
23982396
"""
2399-
cdef long i, j
2397+
cdef long i
24002398
cdef QuaternaryMatrix A = QuaternaryMatrix(self._nrows, self._ncols + self._nrows, ring=self._gf4)
24012399
for i from 0 <= i < self._nrows:
24022400
bitset_lshift(A._M0[i], self._M0[i], self._nrows)
@@ -2522,7 +2520,7 @@ cdef class QuaternaryMatrix(LeanMatrix):
25222520
This is different from what matroid theorists tend to call a
25232521
pivot, as it does not involve a column exchange!
25242522
"""
2525-
cdef long i, j
2523+
cdef long i
25262524
self._row_div(x, self.get(x, y))
25272525
for i from 0 <= i < self._nrows:
25282526
if self.is_nonzero(i, y) and i != x:
@@ -3047,7 +3045,7 @@ cdef class PlusMinusOneMatrix(LeanMatrix):
30473045
This is different from what matroid theorists tend to call a
30483046
pivot, as it does not involve a column exchange!
30493047
"""
3050-
cdef long i, j
3048+
cdef long i
30513049
cdef int a, s
30523050
a = self.get(x, y) # 1 or -1, so inverse is equal to itself
30533051
self.rescale_row_c(x, a, 0)
@@ -3532,7 +3530,7 @@ cdef class RationalMatrix(LeanMatrix):
35323530
This is different from what matroid theorists tend to call a
35333531
pivot, as it does not involve a column exchange!
35343532
"""
3535-
cdef long i, j
3533+
cdef long i
35363534
cdef mpq_t t
35373535
mpq_init(t)
35383536
mpq_inv(t, self._entries[self.index(x, y)])

src/sage/matroids/linear_matroid.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ cdef class LinearMatroid(BasisExchangeMatroid):
2222
cpdef representation_vectors(self)
2323
cpdef LeanMatrix _basic_representation(self, B=*)
2424
cpdef LeanMatrix _reduced_representation(self, B=*)
25-
26-
25+
26+
2727
cpdef bint _is_field_isomorphism(self, LinearMatroid other, morphism)
2828
cpdef is_field_equivalent(self, other)
2929
cpdef is_field_isomorphism(self, other, morphism)

src/sage/matroids/linear_matroid.pyx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,18 @@ Methods
112112
# ****************************************************************************
113113

114114
from copy import copy, deepcopy
115-
from itertools import combinations, product
115+
from itertools import product
116116

117117
from cpython.object cimport Py_EQ, Py_NE
118118

119119
from sage.data_structures.bitset_base cimport *
120-
from sage.graphs.graph import Graph
121-
from sage.graphs.spanning_tree import kruskal
122120
from sage.matrix.constructor import matrix
123121
from sage.matrix.matrix2 cimport Matrix
124122
from sage.matroids.basis_exchange_matroid cimport BasisExchangeMatroid
125123
from sage.matroids.lean_matrix cimport (LeanMatrix, GenericMatrix, BinaryMatrix,
126124
TernaryMatrix, QuaternaryMatrix, PlusMinusOneMatrix,
127-
RationalMatrix, generic_identity)
125+
RationalMatrix)
128126
from sage.matroids.matroid cimport Matroid
129-
from sage.matroids.set_system cimport SetSystem
130127
from sage.matroids.utilities import newlabel, spanning_stars, spanning_forest, lift_cross_ratios
131128
from sage.rings.finite_rings.finite_field_constructor import FiniteField as GF
132129
from sage.rings.integer_ring import ZZ
@@ -168,15 +165,15 @@ cdef inline characteristic(LeanMatrix A):
168165
# cdef long c, p, row
169166
# for c in columns:
170167
# is_pivot = False
171-
# for row in xrange(r, A.nrows()):
168+
# for row in range(r, A.nrows()):
172169
# if A.get_unsafe(row, c) != 0:
173170
# is_pivot = True
174171
# p = row
175172
# break
176173
# if is_pivot:
177174
# A.swap_rows_c(p, r)
178175
# A.rescale_row_c(r, A.get_unsafe(r, c) ** (-1), 0)
179-
# for row in xrange(A.nrows()):
176+
# for row in range(A.nrows()):
180177
# if row != r and A.get_unsafe(row, c) != 0:
181178
# A.add_multiple_of_row_c(row, r, -A.get_unsafe(row, c), 0)
182179
# P.append(c)
@@ -276,7 +273,7 @@ cdef class LinearMatroid(BasisExchangeMatroid):
276273
"""
277274
basis = self._setup_internal_representation(matrix, reduced_matrix, ring, keep_initial_representation)
278275
if groundset is None:
279-
groundset = list(xrange(self._A.nrows() + self._A.ncols()))
276+
groundset = list(range(self._A.nrows() + self._A.ncols()))
280277
else:
281278
groundset = list(groundset)
282279
if len(groundset) != self._A.nrows() + self._A.ncols():
@@ -2089,7 +2086,7 @@ cdef class LinearMatroid(BasisExchangeMatroid):
20892086
[0 1 1 0 0 1 1]
20902087
[0 0 0 1 1 1 1]
20912088
"""
2092-
cdef long i, j
2089+
cdef long i
20932090
cdef LeanMatrix M
20942091
ext = []
20952092
if self._representation is None:
@@ -2139,7 +2136,7 @@ cdef class LinearMatroid(BasisExchangeMatroid):
21392136
[0 0 0 1 1 1 0]
21402137
[1 0 0 0 0 1 1]
21412138
"""
2142-
cdef long i, j
2139+
cdef long i
21432140
cdef LeanMatrix M
21442141
coext = []
21452142
if self._representation is None:
@@ -3344,7 +3341,7 @@ cdef class BinaryMatroid(LinearMatroid):
33443341
"""
33453342
if B is not None:
33463343
self._move_current_basis(B, set())
3347-
rows, cols = self._current_rows_cols()
3344+
_, cols = self._current_rows_cols()
33483345
return self._A.matrix_from_rows_and_columns(range(self.full_rank()), [self._idx[e] for e in cols])
33493346

33503347
# isomorphism
@@ -4417,7 +4414,7 @@ cdef class TernaryMatroid(LinearMatroid):
44174414
"""
44184415
if B is not None:
44194416
self._move_current_basis(B, set())
4420-
rows, cols = self._current_rows_cols()
4417+
_, cols = self._current_rows_cols()
44214418
return self._A.matrix_from_rows_and_columns(range(self.full_rank()), [self._idx[e] for e in cols])
44224419

44234420
# isomorphism
@@ -5314,7 +5311,7 @@ cdef class QuaternaryMatroid(LinearMatroid):
53145311
"""
53155312
if B is not None:
53165313
self._move_current_basis(B, set())
5317-
rows, cols = self._current_rows_cols()
5314+
_, cols = self._current_rows_cols()
53185315
return self._A.matrix_from_rows_and_columns(range(self.full_rank()), [self._idx[e] for e in cols])
53195316

53205317
cpdef _make_invariant(self):
@@ -6033,7 +6030,6 @@ cdef class RegularMatroid(LinearMatroid):
60336030
Digraph on 55 vertices
60346031
"""
60356032
# NEW VERSION, Uses Sage'S Graph Isomorphism
6036-
from sage.graphs.graph import Graph
60376033
from sage.graphs.digraph import DiGraph
60386034
if self._r_hypergraph is not None:
60396035
return (self._hypergraph_vertex_partition, self._hypergraph_tuples, self._r_hypergraph)
@@ -6042,7 +6038,7 @@ cdef class RegularMatroid(LinearMatroid):
60426038
B = {}
60436039
V = []
60446040
E = []
6045-
for i in xrange(P.nrows()):
6041+
for i in range(P.nrows()):
60466042
e = self._E[i]
60476043
w = P.get_unsafe(i, i)
60486044
if w != 0:

src/sage/matroids/matroid.pyx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,8 @@ from collections.abc import Iterable
335335

336336
from sage.structure.richcmp cimport rich_to_bool, richcmp
337337
from sage.structure.sage_object cimport SageObject
338-
from itertools import combinations, permutations, product
338+
from itertools import combinations, product
339339
from .set_system cimport SetSystem
340-
from sage.graphs.spanning_tree import kruskal
341340
from sage.graphs.graph import Graph
342341
from sage.matrix.constructor import matrix
343342

@@ -6305,7 +6304,7 @@ cdef class Matroid(SageObject):
63056304
cdef frozenset Ax, Bx
63066305

63076306
X = set(C)
6308-
e = X.pop()
6307+
_ = X.pop()
63096308
# cl(X) = cl(C), and to be a chord x must be spanned by C
63106309
for x in self._closure(X)-C:
63116310
Ax = self._circuit(X.union([x]))
@@ -7585,7 +7584,7 @@ cdef class Matroid(SageObject):
75857584
MIP.set_objective(sum([f[F] for F in FF]))
75867585
for N in NB:
75877586
MIP.add_constraint(sum([f[F] for F in FF if len(F.intersection(N)) > self.rank(F)]), min=1)
7588-
opt = MIP.solve(log=verbose)
7587+
_ = MIP.solve(log=verbose)
75897588

75907589
fsol = MIP.get_values(f, convert=bool, tolerance=integrality_tolerance)
75917590

src/sage/matroids/set_system.pyx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ cdef class SetSystem:
401401
Helper method for partition methods below.
402402
"""
403403
cdef dict C
404-
cdef long i, j, v, t0, t
404+
cdef long i, v, t0, t
405405
cdef bint split
406406

407407
C = {}
@@ -453,7 +453,6 @@ cdef class SetSystem:
453453
"""
454454
Helper method for partition methods below.
455455
"""
456-
S = {}
457456
if P is None:
458457
P = self.groundset_partition()
459458
if E is None:
@@ -561,7 +560,7 @@ cdef class SetSystem:
561560
partition elements is an invariant of the isomorphism class of the
562561
hypergraph.
563562
"""
564-
cdef long h, l
563+
cdef long h
565564
cdef list EP2, H
566565

567566
if P is None:

0 commit comments

Comments
 (0)