pydata / sparse

Sparse multi-dimensional arrays for the PyData ecosystem

Home Page:https://sparse.pydata.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiplicative assignment warning

piperfw opened this issue · comments

Description
I was recently adapting code to switch from scipy.sparse.csr_array to sparse.COO and was caught out when a multiplcative assignment (see example below) did not multiply the array by a scalar but removed some of the elements, because the COO dtype was int. I assume this expected behaviour (e.g. according to some casting rules), but couldn't find it discussed in the documentation. Might it be worth adding a warning there if not given explicit in the code (note scipy.sparse raises UFuncTypeError if you try to do this operation)?

Example Code

A=sparse.COO([[0,1],[0,1]],[1,1],shape=(2,2))
B = A * 0.5 
B.todense() # [[0.5,0],[0,0.5]] (naively expected)
A *= 0.5
A.todense() # [[0,0],[0,0]] # surprise

That seems like a bug to me. The correct behavior for an in-place operation that would upcast the left-hand size if executed out of place is to raise an exception, not to downcast the right-hand side.

Yes, this is a bug indeed. I'll get around to it sometime today.