![](https://secure.gravatar.com/avatar/cf9a8b662145ad4ab1762b375d8c6466.jpg?s=120&d=mm&r=g)
I had noticed that Travis Oliphant had a sparse.py package, but it no longer is available (clicking on the link gives a "404"). I have a particular kind of sparse matrix that I'd like to use to give vector matrix multiplies. In particular, it's an n x n matrix which has at most k (which is small, usually 2 or 3) non-zeros in each row which are in consecutive locations. I have this encoded as an n x k matrix, the i-th row gives the non-zero values in the i-th row of the big matrix, and an n long vector of indices -- the i-th element gives the starting position in the i-th row. When I want to multiply this matrix by a row vector v on the left. To do the multiplication I do the following: # loc is the location vector n = matrix.shape[0] mm = reshape(v,(-1,1))*matrix w = zeros((n+m),v.typecode()) for i in range(mm.shape[0]): w[loc[i]:loc[i]+matrix.shape[1]] += w[i] w = w[:n] I would like to be able to replace the loop with some Numeric operations. Is there a trick to do this? Note that the n that I'm using is around 100000, so that storing the full matrix is out of the question (and multiplying by that matrix would be extremely inefficient, anyway). -- Victor S. Miller | " ... Meanwhile, those of us who can compute can hardly victor@idaccr.org | be expected to keep writing papers saying 'I can do the CCR, Princeton, NJ | following useless calculation in 2 seconds', and indeed 08540 USA | what editor would publish them?" -- Oliver Atkin
![](https://secure.gravatar.com/avatar/cf9a8b662145ad4ab1762b375d8c6466.jpg?s=120&d=mm&r=g)
Sorry, I had a typo in the program. It should be: # M is n by k, and represents a sparse n by n matrix A # the non-zero entries of row i of A start in column loc[i] # and are the i-th row of M in locations loc[i]:loc[i]+k # loc is the location vector n,k = M.shape mm = reshape(v,(-1,1))*M w = zeros((n+m),v.typecode()) # is there a trick to replace the loop below? for i in range(mm.shape[0]): w[loc[i]:loc[i]+k] += mm[i] w = w[:n] -- Victor S. Miller | " ... Meanwhile, those of us who can compute can hardly victor@idaccr.org | be expected to keep writing papers saying 'I can do the CCR, Princeton, NJ | following useless calculation in 2 seconds', and indeed 08540 USA | what editor would publish them?" -- Oliver Atkin
![](https://secure.gravatar.com/avatar/db2b8768e55888615195a7492260fd1b.jpg?s=120&d=mm&r=g)
On Tue, 30 Jul 2002 09:42:13 -0400 Victor S Miller wrote: Victor> I had noticed that Travis Oliphant had a sparse.py package, Victor> but it no longer is available (clicking on the link gives a Victor> "404"). It's part of scipy now. Greetings, Jochen -- University of North Carolina phone: +1-919-962-4403 Department of Chemistry phone: +1-919-962-1579 Venable Hall CB#3290 (Kenan C148) fax: +1-919-843-6041 Chapel Hill, NC 27599, USA GnuPG key: 44BCCD8E
![](https://secure.gravatar.com/avatar/cf9a8b662145ad4ab1762b375d8c6466.jpg?s=120&d=mm&r=g)
Sorry, I had a typo in the program. It should be: # M is n by k, and represents a sparse n by n matrix A # the non-zero entries of row i of A start in column loc[i] # and are the i-th row of M in locations loc[i]:loc[i]+k # loc is the location vector n,k = M.shape mm = reshape(v,(-1,1))*M w = zeros((n+m),v.typecode()) # is there a trick to replace the loop below? for i in range(mm.shape[0]): w[loc[i]:loc[i]+k] += mm[i] w = w[:n] -- Victor S. Miller | " ... Meanwhile, those of us who can compute can hardly victor@idaccr.org | be expected to keep writing papers saying 'I can do the CCR, Princeton, NJ | following useless calculation in 2 seconds', and indeed 08540 USA | what editor would publish them?" -- Oliver Atkin
![](https://secure.gravatar.com/avatar/db2b8768e55888615195a7492260fd1b.jpg?s=120&d=mm&r=g)
On Tue, 30 Jul 2002 09:42:13 -0400 Victor S Miller wrote: Victor> I had noticed that Travis Oliphant had a sparse.py package, Victor> but it no longer is available (clicking on the link gives a Victor> "404"). It's part of scipy now. Greetings, Jochen -- University of North Carolina phone: +1-919-962-4403 Department of Chemistry phone: +1-919-962-1579 Venable Hall CB#3290 (Kenan C148) fax: +1-919-843-6041 Chapel Hill, NC 27599, USA GnuPG key: 44BCCD8E
participants (2)
-
Jochen Küpper
-
victor@idaccr.org