Skip to content

Commit f5437ab

Browse files
committed
ENH: add angle delegation wrapper
1 parent 7433dc5 commit f5437ab

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/array_api_extra/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Extra array functions built on top of the array API standard."""
22

33
from . import testing
4-
from ._agnostic._elementwise import angle, apply_where
4+
from ._agnostic._elementwise import apply_where
55
from ._agnostic._inspection import default_dtype
66
from ._at import at
77
from ._creation import create_diagonal, one_hot
8-
from ._elementwise import deg2rad, isclose, nan_to_num, rad2deg, sinc
8+
from ._elementwise import angle, deg2rad, isclose, nan_to_num, rad2deg, sinc
99
from ._indexing import diag_indices, tril_indices, triu_indices, unravel_index
1010
from ._lazy import lazy_apply
1111
from ._linalg import kron

src/array_api_extra/_elementwise.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,41 @@
44
from ._lib import _compat, _helpers
55
from ._lib._typing import Array, ArrayNamespace
66

7-
__all__ = ["deg2rad", "isclose", "nan_to_num", "rad2deg", "sinc"]
7+
__all__ = ["angle", "deg2rad", "isclose", "nan_to_num", "rad2deg", "sinc"]
8+
9+
10+
def angle(z: Array, /, *, deg: bool = False, xp: ArrayNamespace | None = None) -> Array:
11+
"""
12+
Return the angle of the complex argument.
13+
14+
Parameters
15+
----------
16+
z : array
17+
Input array. Real input is interpreted as having zero imaginary part.
18+
deg : bool, optional
19+
Return angle in degrees if True, radians if False (default).
20+
xp : array_namespace, optional
21+
The standard-compatible namespace for `z`. Default: infer.
22+
23+
Returns
24+
-------
25+
array
26+
The counterclockwise angle from the positive real axis on the complex
27+
plane in the range ``(-pi, pi]``.
28+
29+
Examples
30+
--------
31+
>>> import array_api_strict as xp
32+
>>> import array_api_extra as xpx
33+
>>> xpx.angle(xp.asarray([1.0, 1.0j, 1 + 1j]), xp=xp)
34+
Array([0. , 1.57079633, 0.78539816], dtype=array_api_strict.float64)
35+
>>> xpx.angle(xp.asarray([1.0, 1.0j, 1 + 1j]), deg=True, xp=xp)
36+
Array([ 0., 90., 45.], dtype=array_api_strict.float64)
37+
"""
38+
if xp is None:
39+
xp = _compat.array_namespace(z)
40+
41+
return _agnostic._elementwise.angle(z, deg=deg, xp=xp)
842

943

1044
def deg2rad(x: Array, /, *, xp: ArrayNamespace | None = None) -> Array:

tests/main/test_elementwise.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from array_api_extra._lib._typing import Array, ArrayNamespace, Device
2424
from array_api_extra.testing import assert_close, assert_equal, lazy_xp_function
2525

26+
lazy_xp_function(angle)
2627
lazy_xp_function(apply_where)
2728
lazy_xp_function(deg2rad)
2829
lazy_xp_function(isclose)

0 commit comments

Comments
 (0)