diff options
author | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-01-02 17:19:48 +0100 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-01-02 17:56:28 +0100 |
commit | bd6d287dd73c483654cfb10f4cfab4dcc7548dd2 (patch) | |
tree | d4ef2356d5b986c3b7aec62e59e6c8149b7d1e75 | |
parent | 192355eb77883b1bd779f0a0bbad9e137de3193d (diff) | |
download | numpy-bd6d287dd73c483654cfb10f4cfab4dcc7548dd2.tar.gz |
BUG: fix complex norm of higher order
asfarray truncates the complex part, so it must be avoided for complex
types.
Closes gh-4156.
-rw-r--r-- | numpy/linalg/linalg.py | 2 | ||||
-rw-r--r-- | numpy/linalg/tests/test_linalg.py | 12 |
2 files changed, 13 insertions, 1 deletions
diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index dd586827e..a7e12aa08 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -2088,7 +2088,7 @@ def norm(x, ord=None, axis=None): # because it will downcast to float64. absx = abs(x) else: - absx = asfarray(x) + absx = x if isComplexType(x.dtype.type) else asfarray(x) if absx.dtype is x.dtype: absx = abs(absx) else: diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py index feb4c8224..0c21a4229 100644 --- a/numpy/linalg/tests/test_linalg.py +++ b/numpy/linalg/tests/test_linalg.py @@ -921,6 +921,18 @@ class _TestNorm(object): x = np.array([-2 ** 31], dtype=np.int32) old_assert_almost_equal(norm(x, ord=3), 2 ** 31, decimal=5) + def test_complex_high_ord(self): + # gh-4156 + d = np.empty((2,), dtype=np.clongdouble) + d[0] = 6+7j + d[1] = -6+7j + res = 11.615898132184 + old_assert_almost_equal(np.linalg.norm(d, ord=3), res, decimal=10) + d = d.astype(np.complex128) + old_assert_almost_equal(np.linalg.norm(d, ord=3), res, decimal=9) + d = d.astype(np.complex64) + old_assert_almost_equal(np.linalg.norm(d, ord=3), res, decimal=5) + class TestNormDouble(_TestNorm): dt = np.double |