I'm calculating the dot product between a scipy.sparse matrix (CSC) and a numpy ndarray vector:
>>> print type(np_vector), np_vector.shape
<type 'numpy.ndarray'> (200,)
>>> print type(sp_matrix), sparse.isspmatrix(sp_matrix), sp_matrix.shape
<class 'scipy.sparse.csc.csc_matrix'> True (200, 200)
>>> dot_vector = dot(np_vector, sp_matrix)
The result seems to be a new ndarray vector as I was expecting:
>>> print type(dot_vector), dot_vector.shape
<type 'numpy.ndarray'> (200,)
But when I then try to add a scalar to that vector I receive the exception:
>>> scalar = 3.0
>>> print dot_vector + scalar
C:\Python27\lib\site-packages\scipy\sparse\compressed.pyc in __add__(self, other)
173 return self.copy()
174 else: # Now we would add this scalar to every element.
--> 175 raise NotImplementedError('adding a nonzero scalar to a '
176 'sparse matrix is not supported')
177 elif isspmatrix(other):
NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported
As if the result dot_vector was a sparse matrix again.
Specifically, it seems as if I have an ndarray but the sparse matrix __add__ is called for the + operator.
This is the method I would expect to be called:
>>> print dot_vector.__add__
<method-wrapper '__add__' of numpy.ndarray object at 0x05250690>
Am I missing something here or does this really look weird?
What determines which method is called for the + operator?
I am running this code in an IPython Notebook (ipython notebook --pylab inline). Could it be that IPython --pylab or the notebook kernel somehow screw things up?
Thanks for any help!
dot_vectorvariable that contains the sparse matrix, and gets used instead of the local one containing the vector. But without seeing the whole script, this is pure speculation.