I am writing a numpy/cython program to compute the minors of small matrices (a lot of them).
My current function looks like (computes the minor of mat wrt. to row ii, col jj):
cdef float minor(np.ndarray[DTYPE_t, ndim = 2] mat,int ii,int jj):
rows = range(mat.shape[0])
col = range(mat.shape[0])
del rows[ii]
del col[jj]
cdef np.ndarray[DTYPE_t, ndim = 2] rM = (mat[rows])[:,col]
cdef float val = (-1)**(ii+jj) * np.linalg.det(rM)
return val
After a little benchmarking, the line
cdef np.ndarray[DTYPE_t, ndim = 2] rM = (mat[rows])[:,col]
Is rather time consuming. Is there a better way to remove one row and one column from an two dimensional array?
Yours,
cp3028
It looks like you’re copying the memory from
(mat[rows])[:,col], allocation and copying is a slow process. Is it not possible to simply make the function callnp.linalg.degon the chunks ofmatin place, instead of copying it and calculating the determinant on the copy?