[SciPy-user] Sparse indexing workarounds

Bill Baxter wbaxter at gmail.com
Sat Apr 14 03:40:43 EDT 2007


Ack!  Pysparse's update_add_mask is bugged!

In [188]: K = arange(12).reshape((3,4))
In [189]: L = spmatrix.ll_mat(10,10)
In [190]: L.update_add_mask(K, [7,8,9],[3,4,5,6], [True]*3,[True]*4)
In [191]: todense(L)
Out[191]:
array([[  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   0.,   3.,   6.,   9.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   1.,   4.,   7.,  10.,   0.,   0.,   0.],
       [  0.,   0.,   0.,   2.,   5.,   8.,  11.,   0.,   0.,   0.]])
In [192]: K
Out[192]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

Notice how K got transposed and reshaped.  To make it work properly
you have to do

    L.update_add_mask(K.T.reshape(3,4), [7,8,9],[3,4,5,6], [True]*3,[True]*4)

--bb


On 4/10/07, Håkan Jakobsson <hakan.jakobsson at gmail.com> wrote:
> You can use pysparse instead of sparse. The update_add_mask function will do
> the trick.
> /Håkan J
>
>
> On 4/10/07, Robert Cimrman < cimrman3 at ntc.zcu.cz> wrote:
> > Bill Baxter wrote:
> > > Does anyone have an easy (and efficient way) to update a submatrix of
> > > a big sparse matrix?
> > >
> > >   K = scipy.sparse.lil_matrix(bigN,bigN)
> > >   ...
> > >   conn = [1,4,11,12]
> > >   K[ix_(conn,conn)] += elemK
> > >
> > > where elemK is a 4x4 dense matrix.
> > >
> > > This kind of thing is very commonly needed in FEM codes for assembling
> > > the global stiffness matrix.  But sparse doesn't seem to support
> > > either += or indexing with the open grid of indices returned by ix_.
> >
> > You may have a look at
> > http://ui505p06-mbs.ntc.zcu.cz/sfe/FEUtilsExample
> > it uses CSR matrix though...
> >
> > r.
> > _______________________________________________
> > SciPy-user mailing list
> > SciPy-user at scipy.org
> > http://projects.scipy.org/mailman/listinfo/scipy-user
> >
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
>



More information about the SciPy-User mailing list