[SciPy-user] broadcasting elementwise on sparse matrix

Nathan Bell wnbell@gmail....
Wed Oct 1 10:52:32 CDT 2008


On Wed, Oct 1, 2008 at 9:50 AM, Robin <robince@gmail.com> wrote:
>
> Is there any way to get broadcasted element wise operations on sparse matrices?
>
> I would like to do something like A * x if A was (n,n) array and x was
> a (n,) array. I saw the sparse matrices have a .multiply method, but
> it doesn't seem to broadcast in the same way as the standard
> multiplication, ie As.multiply(x) gives an "inconsistent shapes"
> error.
>
> The best I have is As.multiply(tile(x,(n,1))) although this has the memory
> overhead that is normally avoided with broadcasting. Is there an alternative?
>

That particular operation is just scaling of the columns, right?  If
so, you can use:
>>> As * spdiags([x],[0], n,n,)

If As is already a CSR matrix, then you can cheat a little and do the
operation in place:
>>> As.data *= x[As.indices]

-- 
Nathan Bell wnbell@gmail.com
http://graphics.cs.uiuc.edu/~wnbell/


More information about the SciPy-user mailing list