diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2018-07-31 12:55:22 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-31 12:55:22 -0500 |
commit | e30cc8e1148523b270fb5884d3743e5968ed3cb1 (patch) | |
tree | da17b259b68f2968269d6f03bc80988ce70eb2e9 /numpy | |
parent | 904571e7f5ebcd46899a2371e42625bc13b3ebc6 (diff) | |
parent | 07d96f4a5c8ab3123581d1c2a0d04f86e3f10816 (diff) | |
download | numpy-e30cc8e1148523b270fb5884d3743e5968ed3cb1.tar.gz |
Merge pull request #11637 from eric-wieser/simplify-angle
ENH: np.angle: Remove unnecessary multiplication, and allow subclasses to pass through
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/lib/function_base.py | 19 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 10 |
2 files changed, 21 insertions, 8 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 9f8b43c4b..75a39beaa 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1309,7 +1309,7 @@ def interp(x, xp, fp, left=None, right=None, period=None): return interp_func(x, xp, fp, left, right) -def angle(z, deg=0): +def angle(z, deg=False): """ Return the angle of the complex argument. @@ -1325,6 +1325,9 @@ def angle(z, deg=0): angle : ndarray or scalar The counterclockwise angle from the positive real axis on the complex plane, with dtype as numpy.float64. + + ..versionchanged:: 1.16.0 + This function works on subclasses of ndarray like `ma.array`. See Also -------- @@ -1339,18 +1342,18 @@ def angle(z, deg=0): 45.0 """ - if deg: - fact = 180/pi - else: - fact = 1.0 - z = asarray(z) - if (issubclass(z.dtype.type, _nx.complexfloating)): + z = asanyarray(z) + if issubclass(z.dtype.type, _nx.complexfloating): zimag = z.imag zreal = z.real else: zimag = 0 zreal = z - return arctan2(zimag, zreal) * fact + + a = arctan2(zimag, zreal) + if deg: + a *= 180/pi + return a def unwrap(p, discont=pi, axis=-1): diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index ba5b90e8c..d5faed6ae 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1043,6 +1043,16 @@ class TestAngle(object): assert_array_almost_equal(y, yo, 11) assert_array_almost_equal(z, zo, 11) + def test_subclass(self): + x = np.ma.array([1 + 3j, 1, np.sqrt(2)/2 * (1 + 1j)]) + x[1] = np.ma.masked + expected = np.ma.array([np.arctan(3.0 / 1.0), 0, np.arctan(1.0)]) + expected[1] = np.ma.masked + actual = angle(x) + assert_equal(type(actual), type(expected)) + assert_equal(actual.mask, expected.mask) + assert_equal(actual, expected) + class TestTrimZeros(object): |