Skip to content

Commit 9dc714e

Browse files
authored
Add complex number support to tanh (#458)
* Add complex number support to `tanh` * Fix equation * Add warning concerning older C versions
1 parent 792f11e commit 9dc714e

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

spec/API_specification/array_api/elementwise_functions.py

+40-5
Original file line numberDiff line numberDiff line change
@@ -1466,28 +1466,63 @@ def tan(x: array, /) -> array:
14661466
"""
14671467

14681468
def tanh(x: array, /) -> array:
1469-
"""
1470-
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``.
1469+
r"""
1470+
Calculates an implementation-dependent approximation to the hyperbolic tangent for each element ``x_i`` of the input array ``x``.
1471+
1472+
The mathematical definition of the hyperbolic tangent is
1473+
1474+
.. math::
1475+
\begin{align} \operatorname{tanh}(x) &= \frac{\operatorname{sinh}(x)}{\operatorname{cosh}(x)} \\ &= \frac{e^x - e^{-x}}{e^x + e^{-x}} \end{align}
1476+
1477+
where :math:`\operatorname{sinh}(x)` is the hyperbolic sine and :math:`\operatorname{cosh}(x)` is the hyperbolic cosine.
14711478
14721479
**Special cases**
14731480
1474-
For floating-point operands,
1481+
.. note::
1482+
For all operands, ``tanh(-x)`` must equal ``-tanh(x)``.
1483+
1484+
For real-valued floating-point operands,
14751485
14761486
- If ``x_i`` is ``NaN``, the result is ``NaN``.
14771487
- If ``x_i`` is ``+0``, the result is ``+0``.
14781488
- If ``x_i`` is ``-0``, the result is ``-0``.
14791489
- If ``x_i`` is ``+infinity``, the result is ``+1``.
14801490
- If ``x_i`` is ``-infinity``, the result is ``-1``.
14811491
1492+
For complex floating-point operands, let ``a = real(x_i)``, ``b = imag(x_i)``, and
1493+
1494+
.. note::
1495+
For complex floating-point operands, ``tanh(conj(x))`` must equal ``conj(tanh(x))``.
1496+
1497+
- If ``a`` is ``+0`` and ``b`` is ``+0``, the result is ``+0 + 0j``.
1498+
- If ``a`` is a nonzero finite number and ``b`` is ``+infinity``, the result is ``NaN + NaN j``.
1499+
- If ``a`` is ``+0`` and ``b`` is ``+infinity``, the result is ``+0 + NaN j``.
1500+
- If ``a`` is a nonzero finite number and ``b`` is ``NaN``, the result is ``NaN + NaN j``.
1501+
- If ``a`` is ``+0`` and ``b`` is ``NaN``, the result is ``+0 + NaN j``.
1502+
- If ``a`` is ``+infinity`` and ``b`` is a positive (i.e., greater than ``0``) finite number, the result is ``1 + 0j``.
1503+
- If ``a`` is ``+infinity`` and ``b`` is ``+infinity``, the result is ``1 + 0j`` (sign of the imaginary component is unspecified).
1504+
- If ``a`` is ``+infinity`` and ``b`` is ``NaN``, the result is ``1 + 0j`` (sign of the imaginary component is unspecified).
1505+
- If ``a`` is ``NaN`` and ``b`` is ``+0``, the result is ``NaN + 0j``.
1506+
- If ``a`` is ``NaN`` and ``b`` is a nonzero number, the result is ``NaN + NaN j``.
1507+
- If ``a`` is ``NaN`` and ``b`` is ``NaN``, the result is ``NaN + NaN j``.
1508+
1509+
.. warning::
1510+
For historical reasons stemming from the C standard, array libraries may not return the expected result when ``a`` is ``+0`` and ``b`` is either ``+infinity`` or ``NaN``. The result should be ``+0 + NaN j`` in both cases; however, for libraries compiled against older C versions, the result may be ``NaN + NaN j``.
1511+
1512+
Array libraries are not required to patch these older C versions, and, thus, users are advised that results may vary across array library implementations for these special cases.
1513+
1514+
.. note::
1515+
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.
1516+
14821517
Parameters
14831518
----------
14841519
x: array
1485-
input array whose elements each represent a hyperbolic angle. Should have a real-valued floating-point data type.
1520+
input array whose elements each represent a hyperbolic angle. Should have a floating-point data type.
14861521
14871522
Returns
14881523
-------
14891524
out: array
1490-
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`.
1525+
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`.
14911526
"""
14921527

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

0 commit comments

Comments
 (0)