6️⃣🧮 NumPy ufunc (Universal Functions)
Estimated reading: 3 minutes 58 views

🌐 NumPy ufunc Hyperbolic – Compute sinh, cosh, tanh & Their Inverses Efficiently

🧲 Introduction – Why Learn Hyperbolic ufuncs in NumPy?

Hyperbolic functions are analogs of trigonometric functions but are based on exponential curves. They are widely used in calculus, physics, machine learning (especially tanh activation), and engineering—especially when modeling waveforms, relativistic equations, and growth curves.

NumPy provides fast, vectorized hyperbolic ufuncs for working with:

  • Hyperbolic sine: sinh()
  • Hyperbolic cosine: cosh()
  • Hyperbolic tangent: tanh()
  • Inverse hyperbolic functions: arcsinh(), arccosh(), arctanh()

🎯 By the end of this guide, you’ll:

  • Use NumPy’s hyperbolic ufuncs element-wise on arrays
  • Understand the relationships and use cases of sinh, cosh, and tanh
  • Calculate inverse hyperbolic values
  • Visualize and validate function outputs

🔢 Step 1: Basic Hyperbolic Functions

import numpy as np

x = np.array([0, 1, 2])

print("sinh:", np.sinh(x))
print("cosh:", np.cosh(x))
print("tanh:", np.tanh(x))

👉 Output:

sinh: [0.         1.17520119 3.62686041]
cosh: [1.         1.54308063 3.76219569]
tanh: [0.         0.76159416 0.96402758]

🔍 Explanation:

  • sinh(x) = (e^x – e^−x) / 2
  • cosh(x) = (e^x + e^−x) / 2
  • tanh(x) = sinh(x) / cosh(x)
    ✅ These ufuncs are element-wise, fast, and broadcasting-compatible

📐 Step 2: Use on Multi-dimensional Arrays

matrix = np.array([[0.5, 1.0], [1.5, 2.0]])
print(np.tanh(matrix))

👉 Output:

[[0.46211716 0.76159416]
 [0.90514825 0.96402758]]

✅ Works with any array shape — just like NumPy’s other ufuncs


🧮 Step 3: Inverse Hyperbolic Functions

values = np.array([0.0, 1.0, 2.0])

print("arcsinh:", np.arcsinh(values))  # Inverse sinh
print("arccosh:", np.arccosh(values))  # Inverse cosh (x ≥ 1)
print("arctanh:", np.arctanh([0.0, 0.5, 0.9]))  # Inverse tanh (|x| < 1)

👉 Output:

arcsinh: [0.         0.88137359 1.44363548]
arccosh: [nan 0.         1.3169579 ]
arctanh: [0.         0.54930614 1.47221949]

📌 Domain Constraints:

  • arccosh(x) is only defined for x ≥ 1
  • arctanh(x) is defined for |x| < 1
    ✅ NumPy returns nan when the input is outside the valid domain

⚠️ Step 4: Handling Invalid Input Ranges

invalid = np.array([0.5, 0.9, 1.1])
print("arctanh:", np.arctanh(invalid))  # Last element will return nan

✅ Always check value ranges before using inverse hyperbolic functions


🧠 Real-World Applications of Hyperbolic Functions

FunctionUse Case Example
tanh()Activation function in neural networks
sinh()Solutions to hyperbolic PDEs (heat equations, etc.)
cosh()Catenary curve (shape of hanging cables)
arctanh()Signal decoding, entropy estimation
arccosh()Optical path, relativity equations

🧾 Bonus: Compose with Other ufuncs

x = np.linspace(-2, 2, 5)
result = np.exp(np.tanh(x))  # Apply exponential to tanh
print(result)

✅ Combine with exp, log, or square for complex mathematical modeling


📌 Summary – Recap & Next Steps

Hyperbolic ufuncs in NumPy give you fast, element-wise control over math expressions used in a wide range of scientific and machine learning workflows.

🔍 Key Takeaways:

  • Use sinh(), cosh(), tanh() for forward hyperbolic transforms
  • Use arcsinh(), arccosh(), arctanh() for inverse transforms
  • Always validate input domains for inverse functions
  • Works on any shape array, with support for broadcasting and chaining

⚙️ Real-world relevance: Used in neural networks, engineering, hyperbolic geometry, and physical simulations


❓ FAQs – NumPy Hyperbolic ufuncs

❓ What’s the difference between trig and hyperbolic functions?
✅ Trig functions are based on the unit circle; hyperbolic on the unit hyperbola.

❓ When is arccosh(x) undefined?
❌ For x < 1, arccosh(x) is undefined and returns nan.

❓ Can I use tanh() as an activation function?
✅ Yes. It’s a common alternative to ReLU in neural networks.

❓ Do hyperbolic ufuncs work on matrices?
✅ Yes. All NumPy ufuncs operate element-wise on arrays of any shape.

❓ What does tanh(large x) approach?
✅ It asymptotically approaches +1 for large positive x, −1 for large negative x.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

NumPy ufunc Hyperbolic

Or Copy Link

CONTENTS
Scroll to Top