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 |
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 |
From: Dominique O. <dom...@gm...> - 2009-11-16 16:39:51
|
2009/11/16 Łukasz Pankowski <lu...@o2...> > 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). Hi Toine, The find/put method is surely the fastest in general. The more fundamental question is do you really need the explicit transpose? Often, algorithms that need the transpose operate directly on the original matrix. If what you need is matrix-vector products of the form A.T*x, you can always use the matvec_transp() method. If you're using the higher-level PysparseMatrix objects, you can do A*x and x*A. The latter actually computes A.T*x. I hope this helps. -- Dominique |