NumPy Hyperbolic Functions

numpy
Published

June 25, 2023

Understanding Hyperbolic Functions

Hyperbolic functions are counterparts to trigonometric functions, but instead of being defined using a unit circle, they’re defined using a unit hyperbola. The key hyperbolic functions are:

  • sinh(x) (Hyperbolic Sine): Defined as (e^x - e^-x) / 2
  • cosh(x) (Hyperbolic Cosine): Defined as (e^x + e^-x) / 2
  • tanh(x) (Hyperbolic Tangent): Defined as sinh(x) / cosh(x) = (e^x - e^-x) / (e^x + e^-x)
  • coth(x) (Hyperbolic Cotangent): Defined as cosh(x) / sinh(x) = (e^x + e^-x) / (e^x - e^-x)
  • sech(x) (Hyperbolic Secant): Defined as 1 / cosh(x) = 2 / (e^x + e^-x)
  • csch(x) (Hyperbolic Cosecant): Defined as 1 / sinh(x) = 2 / (e^x - e^-x)

NumPy’s Implementation

NumPy provides efficient vectorized implementations of these functions, allowing for rapid computations on arrays. Let’s explore some examples:

import numpy as np

x = 2
sinh_x = np.sinh(x)
cosh_x = np.cosh(x)
tanh_x = np.tanh(x)
print(f"sinh({x}): {sinh_x}")
print(f"cosh({x}): {cosh_x}")
print(f"tanh({x}): {tanh_x}")

x_array = np.array([1, 2, 3, 4, 5])
sinh_array = np.sinh(x_array)
cosh_array = np.cosh(x_array)
tanh_array = np.tanh(x_array)
print("\nHyperbolic functions on an array:")
print(f"sinh(x_array): {sinh_array}")
print(f"cosh(x_array): {cosh_array}")
print(f"tanh(x_array): {tanh_array}")


#Example using other hyperbolic functions
x_array2 = np.array([1, 2, 3])
coth_array = np.cosh(x_array2)/np.sinh(x_array2)
sech_array = 1/np.cosh(x_array2)
csch_array = 1/np.sinh(x_array2)
print("\nExample with coth, sech and csch:")
print(f"coth(x_array): {coth_array}")
print(f"sech(x_array): {sech_array}")
print(f"csch(x_array): {csch_array}")

This code demonstrates how easily you can apply NumPy’s hyperbolic functions to both single values and arrays. The vectorized nature of NumPy significantly speeds up calculations compared to manually looping through elements.

Applications of Hyperbolic Functions

Hyperbolic functions appear in various applications:

  • Catenary Curves: Describing the shape of a hanging cable.
  • Special Relativity: Calculations involving velocity and time dilation.
  • Signal Processing: Analyzing and modeling signals.
  • Fluid Dynamics: Solving certain types of differential equations.

Beyond the Basics

NumPy’s hyperbolic functions seamlessly integrate with other NumPy functionalities, allowing you to combine them with array manipulations, linear algebra operations, and more for complex scientific computations. Experiment with different inputs and explore their behavior to fully grasp their potential.