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
7 changes: 4 additions & 3 deletions numpy/core/_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ 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):
x = um.multiply(x, um.conjugate(x), out=x).real
else:
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

ret = umr_sum(x, axis, dtype, out, keepdims)

# Compute degrees of freedom and make sure it is not negative.
Expand Down
3 changes: 3 additions & 0 deletions numpy/core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

B[0] = 1j
assert_almost_equal(np.var(B), 0.25)

class TestIsscalar(object):
def test_isscalar(self):
Expand Down