Skip to content

Commit 4b24dff

Browse files
author
Marie BONBOIRE
committed
add _from_colums in modn_template
1 parent 6701ef4 commit 4b24dff

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/sage/matrix/matrix_modn_dense_template.pxi

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

3090+
def matrix_from_columns(self, columns):
3091+
"""
3092+
Return the matrix constructed from self using columns with indices
3093+
in the columns 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_columns([2,1])
3103+
[2 1]
3104+
[5 4]
3105+
[0 7]
3106+
"""
3107+
#columns = PySequence_Fast(columns, "columns is not iterable")
3108+
cdef Py_ssize_t ncols = len(columns)
3109+
3110+
# Construct new matrix
3111+
cdef Matrix_modn_dense_template A = self.new_matrix(ncols=ncols)
3112+
cdef Py_ssize_t i, j, col
3113+
for j, col in enumerate(columns):
3114+
if col < 0 or col >= self._ncols:
3115+
raise IndexError("column index out of range")
3116+
for i in range(self._nrows):
3117+
A._matrix[i][j] = self._matrix[i][col]
3118+
3119+
#A.set_unsafe(i, j, self.get_unsafe(i, col))
3120+
return A
3121+
30903122
def matrix_from_rows(self, rows):
30913123
"""
30923124
Return the matrix constructed from self using rows with indices in
@@ -3178,8 +3210,8 @@ cdef class Matrix_modn_dense_template(Matrix_dense):
31783210
raise IndexError("row index out of range")
31793211
for j, col in enumerate(columns):
31803212
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))
3213+
3214+
#A.set_unsafe(i, j, self.get_unsafe(row, col))
31833215
return A
31843216

31853217
def __bool__(self):

0 commit comments

Comments
 (0)