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

Nico Schlömer nico.schloemer@gmail....
Tue Oct 12 04:42:04 CDT 2010


> Finite element assembling, right?

Finite volumes, but yeah, the structure is really similar.

> It seems that some of the entries you expect to exist do not exist. The
> following works for me (scipy 0.9.0.dev6812):

Correct. At the creating of the lil_matrix, some of the values were in
fact 0 and got stripped upon .tocsr(). I can exclude this case now and
run the code without the performance warning.
Still, it runs a lot slower when the csr_matrix is filled directly.
The following snippet may help making clear what I'm talking about:
The second loop takes about twenty (!) times as long when running this
on my computer.

=================================== *snip* ===================================
# -*- coding: utf-8 -*-

from scipy import sparse
import time

n = 10000

# ------------------------------------------------------------------------------
starttime = time.clock()
A = sparse.lil_matrix( ( n, n ) )
for k in range( 1, n-1 ):
    A[k,k-1] -= 0.5
    A[k,k]   += 1.0
    A[k,k+1] -= 0.5
A = A.tocsr()
endtime = time.clock()
print endtime - starttime
# ------------------------------------------------------------------------------
A.data[:] = 0.0
starttime = time.clock()
for k in range( 1, n-1 ):
    A[k,k-1] -= 0.5
    A[k,k]   += 1.0
    A[k,k+1] -= 0.5
endtime = time.clock()
print endtime - starttime
# ------------------------------------------------------------------------------
=================================== *snap* ===================================

--Nico


More information about the SciPy-User mailing list