[SciPy-dev] sparse deletion and sparse random matrices.

Viral Shah vshah at interactivesupercomputing.com
Wed Apr 16 14:16:52 EDT 2008


Thanks to all the help I got on this list, I have been able to figure  
out how to get a bunch of our code working with sparse matrices in  
scipy.

The two things I believe could be immediately useful are some simple  
routines for deleting rows and columns, and routines to create sparse  
random matrices. The latter would be greatly useful for examples,  
exploration, debugging etc.

The routines might look something like below. If there's interest, I  
can help with whatever needs to be done to put them in the tree:

# sparse random matrices..
     def sprand (self, m, n, density):
         nnz = m*n*density
         ij = fix(nnz*rand(2, nnz))
         data = rand(nnz)

         s = sparse.coo_matrix((data, ij), shape=(m, n))
         return s

# Deletion. Performance should be comparable to native code..
     def deleterowcol(self, A, delrow, delcol):
         A = A.tocsc()

         m = A.shape[0]
         n = A.shape[1]

         keep = delete (arange(0, m), delrow)
         A = A[keep, :]

         keep = delete (arange(0, m), delcol)
         A = A[:, keep]

         return A




-viral






More information about the SciPy-Dev mailing list