Skip to content

Commit 19608a6

Browse files
author
Marie BONBOIRE
committed
redefine _from_rows and _from_rows_and_columns
1 parent ae6a7dd commit 19608a6

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

src/sage/matrix/matrix_modn_dense_template.pxi

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,6 +3087,101 @@ cdef class Matrix_modn_dense_template(Matrix_dense):
30873087
ans.append(M)
30883088
return ans
30893089

3090+
def matrix_from_rows(self, rows):
3091+
"""
3092+
Return the matrix constructed from self using rows with indices in
3093+
the rows list.
3094+
3095+
EXAMPLES::
3096+
3097+
sage: M = MatrixSpace(Integers(8),3,3)
3098+
sage: A = M(range(9)); A
3099+
[0 1 2]
3100+
[3 4 5]
3101+
[6 7 0]
3102+
sage: A.matrix_from_rows([2,1])
3103+
[6 7 0]
3104+
[3 4 5]
3105+
"""
3106+
#rows = PySequence_Fast(rows, "rows is not iterable")
3107+
cdef Py_ssize_t nrows = len(rows)
3108+
3109+
# Construct new matrix
3110+
cdef Matrix_modn_dense_template A = self.new_matrix(nrows=nrows)
3111+
3112+
cdef Py_ssize_t i, row
3113+
for i, row in enumerate(rows):
3114+
if row < 0 or row >= self._nrows:
3115+
raise IndexError("row index out of range")
3116+
memcpy(A._entries+(i*self._ncols), self._entries+(row*self._ncols), sizeof(celement)*self._ncols)
3117+
3118+
#for j in range(self._ncols):
3119+
# A.set_unsafe(i, j, self.get_unsafe(row, j))
3120+
return A
3121+
3122+
def matrix_from_rows_and_columns(self, rows, columns):
3123+
"""
3124+
Return the matrix constructed from self from the given rows and
3125+
columns.
3126+
3127+
EXAMPLES::
3128+
3129+
sage: M = MatrixSpace(Integers(8),3,3)
3130+
sage: A = M(range(9)); A
3131+
[0 1 2]
3132+
[3 4 5]
3133+
[6 7 0]
3134+
sage: A.matrix_from_rows_and_columns([1], [0,2])
3135+
[3 5]
3136+
sage: A.matrix_from_rows_and_columns([1,2], [1,2])
3137+
[4 5]
3138+
[7 0]
3139+
3140+
Note that row and column indices can be reordered or repeated::
3141+
3142+
sage: A.matrix_from_rows_and_columns([2,1], [2,1])
3143+
[0 7]
3144+
[5 4]
3145+
3146+
For example here we take from row 1 columns 2 then 0 twice, and do
3147+
this 3 times.
3148+
3149+
::
3150+
3151+
sage: A.matrix_from_rows_and_columns([1,1,1],[2,0,0])
3152+
[5 3 3]
3153+
[5 3 3]
3154+
[5 3 3]
3155+
3156+
AUTHORS:
3157+
3158+
- Jaap Spies (2006-02-18)
3159+
3160+
- Didier Deshommes: some Pyrex speedups implemented
3161+
"""
3162+
#rows = PySequence_Fast(rows, "rows is not iterable")
3163+
#columns = PySequence_Fast(columns, "columns is not iterable")
3164+
3165+
cdef Py_ssize_t ncols = len(columns)
3166+
cdef Py_ssize_t nrows = len(rows)
3167+
3168+
# Check whether column indices are valid
3169+
cdef Py_ssize_t i, j, row, col
3170+
for col in columns:
3171+
if col < 0 or col >= self._ncols:
3172+
raise IndexError("column index out of range")
3173+
3174+
# Construct new matrix
3175+
cdef Matrix_modn_dense_template A = self.new_matrix(nrows=nrows, ncols=ncols)
3176+
for i, row in enumerate(rows):
3177+
if row < 0 or row >= self._nrows:
3178+
raise IndexError("row index out of range")
3179+
for j, col in enumerate(columns):
3180+
A._matrix[i][j] = self._matrix[row][col]
3181+
3182+
#memcpy(A._entries+(j+i*self._ncols), self._entries+(col+row*self._ncols), sizeof(celement))
3183+
return A
3184+
30903185
def __bool__(self):
30913186
"""
30923187
Test whether this matrix is zero.

0 commit comments

Comments
 (0)