From d1a4cf467a477c6e9ed2bb34c664edbb0cd82358 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Tue, 21 Jun 2022 14:27:10 -0700 Subject: [PATCH 1/4] Add complex number support to `tan` --- .gitignore | 1 + .../array_api/elementwise_functions.py | 26 +++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 73e203a64..86bab2717 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ node_modules/ __pycache__/ *.pyc spec/**/generated +tmp/ \ No newline at end of file diff --git a/spec/API_specification/array_api/elementwise_functions.py b/spec/API_specification/array_api/elementwise_functions.py index e75b087c9..7b3229b78 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -1318,27 +1318,43 @@ def subtract(x1: array, x2: array, /) -> array: """ def tan(x: array, /) -> array: - """ - Calculates an implementation-dependent approximation to the tangent, having domain ``(-infinity, +infinity)`` and codomain ``(-infinity, +infinity)``, for each element ``x_i`` of the input array ``x``. Each element ``x_i`` is assumed to be expressed in radians. + r""" + Calculates an implementation-dependent approximation to the tangent for each element ``x_i`` of the input array ``x``. + + Each element ``x_i`` is assumed to be expressed in radians. **Special cases** - For floating-point operands, + For real-valued floating-point operands, - If ``x_i`` is ``NaN``, the result is ``NaN``. - If ``x_i`` is ``+0``, the result is ``+0``. - If ``x_i`` is ``-0``, the result is ``-0``. - If ``x_i`` is either ``+infinity`` or ``-infinity``, the result is ``NaN``. + For complex floating-point operands, special cases must be handled as if the operation is implemented as ``-1j +* tanh(x*1j)``. + + .. note:: + Tangent is an analytical function on the complex plane and has no branch cuts. The function is periodic, with period :math:`\pi j`, with respect to the real component and has first order poles along the real line at coordinates :math:`(\pi (\frac{1}{2} + n), 0)`. However, IEEE 754 binary floating-point representation cannot represent the value :math:`\pi / 2` exactly, and, thus, no argument value is possible for which a pole error occurs. + + .. note:: + For complex arguments, the mathematical definition of tangent is + + .. math:: + \begin{align} \operatorname{tan}(x) &= \frac{(e^{-jx} - e^{jx})j}{e^{-jx} + e^{jx}} \\ &= (-1) \frac{(e^{jx} - e^{-jx})j}{e^{jx} + e^{-jx}} \\ &= -j \cdot \operatorname{tanh}(jx) \end{align} + + where :math:`operatorname{tanh}` is the hyperbolic tangent. + Parameters ---------- x: array - input array whose elements are expressed in radians. Should have a real-valued floating-point data type. + input array whose elements are expressed in radians. Should have a floating-point data type. Returns ------- out: array - an array containing the tangent of each element in ``x``. The returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`. + an array containing the tangent of each element in ``x``. The returned array must have a floating-point data type determined by :ref:`type-promotion`. """ def tanh(x: array, /) -> array: From bf2ebd129e8fcf56e74b49fe24a590c758dfc6bf Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Tue, 21 Jun 2022 14:28:38 -0700 Subject: [PATCH 2/4] Fix linebreak --- spec/API_specification/array_api/elementwise_functions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/API_specification/array_api/elementwise_functions.py b/spec/API_specification/array_api/elementwise_functions.py index 7b3229b78..80e851abf 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -1332,8 +1332,7 @@ def tan(x: array, /) -> array: - If ``x_i`` is ``-0``, the result is ``-0``. - If ``x_i`` is either ``+infinity`` or ``-infinity``, the result is ``NaN``. - For complex floating-point operands, special cases must be handled as if the operation is implemented as ``-1j -* tanh(x*1j)``. + For complex floating-point operands, special cases must be handled as if the operation is implemented as ``-1j * tanh(x*1j)``. .. note:: Tangent is an analytical function on the complex plane and has no branch cuts. The function is periodic, with period :math:`\pi j`, with respect to the real component and has first order poles along the real line at coordinates :math:`(\pi (\frac{1}{2} + n), 0)`. However, IEEE 754 binary floating-point representation cannot represent the value :math:`\pi / 2` exactly, and, thus, no argument value is possible for which a pole error occurs. From 838f6d2fa39a574cabe0aea039bbb2cbab6a78a0 Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Tue, 21 Jun 2022 14:32:24 -0700 Subject: [PATCH 3/4] Fix equation --- spec/API_specification/array_api/elementwise_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/API_specification/array_api/elementwise_functions.py b/spec/API_specification/array_api/elementwise_functions.py index 80e851abf..dfba7b748 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -1343,7 +1343,7 @@ def tan(x: array, /) -> array: .. math:: \begin{align} \operatorname{tan}(x) &= \frac{(e^{-jx} - e^{jx})j}{e^{-jx} + e^{jx}} \\ &= (-1) \frac{(e^{jx} - e^{-jx})j}{e^{jx} + e^{-jx}} \\ &= -j \cdot \operatorname{tanh}(jx) \end{align} - where :math:`operatorname{tanh}` is the hyperbolic tangent. + where :math:`\operatorname{tanh}` is the hyperbolic tangent. Parameters ---------- From 5926acc54a9197cf90759f14e38455da256d662c Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Tue, 21 Jun 2022 14:33:05 -0700 Subject: [PATCH 4/4] Update equation --- spec/API_specification/array_api/elementwise_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/API_specification/array_api/elementwise_functions.py b/spec/API_specification/array_api/elementwise_functions.py index dfba7b748..a6587b644 100644 --- a/spec/API_specification/array_api/elementwise_functions.py +++ b/spec/API_specification/array_api/elementwise_functions.py @@ -1341,7 +1341,7 @@ def tan(x: array, /) -> array: For complex arguments, the mathematical definition of tangent is .. math:: - \begin{align} \operatorname{tan}(x) &= \frac{(e^{-jx} - e^{jx})j}{e^{-jx} + e^{jx}} \\ &= (-1) \frac{(e^{jx} - e^{-jx})j}{e^{jx} + e^{-jx}} \\ &= -j \cdot \operatorname{tanh}(jx) \end{align} + \begin{align} \operatorname{tan}(x) &= \frac{j(e^{-jx} - e^{jx})}{e^{-jx} + e^{jx}} \\ &= (-1) \frac{j(e^{jx} - e^{-jx})}{e^{jx} + e^{-jx}} \\ &= -j \cdot \operatorname{tanh}(jx) \end{align} where :math:`\operatorname{tanh}` is the hyperbolic tangent.