Numpy arctanh – Inverse hyperbolic tangent element-wise

INVERSE HYPERBOLIC FUNCTION

Today, we will learn how to implement the inverse hyperbolic function element-wise. This will include seeing the formula, the function, and the required library. We will also learn the domain and range for this function. Finally, we will have some examples of implementing our function in our code snippet.

Also read: NumPy Arctan – A Complete Guide

What is the inverse tangent hyperbolic function?

The inverse tangent hyperbolic function takes an input of x and returns an output of y such that tanh(y) = x. It is the inverse of the tangent hyperbolic function. The formula for both the tangent and inverse tangent hyperbolic function is seen below.

Tan Inverse Hyperbolic
Tanh X 4

You may have known that the domain and range for tanh(y) = x are [-∞, ∞] and [-1,1] respectively.

As we are taking the inverse function today. The domain for our function will be [-1,1] and the range will be [-∞, ∞]. The graph for this function is depicted below.

Tanh Inverse Graph
Tanh Inverse Graph

Syntax of NumPy.arctanh()

numpy.arctanh(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.

Implementing the numpy.arctanh() method

Now, we will see how to implement this function using the two examples below in our code snippets and how to represent our function in the graph.

Passing values in the domain range[-1,1]

import numpy as np
import math
input = [-0.15, -0.75, 0, 0.25, 0.75]  
output1 = np.arctanh(input)
output2 = np.arctanh(0.586)
print(output1)
print(output2)

#output
     [-0.15114044 -0.97295507  0.          0.25541281  0.97295507]
     0.6715522141847394

We have successfully implemented our method in the code snippet above. We imported the required module, Numpy, created an array of input values, and passed the same into our function. The result was then loaded into output1. We again passed a single value into the function and loaded the result into output2, which we printed.

Passing values outside the domain range[-1,1]

import numpy as np
input = [-1.25, 5.25]  
output = np.arctanh(input)
print(output)
 
#output
     [nan nan]
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:5: RuntimeWarning: invalid value encountered in arctanh
  """

As we can see We had passed two values outside the domain range, but instead of implementing the function it throws the error “invalid value encountered in arctanh”. It could not process these values.

Plotting numpy.arctanh() on a graph

import matplotlib.pyplot as plt
import numpy
x=[]
y=[]
i=-0.99
while (i<1):
    x.append(i)
    y.append(numpy.arctanh(i))
    i=i+0.01

plt.xlabel("Domain = [-1,1]")
plt.ylabel("Range = [-∞, ∞]")
plt.grid(linestyle='-', linewidth=0.5,color='red')
plt.plot(x,y, linewidth=3, color = 'black')

We are creating two arrays of x and y as well. We are taking values in the x array starting from -0.99 and gradually increasing by 0.01 till 1.00. Loading all the respective resultant values into the y array after implementing the function throughout the loop. Then plotting the same using the above-obtained arrays will give the result as depicted below.

Conclusion

Finally, we have completed our topic today, Hope you guys have understood it with your code snippets. You can much better understand by passing on some different types of arguments and seeing the result.