Skip to content
Related Articles

Related Articles

Compute the inverse hyperbolic tangent with scimath in Python

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 04 May, 2022

In this article, we will cover how to compute the inverse hyperbolic tangent with scimath in Python using NumPy.

numpy.emath.arctanh method

The inverse hyperbolic tangent is also called as arctanh or tanh-1. To compute the inverse hyperbolic tangent Python provides a method called arctanh which is present in numpy.emath package. arctanh method accepts an array of numbers that may be real, complex, and returns the principal value of arctanh(x). The output of the arctanh method depends on the input array elements. If x=1 then it returns Infinity. If x=-1 then it returns -Infinity. If the absolute value of x is greater than 1 i.e., abs(x)>1, or for complex numbers, a result is always a complex number.  Below is the syntax of emath.arctanh.

Syntax: numpy.emath.arctanh(x, out=None)

Parameters

  • x- It is an input array. 
  • out- It specifies the location into which result is stored. It is an optional parameter.

Returns an array of the same shape as input array x. Resultant array consists principal values.

Example 1

In the below code we passed an input array consisting of values >1 to the arctanh method. As all the abs(x)>1 the arctanh method returns array of complex numbers.

Python3




# import necessary packages
import numpy as np
 
# Create an input array
x = np.array([2, 3, 4])
 
# input array before computation
print("Input array->", x)
 
# compute the inverse hyperbolic tangent
# using arctanh method
print("Resultant Array->", np.emath.arctanh(x))


Output

Input array-> [2 3 4]

Resultant Array-> [0.54930614+1.57079633j 0.34657359+1.57079633j 0.25541281+1.57079633j]

Example 2

Here we passed an input array consisting 1,-1 to arctanh method. For the values of 1,-1 arctanh method returns Infinity and -Infinity values as a result respectively.

Python3




# import necessary packages
import numpy as np
 
# Create an input array
x = np.array([1, -1])
 
# input array before computation
print("Input array->", x)
 
# compute the inverse hyperbolic tangent using
# arctanh method
print("Resultant Array->", np.emath.arctanh(x))


Output

Input array-> [ 1 -1]
Resultant Array-> [ inf -inf]

Example 3

Python3




# import necessary packages
import numpy as np
 
# Create an input array
x = np.array([-1, 0, 1, 5])
 
# input array before computation
print("Input array->", x)
 
# compute the inverse hyperbolic tangent
# using arctanh method
print("Resultant Array->", np.emath.arctanh(x))


Output

Input array-> [-1  0  1  5]

Resultant Array-> [-inf+0.j    0.+0.j    inf+0.j

    0.20273255+1.57079633j]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!