[SciPy-user] Sparse matrices and weave.inline, f2py or Pyrex

William Hunter willemjagter at gmail.com
Sat Aug 12 07:50:35 EDT 2006


Yes, well, my best attempt at it. You'll notice that the sparse
matrices doesn't support fancy indexing, unfortunately. (I'm hoping
someone can prove me wrong :-)
Here's some code you can fiddle with (also attached -
sparsefiddle.py), if you want.


#!/usr/bin/env python

from numpy import allclose, ix_, random
from scipy import sparse

Alil = sparse.lil_matrix((6,6))
A = Alil.todense()

edof = [1,3,0,2]
arb = random.rand(4,4)

# Dense matrix
for r in xrange(10):
    for c in xrange(10):
        A[ix_(edof,edof)] += r*c*arb

# Sparse matrices
for r in xrange(10):
    for c in xrange(10):

        # Alil[ix_(edof,edof)] += r*c*arb    # Unfortunately doesn't work

        for idxR,valR in enumerate(edof):    # This works, but slowly
            for idxC,valC in enumerate(edof):
                Alil[valR,valC] += r*c*arb[idxR,idxC]

print allclose(A,Alil.todense()) # Should return a 'True'
#  EOF

Regards,
William

On 11/08/06, David Grant <davidgrant at gmail.com> wrote:
>
>
> On 8/11/06, William Hunter <willemjagter at gmail.com> wrote:
> > List;
> >
> > Has anyone ever attempted using sparse matrices in combination with
> > either one of the following; weave.inline, f2py or Pyrex?
> >
> > What is the best option? Perhaps a case of personal preference?
> >
> > Reason I'm asking: I've gotten the solving part of my code to run
> > satisfactorily, but building them with Python's for loops takes a heck
> > of a long time, too long.
>
> Have you tried using vector indexing with numpy first? See if you can remove
> some of those python for loops.
>
> --
> David Grant
> http://www.davidgrant.ca
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
>
>
-------------- next part --------------
#!/usr/bin/env python

from numpy import allclose, ix_, random
from scipy import sparse

Alil = sparse.lil_matrix((6,6))
A = Alil.todense()

edof = [1,3,0,2]
arb = random.rand(4,4)


# Dense matrix
for r in xrange(10):
    for c in xrange(10):
        A[ix_(edof,edof)] += r*c*arb

# Sparse matrices
for r in xrange(10):
    for c in xrange(10):

        # Alil[ix_(edof,edof)] += r*c*arb    # Unfortunately doesn't work

        for idxR,valR in enumerate(edof):    # This works, but slowly
            for idxC,valC in enumerate(edof):
                Alil[valR,valC] += r*c*arb[idxR,idxC]

print allclose(A,Alil.todense()) # Should return a 'True'



More information about the SciPy-User mailing list