tanh#
- ivy.tanh(x, /, *, complex_mode='jax', out=None)[source]#
Calculate an implementation-dependent approximation to the hyperbolic tangent, having domain
[-infinity, +infinity]and codomain[-1, +1], for each elementx_iof the input arrayx.Special cases
For floating-point operands,
If
x_iisNaN, the result isNaN.If
x_iis+0, the result is+0.If
x_iis-0, the result is-0.If
x_iis+infinity, the result is+1.If
x_iis-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
ais+0andbis+0, the result is+0 + 0j.If
ais a nonzero finite number andbis+infinity, the result isNaN + NaN j.If
ais+0andbis+infinity, the result is+0 + NaN j.If
ais a nonzero finite number andbisNaN, the result isNaN + NaN j.If
ais+0andbisNaN, the result is+0 + NaN j.If
ais+infinityandbis a positive (i.e., greater than0) finite number, the result is1 + 0j.If
ais+infinityandbis+infinity, the result is1 + 0j(sign of the imaginary component is unspecified).If
ais+infinityandbisNaN, the result is1 + 0j(sign of the imaginary component is unspecified).If
aisNaNandbis+0, the result isNaN + 0j.If
aisNaNandbis a nonzero number, the result isNaN + NaN j.If
aisNaNandbisNaN, the result isNaN + NaN j.
Warning
For historical reasons stemming from the C standard, array libraries may not return the expected result when
ais+0andbis either+infinityorNaN. The result should be+0 + NaN jin 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.
- Parameters:
x (
Union[Array,NativeArray]) – input array whose elements each represent a hyperbolic angle. Should have a real-valued floating-point data type.complex_mode (
Literal['split','magnitude','jax'], default:'jax') – optional specifier for how to handle complex data types. Seeivy.func_wrapper.handle_complex_inputfor more detail.out (
Optional[Array], default:None) – optional output, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – 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 type-promotion.
This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Containerinstances in place of any of the argumentsExamples
With
ivy.Arrayinput:>>> x = ivy.array([0., 1., 2.]) >>> y = ivy.tanh(x) >>> print(y) ivy.array([0., 0.762, 0.964])
>>> x = ivy.array([0.5, -0.7, 2.4]) >>> y = ivy.zeros(3) >>> ivy.tanh(x, out=y) >>> print(y) ivy.array([0.462, -0.604, 0.984])
>>> x = ivy.array([[1.1, 2.2, 3.3], ... [-4.4, -5.5, -6.6]]) >>> ivy.tanh(x, out=x) >>> print(x) ivy.array([[0.8, 0.976, 0.997], [-1., -1., -1.]])
With
ivy.Containerinput:>>> x = ivy.Container(a=ivy.array([0., 1., 2.]), ... b=ivy.array([3., 4., 5.])) >>> y = ivy.tanh(x) >>> print(y) { a: ivy.array([0., 0.762, 0.964]), b: ivy.array([0.995, 0.999, 1.]) }
- Array.tanh(self, *, complex_mode='jax', out=None)[source]#
ivy.Array instance method variant of ivy.tanh. This method simply wraps the function, and so the docstring for ivy.tanh also applies to this method with minimal changes.
- Parameters:
self (
Array) – input array whose elements each represent a hyperbolic angle. Should have a real-valued floating-point data type.complex_mode (
Literal['split','magnitude','jax'], default:'jax') – optional specifier for how to handle complex data types. Seeivy.func_wrapper.handle_complex_inputfor more detail.out (
Optional[Array], default:None) – optional output, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array- Returns:
ret – an array containing the hyperbolic tangent of each element in
self. The returned array must have a real-valued floating-point data type determined by type-promotion.
Examples
>>> x = ivy.array([0., 1., 2.]) >>> y = x.tanh() >>> print(y) ivy.array([0., 0.762, 0.964])
- Container.tanh(self, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, complex_mode='jax', out=None)[source]#
ivy.Container instance method variant of ivy.tanh. This method simply wraps the function, and so the docstring for ivy.tanh also applies to this method with minimal changes.
- Parameters:
self (
Container) – input container whose elements each represent a hyperbolic angle. Should have a real-valued floating-point data type.key_chains (
Optional[Union[List[str],Dict[str,str],Container]], default:None) – The key-chains to apply or not apply the method to. Default isNone.to_apply (
Union[bool,Container], default:True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue.prune_unapplied (
Union[bool,Container], default:False) – Whether to prune key_chains for which the function was not applied. Default isFalse.map_sequences (
Union[bool,Container], default:False) – Whether to also map method to sequences (lists, tuples). Default isFalse.complex_mode (
Literal['split','magnitude','jax'], default:'jax') – optional specifier for how to handle complex data types. Seeivy.func_wrapper.handle_complex_inputfor more detail.out (
Optional[Container], default:None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container- Returns:
ret – a container containing the hyperbolic tangent of each element in
self. The returned container must have a real-valued floating-point data type determined by type-promotion.
Examples
>>> x = ivy.Container(a=ivy.array([0., 1., 2.]), ... b=ivy.array([3., 4., 5.])) >>> y = x.tanh() >>> print(y) { a:ivy.array([0., 0.762, 0.964]), b:ivy.array([0.995, 0.999, 1.]) }