tanh¶
- tanh(x: array, /) array ¶
Calculates an implementation-dependent approximation to the hyperbolic tangent for each element
x_i
of the input arrayx
.The mathematical definition of the hyperbolic tangent is
\[\begin{split}\begin{align} \operatorname{tanh}(x) &= \frac{\operatorname{sinh}(x)}{\operatorname{cosh}(x)} \\ &= \frac{e^x - e^{-x}}{e^x + e^{-x}} \end{align}\end{split}\]where \(\operatorname{sinh}(x)\) is the hyperbolic sine and \(\operatorname{cosh}(x)\) is the hyperbolic cosine.
Note
The hyperbolic tangent is an analytical function on the complex plane and has no branch cuts. The function is periodic, with period \(\pi j\), with respect to the imaginary component and has first order poles along the imaginary line at coordinates \((0, \pi (\frac{1}{2} + n))\). However, IEEE 754 binary floating-point representation cannot represent \(\pi / 2\) exactly, and, thus, no argument value is possible such that a pole error occurs.
- Parameters:
x (array) – input array whose elements each represent a hyperbolic angle. Should have a floating-point data type.
- Returns:
out (array) – an array containing the hyperbolic tangent of each element in
x
. The returned array must have a floating-point data type determined by Type Promotion Rules.
Notes
Special cases
Note
For all operands,
tanh(-x)
must equal-tanh(x)
.For real-valued floating-point operands,
If
x_i
isNaN
, the result isNaN
.If
x_i
is+0
, the result is+0
.If
x_i
is-0
, the result is-0
.If
x_i
is+infinity
, the result is+1
.If
x_i
is-infinity
, the result is-1
.
For complex floating-point operands, let
a = real(x_i)
,b = imag(x_i)
, andNote
For complex floating-point operands,
tanh(conj(x))
must equalconj(tanh(x))
.If
a
is+0
andb
is+0
, the result is+0 + 0j
.If
a
is a nonzero finite number andb
is+infinity
, the result isNaN + NaN j
.If
a
is+0
andb
is+infinity
, the result is+0 + NaN j
.If
a
is a nonzero finite number andb
isNaN
, the result isNaN + NaN j
.If
a
is+0
andb
isNaN
, the result is+0 + NaN j
.If
a
is+infinity
andb
is a positive (i.e., greater than0
) finite number, the result is1 + 0j
.If
a
is+infinity
andb
is+infinity
, the result is1 + 0j
(sign of the imaginary component is unspecified).If
a
is+infinity
andb
isNaN
, the result is1 + 0j
(sign of the imaginary component is unspecified).If
a
isNaN
andb
is+0
, the result isNaN + 0j
.If
a
isNaN
andb
is a nonzero number, the result isNaN + NaN j
.If
a
isNaN
andb
isNaN
, the result isNaN + NaN j
.
Warning
For historical reasons stemming from the C standard, array libraries may not return the expected result when
a
is+0
andb
is either+infinity
orNaN
. The result should be+0 + NaN j
in both cases; however, for libraries compiled against older C versions, the result may beNaN + NaN j
.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.
Changed in version 2022.12: Added complex data type support.