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: Fix typecasting problem in scipy.sparse.lil_matrix truediv #19408
Conversation
Co-authored-by: Dan Schult <dschult@colgate.edu>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A unit test covering this would be quite useful. It looks like we have something similar for DOK already (see TestDOK.test_dok_divide_scalar
), so let's add something similar to the TestLIL class (next to test_scalar_mul
).
scipy/sparse/_lil.py
Outdated
@@ -287,6 +287,9 @@ def _mul_scalar(self, other): | |||
def __truediv__(self, other): # self / other | |||
if isscalarlike(other): | |||
new = self.copy() | |||
new_dtype = np.result_type(self.dtype, np.array(other).dtype) | |||
if new_dtype != self.dtype: | |||
new.data = new.data.astype(new_dtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work. The data
member of LIL-format objects is a numpy array of Python lists, with dtype=object.
To change the dtype, we need to do something like this:
for i, rowvals in enumerate(new.data):
new.data[i] = rowvals.astype(new_dtype)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I should have noticed this. Isn't just the following what we really want?
new.dtype = np.result_type(self, other)
The list data is actually modified correctly. Things only start breaking when trying to convert to a different storage format, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm... sorry that my solution is actually quite wrong in a number of ways.
@vetschn is correct that the values in new.data are correct (complex values).
It is the dtype that is not updated. The values are actually python objects. So they morph to what they need to become to hold the correct value.
It is definitely true that we need a test. That will show us what will work. :)
I think @vetschn 's solution should work. I have tried it at an interactive interpreter.
But there are many subtleties with dtype that I haven't experienced. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks for adding the test!
Can someone restart the failing test? It looks like it timed out during the build phase of the test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI is clean now, core dev approval, and locally the full suite passes on this branch as well.
In it goes with backport label, thanks for your first contribution to SciPy.
…y#19408) * Fix typecasting problem in lil_matrix truedriv * Added test --------- Co-authored-by: Dan Schult <dschult@colgate.edu>
…y#19408) * Fix typecasting problem in lil_matrix truedriv * Added test --------- Co-authored-by: Dan Schult <dschult@colgate.edu>
Reference issue
Closes #19403
What does this implement/fix?
This fixes the inconsistent behavior I observed when dividing a
scipy.sparse.lil_matrix
by a scalar of differing type. Now the resultingdtype
is checked and adjusted if necessary.