w3resource

NumPy: Calculate hyperbolic sine, cosine, and tangent for all elements in a given array


25. Hyperbolic Trigonometric Functions

Write a NumPy program to calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent for all elements in a given array.

Sample Input: Input: Input: [-1., 0, 1.]

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Creating an array of values: [-1, 0, 1]
x = np.array([-1., 0, 1.])

# Computing hyperbolic sine (sinh) of each element in the array using np.sinh()
print(np.sinh(x))

# Computing hyperbolic cosine (cosh) of each element in the array using np.cosh()
print(np.cosh(x))

# Computing hyperbolic tangent (tanh) of each element in the array using np.tanh()
print(np.tanh(x)) 

Sample Output:

[-1.17520119  0.          1.17520119]
[1.54308063 1.         1.54308063]
[-0.76159416  0.          0.76159416]

Explanation:

x = np.array([-1., 0, 1.]): This code creates a NumPy array x with the values [-1., 0, 1.].

np.sinh(x) - The np.sinh(x) function calculates the hyperbolic sine of x, which is defined as (e^x - e^(-x))/2. For the given input, the output would be [-1.17520119e+00, 0.00000000e+00, 1.17520119e+00].

np.cosh(x) - The np.cosh(x) function calculates the hyperbolic cosine of x, which is defined as (e^x + e^(-x))/2. For the given input, the output would be [1.54308063, 1., 1.54308063].

np.tanh(x) - The np.tanh(x) function calculates the hyperbolic tangent of x, which is defined as sinh(x)/cosh(x). For the given input, the output would be [-0.76159416, 0., 0.76159416].


For more Practice: Solve these Related Problems:

  • Implement a function that computes the hyperbolic sine, cosine, and tangent for an array using np.sinh, np.cosh, and np.tanh.
  • Test the function on an array of negative, zero, and positive values to verify symmetry and correctness.
  • Create a solution that returns the three hyperbolic functions as separate arrays for clarity.
  • Compare the hyperbolic tangent output with the ratio of sinh to cosh for each element to ensure consistency.

Go to:


PREV : Convert Degrees to Radians
NEXT : Rounding, Floor, Ceil, and Truncation

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.