[SciPy-user] sparse: slicing/indexing problems

William Hunter williamhunter at ananzi.co.za
Tue Jul 25 10:01:55 EDT 2006


Dear Scipy-Users;

I'm struggling (again/still) with sparse matrices, but now
it has to do
with slicing:

1) The following (inserting a sub-matrix into a bigger
matrix at arbitrary
positions and then updating it) works very well for dense
arrays:


from numpy allclose, import ix_, ones, zeros
Ad = zeros((5,5),float)
Adsub = 5*ones((3,3),float)
indeks = [3,0,4]
Ad[ix_(indeks,indeks)] += Adsub


The last line above will typically be inside a <for> loop.

2) Doing this with sparse matrices is not 'possible',
although I'm sure it
can be done, but I've tried everything I know (slicing as
per normal,
over one axis only, etc.) This is the best I can do, but I
don't like it:

from scipy import sparse
As = sparse.lil_matrix(5,5)
col = 0
for posr in indeks:
    row = 0
    for posc in indeks:
        As[posr,posc] += Adsub[row,col]
        row += 1
    col += 1
# We want a True here...
print allclose(Ad,As.todense())


Any other suggestions, i.e., how do I get rid of the
<for>'s?

I'm using Enthon 2.4-1.0.0.beta4 (but my brand new Linux
box is coming!!!)

Regards,
WH

Sign up for Ananzi Mail - and you and a partner stand the chance to win a breakaway for two at the stunning boutique hotel, Riviera on Vaal Hotel & Country Club!

http://www.swiftsms.co.za/swiftT/track.asp?e=*em*&cid=113&u=8&tid=1115



More information about the SciPy-User mailing list