-
-
Notifications
You must be signed in to change notification settings - Fork 11k
BUG: Fix of var
method for complex arrays
#13177
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
Conversation
…array. Removed a typo from initial commit.
numpy/core/_methods.py
Outdated
x = um.multiply(x, um.conjugate(x), out=x).real | ||
else: | ||
x = um.multiply(x, x, out=x) | ||
x = um.multiply(x,um.conjugate(x),out=x).real |
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 isn't quite the solution suggested by @eric-wieser in #13110 (comment): rather the suggestion is to replace issubclass(arr.dtype.type, nt.complexfloating)
with not issubclass(arr.dtype.type, (nt.floating, nt.integer))
(or something like it, I didn't check naming details), i.e., we do want to keep the optimization for numbers we know are not complex (because .conjugate
makes a copy; if it were just a read-only view this would not be an issue, but that's a separate question).
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.
Thank you @mhvk I have made the necessary changes in my latest commit. not issubclass(arr.dtype.type, (nt.floating, nt.integer))
definitely helps. This ensures that optimisation for non complex numbers is kept intact.
numpy/core/_methods.py
Outdated
@@ -115,7 +115,7 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): | |||
# Note that x may not be inexact and that we need it to be an array, | |||
# not a scalar. | |||
x = asanyarray(arr - arrmean) | |||
if issubclass(arr.dtype.type, nt.complexfloating): | |||
if not issubclass(arr.dtype.type, nt.integer): |
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 should include also nt.floating
. And might as well swap the branches, i.e.,
if issubclass(arr.dtype.type, (nt.floating, nt.integer)):
x = um.multiply(x, x, out=x)
else:
x = um.multiply(x, um.conjugate(x), out=x).real
var
and std
methods for complex arrays
var
and std
methods for complex arraysvar
and std
methods of complex arrays
var
and std
methods of complex arraysvar
and std
methods of complex arrays
var
and std
methods of complex arraysvar
method for complex arrays
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.
The change itself now looks good. Can you add a test case? The one from the original issue suffices.
A program using unittest framework should suffice, right? |
@qawbecrdteyf - there are test files under |
this seems like a good place |
The test cases added are right, aren't they? |
@@ -216,6 +216,9 @@ def test_var(self): | |||
assert_(np.isnan(np.var([]))) | |||
assert_(w[0].category is RuntimeWarning) | |||
|
|||
B = np.array([None, 0]) |
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.
The test is great. As a final suggestion, could you add a comment along the lines of
# Regression test for gh-13177, that object arrays are treated correctly.
B = np.array([1j, 0], dtype=object)
(Note: might as well simplify the definition...)
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.
I don't see that this was resolved
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.
strange, I could have sworn that I saw the comment there when I approved... Anyway, not too big a deal.
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 all OK now, thanks @qawbecrdteyf
Failure is unrelated, so merging. |
Closes bug #13110
The earlier code had an incorrect optimisation for variance and that has been changed. Seems to be a very pervasive bug for complex number arrays.