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 element x_i of the input array x.

Special cases

For 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 +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), and

Note

For complex floating-point operands, tanh(conj(x)) must equal conj(tanh(x)).

  • If a is +0 and b is +0, the result is +0 + 0j.

  • If a is a nonzero finite number and b is +infinity, the result is NaN + NaN j.

  • If a is +0 and b is +infinity, the result is +0 + NaN j.

  • If a is a nonzero finite number and b is NaN, the result is NaN + NaN j.

  • If a is +0 and b is NaN, the result is +0 + NaN j.

  • If a is +infinity and b is a positive (i.e., greater than 0) finite number, the result is 1 + 0j.

  • If a is +infinity and b is +infinity, the result is 1 + 0j (sign of the imaginary component is unspecified).

  • If a is +infinity and b is NaN, the result is 1 + 0j (sign of the imaginary component is unspecified).

  • If a is NaN and b is +0, the result is NaN + 0j.

  • If a is NaN and b is a nonzero number, the result is NaN + NaN j.

  • If a is NaN and b is NaN, the result is NaN + NaN j.

Warning

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.

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. See ivy.func_wrapper.handle_complex_input for 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 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.Container instances in place of any of the arguments

Examples

With ivy.Array input:

>>> 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.Container input:

>>> 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. See ivy.func_wrapper.handle_complex_input for 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 is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • complex_mode (Literal['split', 'magnitude', 'jax'], default: 'jax') – optional specifier for how to handle complex data types. See ivy.func_wrapper.handle_complex_input for 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.])
}