[Scipy-tickets] [SciPy] #1042: sparse matrix failed with element-wise multiplication using numpy.multiply()
SciPy Trac
scipy-tickets@scipy....
Fri Apr 19 03:07:20 CDT 2013
#1042: sparse matrix failed with element-wise multiplication using
numpy.multiply()
--------------------------+-------------------------------------------------
Reporter: dingle | Owner: wnbell
Type: defect | Status: new
Priority: normal | Milestone:
Component: scipy.sparse | Version: 0.7.0
Keywords: multiply |
--------------------------+-------------------------------------------------
Comment(by pv):
The problem with `np.multiply` here is that `np.array(some_sparse_matrix)`
does something pretty weird:
{{{
>>> import numpy as np
>>> from scipy.sparse import lil_matrix
>>> np.array(lil_matrix([[1, 0], [0, 1]]))
array(<2x2 sparse matrix of type '<type 'numpy.int32'>'
with 2 stored elements in LInked List format>, dtype=object)
}}}
Note that it's an 0-dim object array containing the lil_matrix.
One fix is to add an `__array__` method that just does `self.todense()` to
the bottom-most sparse matrix base class. This would make
`np.multiply(asp, b)` first cast `asp` to a dense matrix before doing
anything else.
That's a quick and dirty way to fix up the interoperability a bit. A
better way would be to fix Numpy so that it does something sensible, such
as optionally falling back to `__elmul__` and `__relmul__` methods inside
`np.multiply` --- it already does some checking, but I think this has not
been well thought out so far (see e.g.
`numpy/core/src/umath/ufunc_object.c:1908`).
--
Ticket URL: <http://projects.scipy.org/scipy/ticket/1042#comment:4>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.
More information about the Scipy-tickets
mailing list