|
From: Toine B. <tb...@db...> - 2009-11-16 14:33:54
|
Hi,
What is the most efficient way of transposing a matrix using PySparse?
A search of the website doesn't yield any results and I don't see any
transpose method in the list of methods. I must be missing something
here; I can't imagine there is no fast transpose method available!
I've tried two different ways of calculating the transpose myself:
1) looping through the entries of a matrix M with .items() and then
simply filling a new sparse matrix with the same values, but transposed.
def T_ver1( self, M ):
(rows, cols) = M.shape
t = spmatrix.ll_mat(cols, rows, M.nnz)
for (x, y), value in M.items():
t[y, x] = M[x,y]
return t
2) I've noticed that using the .dot() method to multiply a matrix with
the identity matrix is a faster way of getting the transposed matrix M.
def T_ver2( self, M ):
(rows, cols) = M.shape
I = spmatrix.ll_mat(rows, rows, rows)
for i in xrange(0, rows):
I[i, i] = 1
return spmatrix.dot(M, I)):
Method 2 is faster, which I suspect is because the .dot() function
using either the C or Fortran code directly. But why is there no
direct M.transpose() or M.T method to give me the transposed matrix?
Thanks in advance for the help!
Kind regards,
Toine Bogers |