Skip to content

Commit 9d620dd

Browse files
author
Release Manager
committed
gh-39888: some cleanup in matrix1.pyx mostly cosmetic code formatting (pycodestyle suggestions) ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #39888 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents 6c65c1c + d5a4649 commit 9d620dd

File tree

1 file changed

+56
-44
lines changed

1 file changed

+56
-44
lines changed

src/sage/matrix/matrix1.pyx

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ TESTS::
99
sage: TestSuite(A).run()
1010
"""
1111

12-
#*****************************************************************************
12+
# ***************************************************************************
1313
# Copyright (C) 2005, 2006 William Stein <[email protected]>
1414
#
1515
# This program is free software: you can redistribute it and/or modify
1616
# it under the terms of the GNU General Public License as published by
1717
# the Free Software Foundation, either version 2 of the License, or
1818
# (at your option) any later version.
19-
# http://www.gnu.org/licenses/
20-
#*****************************************************************************
19+
# https://www.gnu.org/licenses/
20+
# ***************************************************************************
2121

2222
from cpython.sequence cimport PySequence_Fast
2323

@@ -55,9 +55,9 @@ cdef class Matrix(Matrix0):
5555
for i from 0 <= i < nr:
5656
tmp = []
5757
for j from 0 <= j < nc:
58-
tmp.append(w[i*nc + j]._pari_init_())
59-
v.append( ','.join(tmp))
60-
return 'Mat([%s])'%(';'.join(v))
58+
tmp.append(w[i * nc + j]._pari_init_())
59+
v.append(','.join(tmp))
60+
return 'Mat([%s])' % (';'.join(v))
6161

6262
def __pari__(self):
6363
"""
@@ -135,11 +135,12 @@ cdef class Matrix(Matrix0):
135135
for i from 0 <= i < self._nrows:
136136
tmp = []
137137
for j from 0 <= j < self._ncols:
138-
tmp.append(self.get_unsafe(i,j)._gap_init_())
139-
v.append( '[%s]'%(','.join(tmp)) )
138+
tmp.append(self.get_unsafe(i, j)._gap_init_())
139+
v.append('[%s]' % (','.join(tmp)))
140140
# It is needed to multiply with 'One(...)', because
141141
# otherwise the result would not be a gap matrix
142-
return '[%s]*One(%s)'%(','.join(v),sage.interfaces.gap.gap(self.base_ring()).name())
142+
return '[%s]*One(%s)' % (','.join(v),
143+
sage.interfaces.gap.gap(self.base_ring()).name())
143144

144145
def _libgap_(self):
145146
"""
@@ -261,9 +262,9 @@ cdef class Matrix(Matrix0):
261262
for i from 0 <= i < self._nrows:
262263
tmp = []
263264
for j from 0 <= j < self._ncols:
264-
tmp.append(self.get_unsafe(i,j)._maxima_init_())
265-
v.append( '[%s]'%(','.join(tmp)) )
266-
return 'matrix(%s)'%(','.join(v))
265+
tmp.append(self.get_unsafe(i, j)._maxima_init_())
266+
v.append('[%s]' % (','.join(tmp)))
267+
return 'matrix(%s)' % (','.join(v))
267268

268269
def _mathematica_init_(self):
269270
"""
@@ -420,7 +421,7 @@ cdef class Matrix(Matrix0):
420421
0+1r5 3
421422
"""
422423
P = polymake(self.parent())
423-
return polymake.new_object(P, [ list(r) for r in self.rows(copy=False) ])
424+
return polymake.new_object(P, [list(r) for r in self.rows(copy=False)])
424425

425426
def _singular_(self, singular=None):
426427
"""
@@ -434,7 +435,8 @@ cdef class Matrix(Matrix0):
434435
except (NotImplementedError, AttributeError):
435436
raise TypeError("Cannot coerce to Singular")
436437

437-
return singular.matrix(self.nrows(),self.ncols(),singular(self.list()))
438+
return singular.matrix(self.nrows(), self.ncols(),
439+
singular(self.list()))
438440

439441
def _macaulay2_(self, macaulay2=None):
440442
"""
@@ -504,9 +506,9 @@ cdef class Matrix(Matrix0):
504506
for i from 0 <= i < nr:
505507
tmp = []
506508
for j from 0 <= j < nc:
507-
tmp.append(w[i*nc + j]._pari_init_())
508-
v.append( ','.join(tmp))
509-
return '[%s]'%(';'.join(v))
509+
tmp.append(w[i * nc + j]._pari_init_())
510+
v.append(','.join(tmp))
511+
return '[%s]' % (';'.join(v))
510512

511513
def _scilab_(self, scilab=None):
512514
"""
@@ -657,7 +659,8 @@ cdef class Matrix(Matrix0):
657659
entries.sort()
658660
# We hand-format the keys to get rid of the space that would
659661
# normally follow the comma
660-
entries = [(sib.name('(%d,%d)'%k), sib(v, 2)) for k,v in entries]
662+
entries = [(sib.name('(%d,%d)' % k), sib(v, 2))
663+
for k, v in entries]
661664
return sib.name('matrix')(self.base_ring(),
662665
sib.int(self.nrows()),
663666
sib.int(self.ncols()),
@@ -736,7 +739,7 @@ cdef class Matrix(Matrix0):
736739
"""
737740
import numpy
738741
A = numpy.matrix(self.list(), dtype=dtype, copy=copy)
739-
return numpy.resize(A,(self.nrows(), self.ncols()))
742+
return numpy.resize(A, (self.nrows(), self.ncols()))
740743

741744
def _mpmath_(self, prec=None, rounding=None):
742745
"""
@@ -768,7 +771,7 @@ cdef class Matrix(Matrix0):
768771
# Define the magic "__array__" function so that numpy.array(m) can convert
769772
# a matrix m to a numpy array.
770773
# See http://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html#converting-an-arbitrary-sequence-object
771-
__array__=numpy
774+
__array__ = numpy
772775

773776
###################################################
774777
# Construction functions
@@ -1046,14 +1049,16 @@ cdef class Matrix(Matrix0):
10461049
raise ValueError(msg.format(copy))
10471050
x = self.fetch('columns')
10481051
if x is not None:
1049-
if copy: return list(x)
1052+
if copy:
1053+
return list(x)
10501054
return x
10511055
if self.is_sparse():
10521056
columns = self.sparse_columns(copy=copy)
10531057
else:
10541058
columns = self.dense_columns(copy=copy)
10551059
self.cache('columns', columns)
1056-
if copy: return list(columns)
1060+
if copy:
1061+
return list(columns)
10571062
return columns
10581063

10591064
def rows(self, copy=True):
@@ -1102,14 +1107,16 @@ cdef class Matrix(Matrix0):
11021107
raise ValueError(msg.format(copy))
11031108
x = self.fetch('rows')
11041109
if x is not None:
1105-
if copy: return list(x)
1110+
if copy:
1111+
return list(x)
11061112
return x
11071113
if self.is_sparse():
11081114
rows = self.sparse_rows(copy=copy)
11091115
else:
11101116
rows = self.dense_rows(copy=copy)
11111117
self.cache('rows', rows)
1112-
if copy: return list(rows)
1118+
if copy:
1119+
return list(rows)
11131120
return rows
11141121

11151122
def dense_columns(self, copy=True):
@@ -1158,7 +1165,8 @@ cdef class Matrix(Matrix0):
11581165
"""
11591166
x = self.fetch('dense_columns')
11601167
if x is not None:
1161-
if copy: return list(x)
1168+
if copy:
1169+
return list(x)
11621170
return x
11631171
cdef Py_ssize_t i
11641172
A = self if self.is_dense() else self.dense_matrix()
@@ -1212,7 +1220,8 @@ cdef class Matrix(Matrix0):
12121220
"""
12131221
x = self.fetch('dense_rows')
12141222
if x is not None:
1215-
if copy: return list(x)
1223+
if copy:
1224+
return list(x)
12161225
return x
12171226

12181227
cdef Py_ssize_t i
@@ -1268,7 +1277,8 @@ cdef class Matrix(Matrix0):
12681277
"""
12691278
x = self.fetch('sparse_columns')
12701279
if x is not None:
1271-
if copy: return list(x)
1280+
if copy:
1281+
return list(x)
12721282
return x
12731283

12741284
cdef Py_ssize_t i, j
@@ -1350,7 +1360,8 @@ cdef class Matrix(Matrix0):
13501360
"""
13511361
x = self.fetch('sparse_rows')
13521362
if x is not None:
1353-
if copy: return list(x)
1363+
if copy:
1364+
return list(x)
13541365
return x
13551366

13561367
cdef Py_ssize_t i, j
@@ -1501,7 +1512,7 @@ cdef class Matrix(Matrix0):
15011512
return self.rows(copy=False)[i]
15021513
cdef Py_ssize_t j
15031514
V = self.row_ambient_module()
1504-
tmp = [self.get_unsafe(i,j) for j in range(self._ncols)]
1515+
tmp = [self.get_unsafe(i, j) for j in range(self._ncols)]
15051516
return V(tmp, coerce=False, copy=False, check=False)
15061517

15071518
###########################################################################
@@ -1750,12 +1761,12 @@ cdef class Matrix(Matrix0):
17501761
bottom = bottom.row()
17511762
else:
17521763
raise TypeError('a matrix must be stacked with '
1753-
'another matrix or a vector')
1764+
'another matrix or a vector')
17541765
other = <Matrix?>bottom
17551766

17561767
if self._ncols != other._ncols:
17571768
raise TypeError("number of columns must be the same, not %s and %s" %
1758-
(self.ncols(), bottom.ncols()) )
1769+
(self.ncols(), bottom.ncols()))
17591770

17601771
top_ring = self._base_ring
17611772
bottom_ring = other._base_ring
@@ -1794,10 +1805,10 @@ cdef class Matrix(Matrix0):
17941805
cdef Py_ssize_t nr = self._nrows
17951806
for r in range(self._nrows):
17961807
for c in range(self._ncols):
1797-
Z.set_unsafe(r, c, self.get_unsafe(r,c))
1808+
Z.set_unsafe(r, c, self.get_unsafe(r, c))
17981809
for r in range(other._nrows):
17991810
for c in range(other._ncols):
1800-
Z.set_unsafe(r+nr, c, other.get_unsafe(r,c))
1811+
Z.set_unsafe(r + nr, c, other.get_unsafe(r, c))
18011812

18021813
return Z
18031814

@@ -1976,29 +1987,29 @@ cdef class Matrix(Matrix0):
19761987
right = right.column()
19771988
else:
19781989
raise TypeError("a matrix must be augmented with another matrix, "
1979-
"or a vector")
1990+
"or a vector")
19801991

19811992
cdef Matrix other
19821993
other = right
19831994

19841995
if self._nrows != other._nrows:
19851996
raise TypeError('number of rows must be the same, '
1986-
'{0} != {1}'.format(self._nrows, other._nrows))
1997+
'{0} != {1}'.format(self._nrows, other._nrows))
19871998
if not (self._base_ring is other.base_ring()):
19881999
other = other.change_ring(self._base_ring)
19892000

19902001
cdef Matrix Z
1991-
Z = self.new_matrix(ncols = self._ncols + other._ncols)
2002+
Z = self.new_matrix(ncols=self._ncols + other._ncols)
19922003

19932004
cdef Py_ssize_t r, c
19942005
for r from 0 <= r < self._nrows:
19952006
for c from 0 <= c < self._ncols:
1996-
Z.set_unsafe(r,c, self.get_unsafe(r,c))
2007+
Z.set_unsafe(r, c, self.get_unsafe(r, c))
19972008
nc = self.ncols()
19982009

19992010
for r from 0 <= r < other._nrows:
20002011
for c from 0 <= c < other._ncols:
2001-
Z.set_unsafe(r, c+nc, other.get_unsafe(r,c))
2012+
Z.set_unsafe(r, c + nc, other.get_unsafe(r, c))
20022013

20032014
if subdivide:
20042015
Z._subdivide_on_augment(self, other)
@@ -2261,7 +2272,7 @@ cdef class Matrix(Matrix0):
22612272
return A
22622273

22632274
def submatrix(self, Py_ssize_t row=0, Py_ssize_t col=0,
2264-
Py_ssize_t nrows=-1, Py_ssize_t ncols=-1):
2275+
Py_ssize_t nrows=-1, Py_ssize_t ncols=-1):
22652276
"""
22662277
Return the matrix constructed from ``self`` using the specified
22672278
range of rows and columns.
@@ -2316,7 +2327,8 @@ cdef class Matrix(Matrix0):
23162327
nrows = self._nrows - row
23172328
if ncols == -1:
23182329
ncols = self._ncols - col
2319-
return self.matrix_from_rows_and_columns(range(row, row+nrows), range(col, col+ncols))
2330+
return self.matrix_from_rows_and_columns(range(row, row + nrows),
2331+
range(col, col + ncols))
23202332

23212333
def set_row(self, row, v):
23222334
r"""
@@ -2382,7 +2394,7 @@ cdef class Matrix(Matrix0):
23822394
raise ValueError(msg.format(self._ncols, len(v)))
23832395
if (row < 0) or (row >= self._nrows):
23842396
msg = "row number must be between 0 and {0} (inclusive), not {1}"
2385-
raise ValueError(msg.format(self._nrows-1, row))
2397+
raise ValueError(msg.format(self._nrows - 1, row))
23862398

23872399
try:
23882400
for j in range(self._ncols):
@@ -2456,7 +2468,7 @@ cdef class Matrix(Matrix0):
24562468
raise ValueError(msg.format(self._nrows, len(v)))
24572469
if (col < 0) or (col >= self._ncols):
24582470
msg = "column number must be between 0 and {0} (inclusive), not {1}"
2459-
raise ValueError(msg.format(self._ncols-1, col))
2471+
raise ValueError(msg.format(self._ncols - 1, col))
24602472

24612473
try:
24622474
for i in range(self._nrows):
@@ -2640,7 +2652,7 @@ cdef class Matrix(Matrix0):
26402652
return self
26412653
cdef Matrix A
26422654
A = self.new_matrix(self._nrows, self._ncols, self,
2643-
coerce=False, sparse=False)
2655+
coerce=False, sparse=False)
26442656
if self._subdivisions is not None:
26452657
A.subdivide(self.subdivisions())
26462658
return A
@@ -2684,7 +2696,7 @@ cdef class Matrix(Matrix0):
26842696
if self.is_sparse():
26852697
return self
26862698
A = self.new_matrix(self._nrows, self._ncols, self,
2687-
coerce=False, sparse=True)
2699+
coerce=False, sparse=True)
26882700
if self._subdivisions is not None:
26892701
A.subdivide(self.subdivisions())
26902702
return A

0 commit comments

Comments
 (0)