[SciPy-User] scipy.sparse.csr_matrix: Refills with same graph, different values
Robert Cimrman
cimrman3@ntc.zcu...
Tue Oct 12 03:49:00 CDT 2010
On Tue, 12 Oct 2010, Nico Schlömer wrote:
> 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* ================================
Finite element assembling, right?
> 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().
It seems that some of the entries you expect to exist do not exist. The
following works for me (scipy 0.9.0.dev6812):
In [1]: import scipy.sparse as sps
In [2]: a = sps.csr_matrix(sps.eye(5, 5))
In [3]: a
Out[3]:
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 5 stored elements in Compressed Sparse Row format>
In [4]: a.data[:] = 0
In [5]: a
Out[5]:
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 5 stored elements in Compressed Sparse Row format>
In [6]: print a
------> print(a)
(0, 0) 0.0
(1, 1) 0.0
(2, 2) 0.0
(3, 3) 0.0
(4, 4) 0.0
In [7]: a[2, 2] += 4
In [8]: a
Out[8]:
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 5 stored elements in Compressed Sparse Row format>
In [9]: print a
------> print(a)
(0, 0) 0.0
(1, 1) 0.0
(2, 2) 4.0
(3, 3) 0.0
(4, 4) 0.0
r.
> On Tue, Oct 12, 2010 at 10:22 AM, Robert Cimrman <cimrman3@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@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
>
More information about the SciPy-User
mailing list