From d5aae9c52e30b93b0d2d262d7c1c9b245f289327 Mon Sep 17 00:00:00 2001 From: qawbecrdteyf Date: Fri, 22 Mar 2019 17:29:02 +0530 Subject: [PATCH 1/9] BUG: Closes #13110. Fixed the incorrect optimization for complex arrays --- numpy/core/_methods.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index 51362c7614ee..aadcb0867124 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -115,13 +115,10 @@ 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: - x = um.multiply(x, x, out=x) - ret = umr_sum(x, axis, dtype, out, keepdims) + x = um.multiply(x,x,out=x) + ret = umr_sum(x,axis,dtype,out,keepdims) - # Compute degrees of freedom and make sure it is not negative. + # Compute degrees of freedom and make sure it is not negative. rcount = max([rcount - ddof, 0]) # divide by degrees of freedom @@ -132,7 +129,6 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): ret = ret.dtype.type(ret / rcount) else: ret = ret / rcount - return ret def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): From d68e620738caf67aec7b102b23234a1a5aee6b4f Mon Sep 17 00:00:00 2001 From: qawbecrdteyf Date: Fri, 22 Mar 2019 18:12:08 +0530 Subject: [PATCH 2/9] Closes #13110. Removedd incorrect optimisation for complex type array. Removed a typo from initial commit. --- numpy/core/_methods.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index aadcb0867124..82f9b741740a 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -115,10 +115,10 @@ 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) - x = um.multiply(x,x,out=x) - ret = umr_sum(x,axis,dtype,out,keepdims) + x = um.multiply(x, x, out=x) + ret = umr_sum(x, axis, dtype, out, keepdims) - # Compute degrees of freedom and make sure it is not negative. + # Compute degrees of freedom and make sure it is not negative. rcount = max([rcount - ddof, 0]) # divide by degrees of freedom @@ -129,6 +129,7 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): ret = ret.dtype.type(ret / rcount) else: ret = ret / rcount + return ret def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): From 494ed9d3457f5e07e6233fa5a2331dbb462f15ab Mon Sep 17 00:00:00 2001 From: qawbecrdteyf Date: Fri, 22 Mar 2019 21:58:53 +0530 Subject: [PATCH 3/9] Closes #13110. Removedd incorrect optimisation for complex type array. --- numpy/core/_methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index 82f9b741740a..3568898cd55d 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -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) - x = um.multiply(x, x, out=x) + 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. From 7ec463c3bc7eddfcf4a6c3cf36024d5f77cc99be Mon Sep 17 00:00:00 2001 From: qawbecrdteyf Date: Sat, 23 Mar 2019 01:07:47 +0530 Subject: [PATCH 4/9] BUG: Closes #13110. Fixed the incorrect optimization for variance calculation --- numpy/core/_methods.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index 3568898cd55d..eeb802b03e6e 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -115,7 +115,10 @@ 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) - x = um.multiply(x,um.conjugate(x),out=x).real + if not issubclass(arr.dtype.type, nt.integer): + x = um.multiply(x, um.conjugate(x), out=x).real + else: + x = um.multiply(x, x, out=x) ret = umr_sum(x, axis, dtype, out, keepdims) # Compute degrees of freedom and make sure it is not negative. From 9edffa3c5fd8641cf1062a0e0798a61901564555 Mon Sep 17 00:00:00 2001 From: qawbecrdteyf Date: Mon, 25 Mar 2019 19:19:01 +0530 Subject: [PATCH 5/9] BUG: Closes #13110. Fix of method for complex arrays --- numpy/core/_methods.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index eeb802b03e6e..953e7e1b84c2 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -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 not issubclass(arr.dtype.type, nt.integer): - 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. From 6bc7126388792f3bac9dda59528bfb857912c78f Mon Sep 17 00:00:00 2001 From: qawbecrdteyf Date: Mon, 25 Mar 2019 22:30:08 +0530 Subject: [PATCH 6/9] Added test case for method for complex arrays --- numpy/core/tests/test_numeric.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 90ac43a56dc0..1822a7adf72b 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -216,6 +216,9 @@ def test_var(self): assert_(np.isnan(np.var([]))) assert_(w[0].category is RuntimeWarning) + B = np.array([None, 0]) + B[0] = 1j + assert_almost_equal(np.var(B), 0.25) class TestIsscalar(object): def test_isscalar(self): From fadda3937ffd416117febaf331513a38a24022eb Mon Sep 17 00:00:00 2001 From: qawbecrdteyf Date: Mon, 1 Apr 2019 14:00:08 +0530 Subject: [PATCH 7/9] Added comment to define the newly added test cases for imaginary data in object arrays --- numpy/core/tests/test_numeric.py | 1 + 1 file changed, 1 insertion(+) diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 1822a7adf72b..c16d60b936a3 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -216,6 +216,7 @@ def test_var(self): assert_(np.isnan(np.var([]))) assert_(w[0].category is RuntimeWarning) + # Regression test for gh-13177, that object arrays are treated correctly. B = np.array([None, 0]) B[0] = 1j assert_almost_equal(np.var(B), 0.25) From 3b14a2c9c6ffc1628e99c71d26e93b0c13e07de3 Mon Sep 17 00:00:00 2001 From: qawbecrdteyf Date: Mon, 1 Apr 2019 16:15:35 +0530 Subject: [PATCH 8/9] Added comment to define the newly added test cases for imaginary data in object arrays --- numpy/core/tests/test_numeric.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index c16d60b936a3..4d5ed886f19d 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -210,17 +210,16 @@ def test_var(self): assert_almost_equal(np.var(A), 2.9166666666666665) assert_almost_equal(np.var(A, 0), np.array([2.25, 2.25, 2.25])) assert_almost_equal(np.var(A, 1), np.array([0.66666667, 0.66666667])) + # gh-13177. Regression test to check if imaginary data in object arrays are treated correctly. + B = np.array([None, 0]) + B[0] = 1j + assert_almost_equal(np.var(B), 0.25) with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', RuntimeWarning) assert_(np.isnan(np.var([]))) assert_(w[0].category is RuntimeWarning) - # Regression test for gh-13177, that object arrays are treated correctly. - B = np.array([None, 0]) - B[0] = 1j - assert_almost_equal(np.var(B), 0.25) - class TestIsscalar(object): def test_isscalar(self): assert_(np.isscalar(3.1)) From d64cb8cb2e3b021ebb8a0ea625215c043e3351a7 Mon Sep 17 00:00:00 2001 From: qawbecrdteyf Date: Mon, 1 Apr 2019 18:43:55 +0530 Subject: [PATCH 9/9] Added test for imaginary data in object arrays --- numpy/core/tests/test_numeric.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 4d5ed886f19d..1822a7adf72b 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -210,16 +210,16 @@ def test_var(self): assert_almost_equal(np.var(A), 2.9166666666666665) assert_almost_equal(np.var(A, 0), np.array([2.25, 2.25, 2.25])) assert_almost_equal(np.var(A, 1), np.array([0.66666667, 0.66666667])) - # gh-13177. Regression test to check if imaginary data in object arrays are treated correctly. - B = np.array([None, 0]) - B[0] = 1j - assert_almost_equal(np.var(B), 0.25) with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', RuntimeWarning) assert_(np.isnan(np.var([]))) assert_(w[0].category is RuntimeWarning) + B = np.array([None, 0]) + B[0] = 1j + assert_almost_equal(np.var(B), 0.25) + class TestIsscalar(object): def test_isscalar(self): assert_(np.isscalar(3.1))