[SciPy-User] scipy.sparse.csr_matrix: Refills with same graph, different values

Nico Schlömer nico.schloemer at gmail.com
Tue Oct 12 04:39:37 EDT 2010


Hi,

> mtx.data[:] = 0.0
Works, thanks.

> But then do not perform operations that would create a new matrix, as the
> zero entries would be thrown away.

What are those? I do something along the lines of

================================ *snip* ================================
        A.data[:] = 0.0
        for element in self.mesh.elements:
            for edge in element.edges:
                A[ edge[0], edge[0] ] += value
                A[ edge[0], edge[1] ] += value2
                ....
================================ *snap* ================================

but this generates the warning

================================ *snip* ================================
/opt/scipy/0.8.0/lib/python/scipy/sparse/compressed.py:500:
SparseEfficiencyWarning: changing the sparsity structure of a
csr_matrix is expensive. lil_matrix is more efficient.
  SparseEfficiencyWarning)
================================ *snap* ================================

and matrix filling takes about five times as long as compared to a
lil_matrix with subsequent .tocsr().

--Nico






On Tue, Oct 12, 2010 at 10:22 AM, Robert Cimrman <cimrman3 at ntc.zcu.cz> wrote:
> Hi Nico,
>
> On Tue, 12 Oct 2010, Nico Schlömer wrote:
>
>> Hi all,
>>
>> I do computations with a sparse matrix the values of which need to
>> change, but never the sparsity structure.
>> Until now, every time the values change, I tossed away the old matrix,
>> create a new lil_matrix, and then use .tocsr() to get what I want.
>> This seems really wasteful, though, as this way, all the sparsity
>> information is computed anew.
>> I've tried setting the values in the csr_matrix -- one created --
>> directly, but even though they go into spots where there's a nonzero
>> already, this operation seems slow. Also, I don't know of a way to
>> zero-out a csr_matrix while keeping the sparsity information.
>>
>> Any hints on this?
>
> The csr_matrix has a data attribute, so zeroing is easy:
>
> mtx.data[:] = 0.0
>
> But then do not perform operations that would create a new matrix, as the
> zero entries would be thrown away.
>
> As for the speed issues, can you be more specific?
>
> r.
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>



More information about the SciPy-User mailing list