Skip to content

Commit 8732592

Browse files
committed
Add complex number support to tanh
1 parent 6a35bc1 commit 8732592

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ node_modules/
3030
__pycache__/
3131
*.pyc
3232
spec/**/generated
33+
tmp/

spec/API_specification/array_api/elementwise_functions.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,28 +1342,58 @@ def tan(x: array, /) -> array:
13421342
"""
13431343

13441344
def tanh(x: array, /) -> array:
1345-
"""
1346-
Calculates an implementation-dependent approximation to the hyperbolic tangent, having domain ``[-infinity, +infinity]`` and codomain ``[-1, +1]``, for each element ``x_i`` of the input array ``x``.
1345+
r"""
1346+
Calculates an implementation-dependent approximation to the hyperbolic tangent for each element ``x_i`` of the input array ``x``.
1347+
1348+
The mathematical definition of the hyperbolic tangent is
1349+
1350+
.. math::
1351+
\begin{align} \operatorname{tanh}(x) &= \frac{\operatorname{sinh}(x)}{\operatorname{cosh}(x)} \\ &= \frac{e^x - e^{-x}}{e^x + e^{-x}} \end{align}
1352+
1353+
where :math:`\operatorname{sinh}(x)` is the hyperbolic sine and :math:`operatorname{cosh}(x)` is the hyperbolic cosine.
13471354
13481355
**Special cases**
13491356
1350-
For floating-point operands,
1357+
.. note::
1358+
For all operands, ``tanh(-x)`` must equal ``-tanh(x)``.
1359+
1360+
For real-valued floating-point operands,
13511361
13521362
- If ``x_i`` is ``NaN``, the result is ``NaN``.
13531363
- If ``x_i`` is ``+0``, the result is ``+0``.
13541364
- If ``x_i`` is ``-0``, the result is ``-0``.
13551365
- If ``x_i`` is ``+infinity``, the result is ``+1``.
13561366
- If ``x_i`` is ``-infinity``, the result is ``-1``.
13571367
1368+
For complex floating-point operands, let ``a = real(x_i)``, ``b = imag(x_i)``, and
1369+
1370+
.. note::
1371+
For complex floating-point operands, ``tanh(conj(x))`` must equal ``conj(tanh(x))``.
1372+
1373+
- If ``a`` is ``+0`` and ``b`` is ``+0``, the result is ``+0 + 0j``.
1374+
- If ``a`` is a nonzero finite number and ``b`` is ``+infinity``, the result is ``NaN + NaN j``.
1375+
- If ``a`` is ``+0`` and ``b`` is ``+infinity``, the result is ``+0 + NaN j``.
1376+
- If ``a`` is a nonzero finite number and ``b`` is ``NaN``, the result is ``NaN + NaN j``.
1377+
- If ``a`` is ``+0`` and ``b`` is ``NaN``, the result is ``+0 + NaN j``.
1378+
- If ``a`` is ``+infinity`` and ``b`` is a positive (i.e., greater than ``0``) finite number, the result is ``1 + 0j``.
1379+
- If ``a`` is ``+infinity`` and ``b`` is ``+infinity``, the result is ``1 + 0j`` (sign of the imaginary component is unspecified).
1380+
- If ``a`` is ``+infinity`` and ``b`` is ``NaN``, the result is ``1 + 0j`` (sign of the imaginary component is unspecified).
1381+
- If ``a`` is ``NaN`` and ``b`` is ``+0``, the result is ``NaN + 0j``.
1382+
- If ``a`` is ``NaN`` and ``b`` is a nonzero number, the result is ``NaN + NaN j``.
1383+
- If ``a`` is ``NaN`` and ``b`` is ``NaN``, the result is ``NaN + NaN j``.
1384+
1385+
.. note::
1386+
The hyperbolic 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 imaginary component and has first order poles along the imaginary line at coordinates :math:`(0, \pi (\frac{1}{2} + n))`. However, IEEE 754 binary floating-point representation cannot represent :math:`\pi / 2` exactly, and, thus, no argument value is possible such that a pole error occurs.
1387+
13581388
Parameters
13591389
----------
13601390
x: array
1361-
input array whose elements each represent a hyperbolic angle. Should have a real-valued floating-point data type.
1391+
input array whose elements each represent a hyperbolic angle. Should have a floating-point data type.
13621392
13631393
Returns
13641394
-------
13651395
out: array
1366-
an array containing the hyperbolic tangent of each element in ``x``. The returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`.
1396+
an array containing the hyperbolic tangent of each element in ``x``. The returned array must have a floating-point data type determined by :ref:`type-promotion`.
13671397
"""
13681398

13691399
def trunc(x: array, /) -> array:

0 commit comments

Comments
 (0)