-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: A sparse matrix raises a ValueError when __rmul__
with a value as None
#15210
Comments
Thanks for reporting this bug, @takagi. You are correct, that should be a |
I'm adding new issues that should be fixed for 1.8.0rc2 (or 1.8.0 final) to the 1.8.0 milestone. If a different procedure is preferred, let me know. |
xref: cupy/cupy#6233 |
Thanks, it is definitely fine for us. |
The problem is that
I can add a specific check for None, but it sounds like you want a wider class of objects excluded? I you only want None to be excluded, I can easily add that back in with a special check. |
Here's the offending code from # If it's a list or whatever, treat it like a matrix
other_a = np.asanyarray(other) |
How about reverting this line https://github.com/scipy/scipy/pull/14822/files#diff-8aed1ba485f8d01bc74679865b6ca8fa04efde8b6fef5a57f587b61eb7a8734cR607 to return (self.transpose() * tr).transpose() or return self.transpose()._mul_dispatch(tr).transpose() |
We cannot revert that line, or it will break for arrays. I think by the time we get to the point where we think |
I got
but I expect
|
I think the problem is that |
This is a bug, for sure! |
OK, figured it out: rmul is missing from the array interface 🙄 |
I'd like to see the issue reopened to clarify the status. The original problem persists in 1.8.0rc2.
|
Yes, sorry, my fix only helped for sparse arrays, not matrices. |
Would this be too explicit a fix? diff --git a/scipy/sparse/_base.py b/scipy/sparse/_base.py
index 0611242e9..47b0ca5fc 100644
--- a/scipy/sparse/_base.py
+++ b/scipy/sparse/_base.py
@@ -604,6 +604,10 @@ class spmatrix:
tr = other.transpose()
except AttributeError:
tr = np.asarray(other).transpose()
+
+ if tr.shape == ():
+ raise TypeError('Multiplier is not scalar-like, but also cannot be converted to an array')
+
return (self.transpose() @ tr).transpose()
def __rmul__(self, other): # other * self Cut-and-pastable test code: import numpy as np
import scipy.sparse as sp
m = np.eye(3)
m = sp.coo_matrix(m)
None * m |
It might be possible that
If the "array" class does not implement
with SciPy 1.7.3 and
with SciPy 1.8.0rc2. |
Describe your issue.
A sparse matrix multiplied with a value of an unsupported operand type raises TypeError. However, in SciPy 1.8.0rc1, a sparse matrix raises ValueError when it is called
__rmul__
with a value that is not interpretable as an array, e.g.None
. It should raise TypeError instead as__mul__
does.Reproducing Code Example
Error message
SciPy/NumPy/Python version information
1.8.0rc1 1.22.0rc1 sys.version_info(major=3, minor=9, micro=7, releaselevel='final', serial=0)
The text was updated successfully, but these errors were encountered: