Skip to content

Commit d5a4649

Browse files
committed
some cleanup in matrix1.pyx
1 parent 871ba9d commit d5a4649

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,12 +739,12 @@ 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
# Define the magic "__array__" function so that numpy.array(m) can convert
742745
# a matrix m to a numpy array.
743746
# See http://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html#converting-an-arbitrary-sequence-object
744-
__array__=numpy
747+
__array__ = numpy
745748

746749
###################################################
747750
# Construction functions
@@ -1019,14 +1022,16 @@ cdef class Matrix(Matrix0):
10191022
raise ValueError(msg.format(copy))
10201023
x = self.fetch('columns')
10211024
if x is not None:
1022-
if copy: return list(x)
1025+
if copy:
1026+
return list(x)
10231027
return x
10241028
if self.is_sparse():
10251029
columns = self.sparse_columns(copy=copy)
10261030
else:
10271031
columns = self.dense_columns(copy=copy)
10281032
self.cache('columns', columns)
1029-
if copy: return list(columns)
1033+
if copy:
1034+
return list(columns)
10301035
return columns
10311036

10321037
def rows(self, copy=True):
@@ -1075,14 +1080,16 @@ cdef class Matrix(Matrix0):
10751080
raise ValueError(msg.format(copy))
10761081
x = self.fetch('rows')
10771082
if x is not None:
1078-
if copy: return list(x)
1083+
if copy:
1084+
return list(x)
10791085
return x
10801086
if self.is_sparse():
10811087
rows = self.sparse_rows(copy=copy)
10821088
else:
10831089
rows = self.dense_rows(copy=copy)
10841090
self.cache('rows', rows)
1085-
if copy: return list(rows)
1091+
if copy:
1092+
return list(rows)
10861093
return rows
10871094

10881095
def dense_columns(self, copy=True):
@@ -1131,7 +1138,8 @@ cdef class Matrix(Matrix0):
11311138
"""
11321139
x = self.fetch('dense_columns')
11331140
if x is not None:
1134-
if copy: return list(x)
1141+
if copy:
1142+
return list(x)
11351143
return x
11361144
cdef Py_ssize_t i
11371145
A = self if self.is_dense() else self.dense_matrix()
@@ -1185,7 +1193,8 @@ cdef class Matrix(Matrix0):
11851193
"""
11861194
x = self.fetch('dense_rows')
11871195
if x is not None:
1188-
if copy: return list(x)
1196+
if copy:
1197+
return list(x)
11891198
return x
11901199

11911200
cdef Py_ssize_t i
@@ -1241,7 +1250,8 @@ cdef class Matrix(Matrix0):
12411250
"""
12421251
x = self.fetch('sparse_columns')
12431252
if x is not None:
1244-
if copy: return list(x)
1253+
if copy:
1254+
return list(x)
12451255
return x
12461256

12471257
cdef Py_ssize_t i, j
@@ -1323,7 +1333,8 @@ cdef class Matrix(Matrix0):
13231333
"""
13241334
x = self.fetch('sparse_rows')
13251335
if x is not None:
1326-
if copy: return list(x)
1336+
if copy:
1337+
return list(x)
13271338
return x
13281339

13291340
cdef Py_ssize_t i, j
@@ -1474,7 +1485,7 @@ cdef class Matrix(Matrix0):
14741485
return self.rows(copy=False)[i]
14751486
cdef Py_ssize_t j
14761487
V = self.row_ambient_module()
1477-
tmp = [self.get_unsafe(i,j) for j in range(self._ncols)]
1488+
tmp = [self.get_unsafe(i, j) for j in range(self._ncols)]
14781489
return V(tmp, coerce=False, copy=False, check=False)
14791490

14801491
###########################################################################
@@ -1723,12 +1734,12 @@ cdef class Matrix(Matrix0):
17231734
bottom = bottom.row()
17241735
else:
17251736
raise TypeError('a matrix must be stacked with '
1726-
'another matrix or a vector')
1737+
'another matrix or a vector')
17271738
other = <Matrix?>bottom
17281739

17291740
if self._ncols != other._ncols:
17301741
raise TypeError("number of columns must be the same, not %s and %s" %
1731-
(self.ncols(), bottom.ncols()) )
1742+
(self.ncols(), bottom.ncols()))
17321743

17331744
top_ring = self._base_ring
17341745
bottom_ring = other._base_ring
@@ -1767,10 +1778,10 @@ cdef class Matrix(Matrix0):
17671778
cdef Py_ssize_t nr = self._nrows
17681779
for r in range(self._nrows):
17691780
for c in range(self._ncols):
1770-
Z.set_unsafe(r, c, self.get_unsafe(r,c))
1781+
Z.set_unsafe(r, c, self.get_unsafe(r, c))
17711782
for r in range(other._nrows):
17721783
for c in range(other._ncols):
1773-
Z.set_unsafe(r+nr, c, other.get_unsafe(r,c))
1784+
Z.set_unsafe(r + nr, c, other.get_unsafe(r, c))
17741785

17751786
return Z
17761787

@@ -1949,29 +1960,29 @@ cdef class Matrix(Matrix0):
19491960
right = right.column()
19501961
else:
19511962
raise TypeError("a matrix must be augmented with another matrix, "
1952-
"or a vector")
1963+
"or a vector")
19531964

19541965
cdef Matrix other
19551966
other = right
19561967

19571968
if self._nrows != other._nrows:
19581969
raise TypeError('number of rows must be the same, '
1959-
'{0} != {1}'.format(self._nrows, other._nrows))
1970+
'{0} != {1}'.format(self._nrows, other._nrows))
19601971
if not (self._base_ring is other.base_ring()):
19611972
other = other.change_ring(self._base_ring)
19621973

19631974
cdef Matrix Z
1964-
Z = self.new_matrix(ncols = self._ncols + other._ncols)
1975+
Z = self.new_matrix(ncols=self._ncols + other._ncols)
19651976

19661977
cdef Py_ssize_t r, c
19671978
for r from 0 <= r < self._nrows:
19681979
for c from 0 <= c < self._ncols:
1969-
Z.set_unsafe(r,c, self.get_unsafe(r,c))
1980+
Z.set_unsafe(r, c, self.get_unsafe(r, c))
19701981
nc = self.ncols()
19711982

19721983
for r from 0 <= r < other._nrows:
19731984
for c from 0 <= c < other._ncols:
1974-
Z.set_unsafe(r, c+nc, other.get_unsafe(r,c))
1985+
Z.set_unsafe(r, c + nc, other.get_unsafe(r, c))
19751986

19761987
if subdivide:
19771988
Z._subdivide_on_augment(self, other)
@@ -2234,7 +2245,7 @@ cdef class Matrix(Matrix0):
22342245
return A
22352246

22362247
def submatrix(self, Py_ssize_t row=0, Py_ssize_t col=0,
2237-
Py_ssize_t nrows=-1, Py_ssize_t ncols=-1):
2248+
Py_ssize_t nrows=-1, Py_ssize_t ncols=-1):
22382249
"""
22392250
Return the matrix constructed from ``self`` using the specified
22402251
range of rows and columns.
@@ -2289,7 +2300,8 @@ cdef class Matrix(Matrix0):
22892300
nrows = self._nrows - row
22902301
if ncols == -1:
22912302
ncols = self._ncols - col
2292-
return self.matrix_from_rows_and_columns(range(row, row+nrows), range(col, col+ncols))
2303+
return self.matrix_from_rows_and_columns(range(row, row + nrows),
2304+
range(col, col + ncols))
22932305

22942306
def set_row(self, row, v):
22952307
r"""
@@ -2355,7 +2367,7 @@ cdef class Matrix(Matrix0):
23552367
raise ValueError(msg.format(self._ncols, len(v)))
23562368
if (row < 0) or (row >= self._nrows):
23572369
msg = "row number must be between 0 and {0} (inclusive), not {1}"
2358-
raise ValueError(msg.format(self._nrows-1, row))
2370+
raise ValueError(msg.format(self._nrows - 1, row))
23592371

23602372
try:
23612373
for j in range(self._ncols):
@@ -2429,7 +2441,7 @@ cdef class Matrix(Matrix0):
24292441
raise ValueError(msg.format(self._nrows, len(v)))
24302442
if (col < 0) or (col >= self._ncols):
24312443
msg = "column number must be between 0 and {0} (inclusive), not {1}"
2432-
raise ValueError(msg.format(self._ncols-1, col))
2444+
raise ValueError(msg.format(self._ncols - 1, col))
24332445

24342446
try:
24352447
for i in range(self._nrows):
@@ -2613,7 +2625,7 @@ cdef class Matrix(Matrix0):
26132625
return self
26142626
cdef Matrix A
26152627
A = self.new_matrix(self._nrows, self._ncols, self,
2616-
coerce=False, sparse=False)
2628+
coerce=False, sparse=False)
26172629
if self._subdivisions is not None:
26182630
A.subdivide(self.subdivisions())
26192631
return A
@@ -2657,7 +2669,7 @@ cdef class Matrix(Matrix0):
26572669
if self.is_sparse():
26582670
return self
26592671
A = self.new_matrix(self._nrows, self._ncols, self,
2660-
coerce=False, sparse=True)
2672+
coerce=False, sparse=True)
26612673
if self._subdivisions is not None:
26622674
A.subdivide(self.subdivisions())
26632675
return A

0 commit comments

Comments
 (0)