|
From: <lu...@o2...> - 2009-11-16 16:05:12
|
Toine Bogers <tb...@db...> writes:
> 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?
Hi,
Mine is:
def transpose(a):
b = ll_mat(a.shape[1], a.shape[0], a.nnz)
v, r, c = a.find()
b.put(v, c, r)
return b
should be faster, though for the price of extra memory usage (v, r, c
arrays).
>
> Thanks in advance for the help!
>
>
> Kind regards,
> Toine Bogers
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now. http://p.sf.net/sfu/bobj-july_______________________________________________
> Pysparse-users mailing list
> Pys...@li...
> https://lists.sourceforge.net/lists/listinfo/pysparse-users
|