NumPy tanh() Function



The NumPy tanh() function is used to compute the hyperbolic tangent of each element in an input array. It calculates the ratio of the hyperbolic sine to the hyperbolic cosine for each input element, returning values in the range (-1, 1).

  • Domain: The function accepts input values for all real numbers. It can handle any scalar or array of real numbers.
  • Range: The output values lie in the range (-1, 1), as the hyperbolic tangent function always returns values within this interval.

Syntax

Following is the syntax of the NumPy tanh() function −

numpy.tanh(x, /, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Parameters

This function accepts the following parameters −

  • x: The input array or scalar. The function computes the hyperbolic tangent of each element of the array or scalar.
  • out (optional): A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where (optional): This condition is broadcast over the input. At locations where the condition is True, the result will be computed. Otherwise, the result will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are processed. The dtype of x is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array where each element is the hyperbolic tangent of the corresponding element in the input array x, in radians.

Example: Basic Usage of tanh() Function

In the following example, we use the tanh() function to compute the hyperbolic tangent of each element in a 1-dimensional array −

import numpy as np

# Creating a 1-dimensional array
arr = np.array([-1, 0, 1, 2])

# Applying tanh to each element
result = np.tanh(arr)
print(result)

The output obtained will be −

[-0.76159416  0.          0.76159416  0.96402758]

Example: Hyperbolic Tangent of Angles in Degrees

In this example, we first calculate the hyperbolic tangent in radians and then convert the result to degrees using the numpy.degrees() function −

import numpy as np

# Creating an array of values
values = np.array([-1, 0, 1, 2])

# Calculate tanh in radians
radians_result = np.tanh(values)

# Convert radians to degrees
degrees_result = np.degrees(radians_result)
print(degrees_result)

This will produce the following result −

[-43.63613084   0.          43.63613084  55.23471167]

Example: Hyperbolic Tangent of Single Scalar Value

In the example below, we are using the tanh() function to calculate the hyperbolic tangent of a single scalar value −

import numpy as np

# Scalar value
scalar = 1

# Applying tanh to the scalar
result = np.tanh(scalar)
print(result)

The output obtained is −

0.7615941559557649

Example: Hyperbolic Tangent of Large Values

In this example, we calculate the hyperbolic tangent of very large values. The function will approach 1 as the input value increases −

import numpy as np

# Large values
large_values = np.array([1000, 10000, 100000])

# Applying tanh to the array
result = np.tanh(large_values)
print(result)

This will produce the following result −

[1. 1. 1.]
numpy_trigonometric_functions.htm
Advertisements