Numpy cosh – Hyperbolic cosine, element-wise

(467)

Today We are going to learn an interesting topic which is “How to implement the Hyperbolic cosine function using Python”. You must be aware of all trigonometric functions and related terminologies as well as their corresponding angle concepts earlier. You must have solved trigonometric problems when you are being a kid.

Also read: Numpy sinh – Hyperbolic sine, element-wise

Exactly the same as what we are going to implement today. The only difference is that instead of implementing it on pen and paper, we are going to implement the same on our computer using an in-built method with their appropriate module on our python IDLE shell. Let us get started.

What is a hyperbolic trigonometric function?

First of all, Let us understand what is a Hyperbolic trigonometric 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.

Some 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 cosine?

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

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

Working with numpy.cosh()

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

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

numpy.cosh(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.cosh(math.pi/5)

We can see our resultant output by printing the same.

print(output)
   
    1.2039720893382206

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.cosh(input)

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

print(another_output)

   [  1.           2.50917848  11.59195328  55.66338089 267.74676148]

Method 3: 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 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
optional_output = np.array([0, 0, 0, 0, 0], dtype='d')

#passing our optional_output array where the result should be loaded
np.cosh(input, optional_output)

#implementing the same and loading the result into the different_array
different_output = np.cosh(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.cosh() 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)
      
     [  1.           2.50917848  11.59195328  55.66338089 267.74676148]
     [  1.           2.50917848  11.59195328  55.66338089 267.74676148]

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.cosh(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.cosh() method We will get our result. Let’s print our results.

print(z)
print(c)

     (2+3j)
     (-3.7245455049153224+0.5118225699873846j)

Plotting Numpy.cosh() on a graph

output_array = np.cosh(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 below.

Summary

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.