# [SciPy-User] Can I copy a sparse matrix into an existing dense numpy matrix?

Warren Weckesser warren.weckesser@enthought....
Mon Feb 6 10:21:09 CST 2012

```On Mon, Feb 6, 2012 at 8:56 AM, Conrad Lee <conradlee@gmail.com> wrote:

> Warren, thanks for the suggestion with the COO matrix.  In general I'm
> storing sparse matrices in the CSR format for quick multiplication, so your
> approach would mean that I have to convert to a COO matrix every time, but
> that conversion is pretty quick.
>

Here's an example of how you could do the assignment directly with a CSR
matrix:

import numpy as np
from scipy.sparse import csr_matrix

# 'c' is a sparse matrix in CSR format.
c = csr_matrix([[0,0,1,0,0,0],
[0,2,0,3,0,0],
[0,0,0,0,0,0],
[4,0,0,0,5,0]])

# 'a' is the dense array into which we'll copy the nonzero
# elements of 'c'
a = np.zeros(c.shape, dtype=c.dtype)

# The next line is the key part: it converts c.indptr into
# the row indices in the dense array. (c.indices already has
# the columns.)
rows = sum((m*[k] for k, m in enumerate(np.diff(c.indptr))), [])

a[rows, c.indices] = c.data

print c.todense()
print a
print np.all(c.todense() == a)

This might be more efficient than converting to COO.

Warren

> Although, unless your sparsity pattern doesn't change (which it may not),
>> you'll need to zero the entire dense array before reassigning, which will
>> also take "a non-negligible amount of time".
>>
>
> Zeroing out a matrix seems to happen very quickly, probably because it's a
> vectorized operation taking advantage of the SIMD instructions on modern
> processors.  As far as I understand it, allocating huge amounts of memory
> requires slower operations.  I did a quick and dirty benchmark, and zeroing
> takes a small fraction of the time of allocating.
>
>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120206/f9207606/attachment.html
```