Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 9 commits into from
Apr 1, 2019
Merged

BUG: Fix of var method for complex arrays #13177

merged 9 commits into from
Apr 1, 2019

Conversation

cnighut
Copy link
Contributor

@cnighut cnighut commented Mar 22, 2019

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.

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
Copy link
Contributor

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).

Copy link
Contributor Author

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.

@@ -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):
Copy link
Contributor

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

@charris charris changed the title BUG: Closes #13110. Fixed the incorrect optimization for complex arrays BUG: Fix incorrect computation of variance for complex arrays Mar 25, 2019
@charris charris changed the title BUG: Fix incorrect computation of variance for complex arrays BUG: Fix incorrect computation of var and std methods for complex arrays Mar 25, 2019
@charris charris changed the title BUG: Fix incorrect computation of var and std methods for complex arrays BUG: Fix computation of var and std methods of complex arrays Mar 25, 2019
@charris charris changed the title BUG: Fix computation of var and std methods of complex arrays BUG: Fix of var and std methods of complex arrays Mar 25, 2019
@charris charris changed the title BUG: Fix of var and std methods of complex arrays BUG: Fix of var method for complex arrays Mar 25, 2019
Copy link
Contributor

@mhvk mhvk left a 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.

@cnighut
Copy link
Contributor Author

cnighut commented Mar 25, 2019

A program using unittest framework should suffice, right?

@mhvk
Copy link
Contributor

mhvk commented Mar 25, 2019

@qawbecrdteyf - there are test files under numpy/core/tests - find the relevant one that tests .var (sorry, pressed for time so cannot point you to it...) and just add one.

@mattip
Copy link
Member

mattip commented Mar 25, 2019

this seems like a good place

@cnighut
Copy link
Contributor Author

cnighut commented Mar 31, 2019

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])
Copy link
Contributor

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...)

Copy link
Member

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

Copy link
Contributor

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.

Copy link
Contributor

@mhvk mhvk left a 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

@mhvk
Copy link
Contributor

mhvk commented Apr 1, 2019

Failure is unrelated, so merging.

@mhvk mhvk merged commit f297d31 into numpy:master Apr 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants