Numpy sinh – Hyperbolic sine, element-wise

(467)

You must have heard about hyperbolic functions. If not, you might be familiar with trigonometric functions such as sine, cosine, tangent, cotangent, secant, and cosecant and the others like hypotenuse, base, and perpendicular.

Also read: NumPy Tan – A Complete Guide

Today we are going to learn about the sine hyperbolic function. Before getting into it let us understand what is a hyperbolic function.

What is a hyperbolic function?

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 or angles) but a hyperbolic function defines the values in a hyperbola rather than a circle.

The points form the right half of the unit hyperbola. some important formulas are depicted below for hyperbolic functions. Let us look at them.

Sinh X
Cosh X
Tanh X 2

What is hyperbolic sin or sinh?

These are the three important hyperbolic functions as well. And today we are going to learn the hyperbolic sine function.

We can represent the sinh function in the form of code as well below.

sinh (x) = 1/2 * (np.exp(x) - np.exp(-x))

Working with numpy.sinh()

In our code snippet, we are not going to use the above (we are not going to use the formula to write). There is a predefined function for hyperbolic sine as well which is NumPy.sin() provided by the NumPy library in python. We are going to use the same. In this tutorial, we will learn how to find hyperbolic sine functions for Radian and complex values as well.

Syntax of numpy.sinh()

Let us have a quick look at the syntax below.

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

In the above syntax We are passing some arguments 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 are going to implement the same method or function in our code snippet.

np.sinh() for single input

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

#importing required modules
import numpy as np
import math
  
output = np.sinh(math.pi/3)
output

The above code should give the output as follow.

1.2493670505239751

np.sinh() for multi-input or array

We are going to pass five inputs 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 and printing the same.

#providig multi input in the form of array
import numpy as np
import math
input = [0, math.pi / 2, math.pi, 3 * math.pi/2, 2 * math.pi]  
another_output = np.sinh(input)
another_output

The above code should give the output as follow.

array([  0.        ,   2.3012989 ,  11.54873936,  55.6543976 ,
       267.74489404])

Providing an Optional Output Variable 

Let us recall a small thing. When we were discussing the syntax and parameters for our NumPy.cosh() 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 an 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

optional_output = np.array([0, 0, 0, 0, 0], dtype='d')
np.sinh(input, optional_output)
different_output = np.sinh(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.sinh() method, we had passed this array as the value for “out”.

The resultant array will be loaded into this array. By printing the same We can have our result.

print(optional_output)
print(different_output)
    
     [  0.           2.3012989   11.54873936  55.6543976  267.74489404]
     [  0.           2.3012989   11.54873936  55.6543976  267.74489404]

np.sinh() for complex input

Now in our last code snippet, We are going to input a complex no. and get the hyperbolic sine result. Let’s have a quick look below.

import numpy as np
import math
import cmath

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

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

print(z)
     (2+3j)
   
print(c)
     (-3.59056458998578+0.5309210862485197j)

Plotting Numpy.sinh() on a graph

import numpy as np
import math
import matplotlib.pyplot as plt

input = [0, math.pi / 2, math.pi, 3 * math.pi/2, 2 * math.pi]  
output_array = np.sinh(input)
 
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 are passing the multivalued array named input in the function implementation. 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 below.

Summary

Like this way, We have completed our article today. We learned about the method along with its parameters. We practiced our code snippet by passing various values and getting our expected results. In this way, we can have the same for hyperbolic cosines and hyperbolic tangents as well. We must visit again with these exciting topics. Thank you.