|
13 | 13 | from typing_extensions import override |
14 | 14 |
|
15 | 15 | from array_api_extra import ( |
| 16 | + angle, |
16 | 17 | apply_where, |
17 | 18 | argpartition, |
18 | 19 | at, |
|
49 | 50 | from array_api_extra._lib._utils._typing import Array, Device |
50 | 51 | from array_api_extra.testing import lazy_xp_function |
51 | 52 |
|
| 53 | +lazy_xp_function(angle) |
52 | 54 | lazy_xp_function(apply_where) |
53 | 55 | lazy_xp_function(argpartition) |
54 | 56 | lazy_xp_function(atleast_nd) |
|
73 | 75 | lazy_xp_function(_funcs_searchsorted) |
74 | 76 |
|
75 | 77 |
|
| 78 | +class TestAngle: |
| 79 | + def test_basic(self, xp: ModuleType): |
| 80 | + x = xp.asarray( |
| 81 | + [ |
| 82 | + 1 + 3j, |
| 83 | + np.sqrt(2) / 2.0 + 1j * np.sqrt(2) / 2, |
| 84 | + 1, |
| 85 | + 1j, |
| 86 | + -1, |
| 87 | + -1j, |
| 88 | + 1 - 3j, |
| 89 | + -1 + 3j, |
| 90 | + ], |
| 91 | + dtype=xp.complex128, |
| 92 | + ) |
| 93 | + expected = xp.asarray( |
| 94 | + [ |
| 95 | + np.arctan(3.0 / 1.0), |
| 96 | + np.arctan(1.0), |
| 97 | + 0, |
| 98 | + np.pi / 2, |
| 99 | + np.pi, |
| 100 | + -np.pi / 2.0, |
| 101 | + -np.arctan(3.0 / 1.0), |
| 102 | + np.pi - np.arctan(3.0 / 1.0), |
| 103 | + ], |
| 104 | + dtype=xp.float64, |
| 105 | + ) |
| 106 | + |
| 107 | + xp_assert_close(angle(x), expected, rtol=0, atol=1e-11) |
| 108 | + xp_assert_close(angle(x, deg=True), expected * 180 / xp.pi, rtol=0, atol=1e-11) |
| 109 | + |
| 110 | + def test_real(self, xp: ModuleType): |
| 111 | + x = xp.asarray([0.0, -0.0, 1.0, -1.0]) |
| 112 | + expected = xp.asarray([0.0, xp.pi, 0.0, xp.pi], dtype=x.dtype) |
| 113 | + xp_assert_close(angle(x), expected) |
| 114 | + |
| 115 | + def test_integral(self, xp: ModuleType): |
| 116 | + x = xp.asarray([0, -1, 1], dtype=xp.int32) |
| 117 | + actual = angle(x) |
| 118 | + expected = xp.asarray( |
| 119 | + [0.0, xp.pi, 0.0], dtype=default_dtype(xp, device=get_device(x)) |
| 120 | + ) |
| 121 | + xp_assert_close(actual, expected) |
| 122 | + assert actual.dtype == expected.dtype |
| 123 | + |
| 124 | + @pytest.mark.skip_xp_backend(Backend.NUMPY_READONLY, reason="xp=xp") |
| 125 | + def test_xp(self, xp: ModuleType): |
| 126 | + x = xp.asarray([1.0, 1.0j, 1 + 1j], dtype=xp.complex128) |
| 127 | + expected = xp.asarray([0.0, xp.pi / 2, xp.pi / 4], dtype=xp.float64) |
| 128 | + xp_assert_close(angle(x, xp=xp), expected) |
| 129 | + |
| 130 | + |
76 | 131 | class TestApplyWhere: |
77 | 132 | @staticmethod |
78 | 133 | def f1(x: Array, y: Array | int = 10) -> Array: |
|
0 commit comments