Skip to content

Commit

Permalink
Trac #18761: method submatrix of matrix_mod2_dense needs default argu…
Browse files Browse the repository at this point in the history
…ments

The `submatrix` methods for dense matrices mod 2^e^ have a different
signature than the other `submatrix` methods; the latter treat their
last two arguments as optional. This leads to errors like this (from
6.8.beta5):

{{{
sage: d0=matrix(GF(2),[[1, 1], [1, 1], [1, 1], [1, 1]])
sage: d0._echelon_form_PID()--------------------------------------------
-------------------------------
TypeError                                 Traceback (most recent call
last)
...
TypeError: submatrix() takes exactly 4 positional arguments (2 given)
}}}

or this

{{{
sage: d0=matrix(GF(2),[[1, 1], [1, 1], [1, 1], [1, 1]])
sage: d1=matrix(GF(2),[[1, 1, 0, 0],
 [1, 1, 0, 0],
 [1, 1, 1, 1],
 [1, 1, 1, 1],
 [0, 0, 1, 1],
 [0, 0, 1, 1]]
)
sage: C=ChainComplex(data=(d0,d1))
sage: C.homology(1,generators=True)
------------------------------------------------------------------------
---
TypeError                                 Traceback (most recent call
last)
...
TypeError: submatrix() takes exactly 4 positional arguments (2 given)
}}}

URL: http://trac.sagemath.org/18761
Reported by: cnassau
Ticket author(s): Christian Nassau
Reviewer(s): Martin Albrecht
  • Loading branch information
Release Manager authored and vbraun committed Jun 24, 2015
2 parents 9f1e7dd + 3549edf commit 983a272
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
47 changes: 31 additions & 16 deletions src/sage/matrix/matrix_mod2_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1667,15 +1667,16 @@ cdef class Matrix_mod2_dense(matrix_dense.Matrix_dense): # dense or sparse
Z._entries = mzd_stack(Z._entries, self._entries, other._entries)
return Z

def submatrix(self, lowr, lowc, nrows , ncols):
def submatrix(self, Py_ssize_t row=0, Py_ssize_t col=0,
Py_ssize_t nrows=-1, Py_ssize_t ncols=-1):
"""
Return submatrix from the index lowr,lowc (inclusive) with
Return submatrix from the index row, col (inclusive) with
dimension nrows x ncols.
INPUT:
- lowr -- index of start row
- lowc -- index of start column
- row -- index of start row
- col -- index of start column
- nrows -- number of rows of submatrix
- ncols -- number of columns of submatrix
Expand All @@ -1695,33 +1696,47 @@ cdef class Matrix_mod2_dense(matrix_dense.Matrix_dense): # dense or sparse
True
sage: A[1:200,1:200] == A.submatrix(1,1,199,199)
True
TESTS for handling of default arguments (ticket #18761)::
sage: A.submatrix(17,15) == A.submatrix(17,15,183,185)
True
sage: A.submatrix(row=100,col=37,nrows=1,ncols=3) == A.submatrix(100,37,1,3)
True
"""
cdef Matrix_mod2_dense A

cdef int highr, highc

highr = lowr + nrows
highc = lowc + ncols
if nrows < 0:
nrows = self._nrows - row
if nrows < 0:
nrows = 0

if ncols < 0:
ncols = self._ncols - col
if ncols < 0:
ncols = 0

if nrows <= 0 or ncols <= 0:
raise TypeError("Expected nrows, ncols to be > 0, but got %d,%d instead."%(nrows, ncols))
highr = row + nrows
highc = col + ncols

if row < 0:
raise TypeError("Expected row >= 0, but got %d instead."%row)

if col < 0:
raise TypeError("Expected col >= 0, but got %d instead."%col)

if highc > self._entries.ncols:
raise TypeError("Expected highc <= self.ncols(), but got %d > %d instead."%(highc, self._entries.ncols))

if highr > self._entries.nrows:
raise TypeError("Expected highr <= self.nrows(), but got %d > %d instead."%(highr, self._entries.nrows))

if lowr < 0:
raise TypeError("Expected lowr >= 0, but got %d instead."%lowr)

if lowc < 0:
raise TypeError("Expected lowc >= 0, but got %d instead."%lowc)

A = self.new_matrix(nrows = nrows, ncols = ncols)
if self._ncols == 0 or self._nrows == 0:
if ncols == 0 or nrows == 0:
return A
A._entries = mzd_submatrix(A._entries, self._entries, lowr, lowc, highr, highc)
A._entries = mzd_submatrix(A._entries, self._entries, row, col, highr, highc)
return A

def __reduce__(self):
Expand Down
43 changes: 27 additions & 16 deletions src/sage/matrix/matrix_mod2e_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1279,15 +1279,16 @@ cdef class Matrix_mod2e_dense(matrix_dense.Matrix_dense):
A._entries = mzed_stack(A._entries, self._entries, other._entries)
return A

def submatrix(self, lowr, lowc, nrows , ncols):
def submatrix(self, Py_ssize_t row=0, Py_ssize_t col=0,
Py_ssize_t nrows=-1, Py_ssize_t ncols=-1):
"""
Return submatrix from the index ``lowr,lowc`` (inclusive) with
Return submatrix from the index ``row,col`` (inclusive) with
dimension ``nrows x ncols``.
INPUT:
- ``lowr`` -- index of start row
- ``lowc`` -- index of start column
- ``row`` -- index of start row
- ``col`` -- index of start column
- ``nrows`` -- number of rows of submatrix
- ``ncols`` -- number of columns of submatrix
Expand All @@ -1308,29 +1309,39 @@ cdef class Matrix_mod2e_dense(matrix_dense.Matrix_dense):
True
sage: A[1:200,1:200] == A.submatrix(1,1,199,199)
True
TESTS for handling of default arguments (ticket #18761)::
sage: A.submatrix(17,15) == A.submatrix(17,15,183,185)
True
sage: A.submatrix(row=100,col=37,nrows=1,ncols=3) == A.submatrix(100,37,1,3)
True
"""
cdef int highr = lowr + nrows
cdef int highc = lowc + ncols
if nrows < 0:
nrows = self._nrows - row

if ncols < 0:
ncols = self._ncols - col

if nrows <= 0 or ncols <= 0:
raise TypeError("Expected nrows, ncols to be > 0, but got %d,%d instead."%(nrows, ncols))
cdef int highr = row + nrows
cdef int highc = col + ncols

if row < 0:
raise TypeError("Expected row >= 0, but got %d instead."%row)

if col < 0:
raise TypeError("Expected col >= 0, but got %d instead."%col)

if highc > self._entries.ncols:
raise TypeError("Expected highc <= self.ncols(), but got %d > %d instead."%(highc, self._entries.ncols))

if highr > self._entries.nrows:
raise TypeError("Expected highr <= self.nrows(), but got %d > %d instead."%(highr, self._entries.nrows))

if lowr < 0:
raise TypeError("Expected lowr >= 0, but got %d instead."%lowr)

if lowc < 0:
raise TypeError("Expected lowc >= 0, but got %d instead."%lowc)

cdef Matrix_mod2e_dense A = self.new_matrix(nrows = nrows, ncols = ncols)
if self._ncols == 0 or self._nrows == 0:
if ncols == 0 or nrows == 0:
return A
A._entries = mzed_submatrix(A._entries, self._entries, lowr, lowc, highr, highc)
A._entries = mzed_submatrix(A._entries, self._entries, row, col, highr, highc)
return A

def rank(self):
Expand Down

0 comments on commit 983a272

Please sign in to comment.