Numpy tanh – hyperbolic tangent element-wise.

(467)

It’s time for the element-wise Numpy hyperbolic tangent function. We are going to use our google collab for the same. Most of us have already experienced working in google collab right? It gives us a better user-friendly platform to work on python language. Let us get started.

Also read: Numpy cosh – Hyperbolic cosine, element-wise

What is a hyperbolic function?

First of all, Let us understand what is hyperbolic function. What makes this different from ordinary trigonometric functions?

A hyperbolic function is somewhat similar to a trigonometric function as well. The only difference between normal trigonometric functions and hyperbolic functions is that the trigonometric function defines the values within a circle (circle radians) but a hyperbolic function defines the values in a hyperbola rather than a circle. the points form the right half of the unit hyperbola.

Three important formulas are depicted below for hyperbolic functions. Let us look at them.

Sinh X 1
Cosh X 1
Tanh X 3

What is hyperbolic tan?

hese are the three most usable hyperbolic functions as well. And today we are going to learn that one among them that is the hyperbolic Tangent function. We can represent this function in the form of code as well below.

numpy.tanh(x) = np.sinh(x)/np.cosh(x)

But in our code snippet today, We are not going to use the above for our implementation today. We are having a predefined hyperbolic tangent function which is NumPy.tanh() provided by the NumPy library. We are going to use the same.

Working with hyperbolic tan

In this tutorial, we will learn how to find the hyperbolic tangent functions for Radian measures as well as complex values. Let us have a quick look at the syntax below.

numpy.tanh(x, out=None, where=True, casting='same_kind', order='K', subok : [bool, datatype])

The parameters used in the above snippet function as follows:

  • x: It can be a variable containing a value in Radian or it may be an array containing some value
  • out: A location in which the result is to be stored. If provided, it must have a shape similar to the input X. If not provided or None, a fresh value or result is returned. It’s optional.
  • where = true:  Where the condition is True, We must get our result or output for “where = False” We will not get any result. It’s optional. y default its value is True.
  • casting=’same_kind’: It means only float64 to float32 value or results are allowed. The function shout cast values in this range or datatype.
  • order = ‘K’: It always results in an output array in the form of k ordered. (Note: There are 4 types of orders as {‘K’, ‘C’, ‘F’, ‘A’}). It’s optional.
  • subok: [bool, datatype] to make a subclass of the result or not. if True then provide a name for the subclass. It returns an array with the same shape and type as a given array. It’s also optional.

Now, We will see how to implement this function by passing different arguments in different methods.

Method 1: Passing a single-valued parameter

We are going to pass a single input for x (i.e. math.pi/5) and loading our result into a variable named output.

import numpy as np
import math
  
output = np.tanh(math.pi/5)

We can see our resultant output by printing the same.

print(output)

    0.5568933069002107

Method 2: Passing multi-valued parameter

In this method, We are going to pass multi-input values for x in the form of an array(i.e. 0, math.pi / 2, math.pi, 3 * math.pi/2, 2 * math.pi) and loading our result into a variable named another_output.

import numpy as np
import math
input = [0, math.pi / 2, math.pi, 3 * math.pi/2, 2 * math.pi]  
another_output = np.tanh(input)

You can see that we have created an array of radians named input and passed the same in our Numpy.tanh() method. We got our resultant array loaded into the variable another_output. let’s print the same.

print(another_output)

     [0.         0.91715234 0.99627208 0.99983861 0.99999303]

Method 3: Providing an Optional Output Variable  

Let us recall a small thing. When we were discussing the syntax and parameters for our NumPy.tanh() method. There was a parameter that is “out”. It is a location in which the result is to be stored. If provided, it must have a shape similar to the input x. If not provided or None, a fresh value or result is returned. It’s optional. In this method, We will see how to try this parameter.

In this code snippet, We are going to provide a “optional output” variable in which the output/result will be loaded. Have a look below at how it was implemented.

import numpy as np
import math

#passing our optional_output array where the result is to be loaded
optional_output = np.array([0, 0, 0, 0, 0], dtype='d')
np.tanh(input, optional_output)

#implementing the same and loading the result into the different_array
different_output = np.tanh(input)

You can see that We had created an array named ” optional_output ” which is similar in shape to our input array. In our, numpy.tanh() method, we had passed the same. The resultant array will be loaded into this array. Let us print it and see how it works.

print(optional_output)
print(different_output)
 
     [0.         0.91715234 0.99627208 0.99983861 0.99999303]
     [0.         0.91715234 0.99627208 0.99983861 0.99999303]

Method 4: Passing a Complex variable as a parameter

Let us pass a complex variable as the parameter and see the result for the same.

import numpy as np
import math
import cmath

a = 2.0
b = 3.0
z = complex(a,b)
c = np.tanh(z)

To use a complex variable we need to import a library named cmath. Then using the complex() method to get our complex variable ready and pass the same in our numpy.tanh() method We will get our result. Let’s print our results.

print(z)
print(c)
     
     (2+3j)
     (0.965385879022133-0.009884375038322492j)

Representation of Numpy.tanh() in graph

output_array = np.tanh(input)
print(output_array)

import matplotlib.pyplot as plt
plt.xlabel("input")
plt.ylabel("output_array")
plt.plot(input, output_array, color = 'green', marker = "*")

As you all know, to plot something on the graph, we need to import an additional library called Matplotlib. We imported the same and passed the input array created in method 2. We got our output loaded into the “output_array” After the implementation of our function. After all, we represented our function using the input and output array and got the result as below.

Conclusion

Finally, We covered our article today. We tried four different methods by passing four different types of arguments. In this way, we can implement other hyperbolic functions. You can try them all by using the same code snippets and substituting the function syntax with our required hyperbolic function. Else We will visit again for the same. Thank you.