@@ -3087,6 +3087,38 @@ cdef class Matrix_modn_dense_template(Matrix_dense):
3087
3087
ans.append(M)
3088
3088
return ans
3089
3089
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
+
3090
3122
def matrix_from_rows (self , rows ):
3091
3123
"""
3092
3124
Return the matrix constructed from self using rows with indices in
@@ -3178,8 +3210,8 @@ cdef class Matrix_modn_dense_template(Matrix_dense):
3178
3210
raise IndexError (" row index out of range" )
3179
3211
for j, col in enumerate (columns):
3180
3212
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 ))
3183
3215
return A
3184
3216
3185
3217
def __bool__ (self ):
0 commit comments