[SciPy-dev] Adding to an array with __radd__()

Ed Schofield schofield at ftw.at
Mon Oct 31 06:52:44 CST 2005

Sparse matrix objects now support adding a sparse matrix to a dense
matrix of the same dimensions with the syntax:

newdense = sparse + dense

I'd like to add support for the opposite order too:

newdense = dense + sparse

but this doesn't seem possible currently.  This operation calls the
__radd__() method of the sparse matrix object multiple times, each time
passing a single element from the dense matrix.  The problem is that the
sparse matrix's __radd__ function also needs to know the shape of the
matrix and the position of the element it's receiving.

Could we change the default behaviour of ndarray objects to invoke the
right-hand object's __radd__ function just once, passing the dense
array?  I think this is the default behaviour for Python objects.  What
code would break if we made this change?

-- Ed

More information about the Scipy-dev mailing list