Hyperbolic & Logarithmic Functions in NumPy
NumPy offers a comprehensive set of hyperbolic and logarithmic functions to support advanced mathematical computations. These functions operate element-wise on arrays and are optimized for performance.
Whether you are analyzing exponential growth, decay, or working with complex signal processing, NumPy's hyperbolic and logarithmic tools provide efficient and reliable calculations.
- Hyperbolic Functions: Includes sinh(), cosh(), tanh(), and their inverses.
- Logarithmic Functions: Provides log(), log10(), log2(), and natural logarithms.
These functions are essential for applications in calculus, machine learning, scientific simulations, and more.
Hyperbolic Functions in NumPy
NumPy provides a variety of hyperbolic functions that compute element-wise hyperbolic sine, cosine, tangent, and their inverses. These functions are useful in areas such as calculus, physics, and engineering.
Hyperbolic functions are similar to trigonometric functions but based on hyperbolas rather than circles. NumPy implements these with high performance on arrays.
import numpy as np
x = np.array([-1, 0, 1])
# Hyperbolic functions
print("sinh:", np.sinh(x)) # [-1.17520119 0. 1.17520119]
print("cosh:", np.cosh(x)) # [1.54308063 1. 1.54308063]
print("tanh:", np.tanh(x)) # [-0.76159416 0. 0.76159416]
# Inverse hyperbolic functions
print("arcsinh:", np.arcsinh(x)) # [-0.88137359 0. 0.88137359]
print("arccosh:", np.arccosh(np.array([1, 2, 3]))) # [0. 1.3169579 1.76274717]
print("arctanh:", np.arctanh(np.array([-0.5, 0, 0.5]))) # [-0.54930614 0. 0.54930614]
import numpy as np
x = np.array([-1, 0, 1])
# Hyperbolic functions
print("sinh:", np.sinh(x)) # [-1.17520119 0. 1.17520119]
print("cosh:", np.cosh(x)) # [1.54308063 1. 1.54308063]
print("tanh:", np.tanh(x)) # [-0.76159416 0. 0.76159416]
# Inverse hyperbolic functions
print("arcsinh:", np.arcsinh(x)) # [-0.88137359 0. 0.88137359]
print("arccosh:", np.arccosh(np.array([1, 2, 3]))) # [0. 1.3169579 1.76274717]
print("arctanh:", np.arctanh(np.array([-0.5, 0, 0.5]))) # [-0.54930614 0. 0.54930614]
How It Works:
- np.sinh(x): Computes the hyperbolic sine of each element.
- np.cosh(x): Computes the hyperbolic cosine.
- np.tanh(x): Computes the hyperbolic tangent.
- np.arcsinh(x): Computes the inverse hyperbolic sine.
- np.arccosh(x): Computes the inverse hyperbolic cosine (input values must be ≥ 1).
- np.arctanh(x): Computes the inverse hyperbolic tangent (input values must be between -1 and 1).
Output
sinh: [-1.17520119 0. 1.17520119]
cosh: [1.54308063 1. 1.54308063]
tanh: [-0.76159416 0. 0.76159416]
arcsinh: [-0.88137359 0. 0.88137359]
arccosh: [0. 1.3169579 1.76274717]
arctanh: [-0.54930614 0. 0.54930614]
sinh: [-1.17520119 0. 1.17520119]
cosh: [1.54308063 1. 1.54308063]
tanh: [-0.76159416 0. 0.76159416]
arcsinh: [-0.88137359 0. 0.88137359]
arccosh: [0. 1.3169579 1.76274717]
arctanh: [-0.54930614 0. 0.54930614]
💡 Tip: Inverse hyperbolic functions have domain restrictions — make sure your inputs fall within valid ranges to avoid warnings or NaNs.
Logarithmic Functions in NumPy
NumPy provides several logarithmic functions to perform element-wise computations of natural logarithms, base-10, base-2, and logarithms with arbitrary bases.
Logarithms are essential in fields like data science, statistics, and scientific computing to transform data and solve exponential equations.
import numpy as np
x = np.array([1, np.e, 10, 100])
# Natural logarithm (base e)
print("log:", np.log(x))
# Base-10 logarithm
print("log10:", np.log10(x))
# Base-2 logarithm
print("log2:", np.log2(x))
# Logarithm with arbitrary base (e.g., base 3)
base = 3
print("log base 3:", np.log(x) / np.log(base))
import numpy as np
x = np.array([1, np.e, 10, 100])
# Natural logarithm (base e)
print("log:", np.log(x))
# Base-10 logarithm
print("log10:", np.log10(x))
# Base-2 logarithm
print("log2:", np.log2(x))
# Logarithm with arbitrary base (e.g., base 3)
base = 3
print("log base 3:", np.log(x) / np.log(base))
How It Works:
- np.log(x): Computes natural logarithm (base e) element-wise.
- np.log10(x): Computes base-10 logarithm.
- np.log2(x): Computes base-2 logarithm.
- Logarithm with any base can be calculated using np.log(x) / np.log(base).
Output
log: [0. 1. 2.30258509 4.60517019]
log10: [0. 1. 1. 2.]
log2: [0. 1.44269504 3.32192809 6.64385619]
log base 3: [0. 0.91023923 2.09590327 4.19180654]
log: [0. 1. 2.30258509 4.60517019]
log10: [0. 1. 1. 2.]
log2: [0. 1.44269504 3.32192809 6.64385619]
log base 3: [0. 0.91023923 2.09590327 4.19180654]
💡 Tip: Inputs must be positive numbers, as logarithms of zero or negative values are undefined and will produce warnings or NaNs.
Frequently Asked Questions
What are hyperbolic functions in NumPy?
What are hyperbolic functions in NumPy?
Hyperbolic functions in NumPy include sinh, cosh, tanh and their inverses arcsinh, arccosh, and arctanh. They are based on hyperbolas and are used in calculus, physics, and engineering.
How do I calculate the natural logarithm in NumPy?
How do I calculate the natural logarithm in NumPy?
Use np.log() to calculate the natural logarithm (base e) of each element in an array. Make sure all values are positive to avoid runtime warnings or invalid results.
What is the difference between np.log(), np.log10(), and np.log2()?
What is the difference between np.log(), np.log10(), and np.log2()?
np.log() calculates the natural logarithm (base e), np.log10() computes the base-10 log, and np.log2() computes the base-2 log. All are applied element-wise.
Are there input restrictions for inverse hyperbolic functions?
Are there input restrictions for inverse hyperbolic functions?
Yes. np.arccosh() requires inputs greater than or equal to 1, and np.arctanh() accepts values strictly between -1 and 1. Violating these ranges may result in NaNs.
How can I compute logarithms with a custom base in NumPy?
How can I compute logarithms with a custom base in NumPy?
You can compute custom base logarithms using the identity np.log(x) / np.log(base). This allows for base-3, base-5, or any logarithmic base transformations.
What's Next?
Next, we’ll dive into Statistics and Complex Number Functions in NumPy — essential tools for numerical precision and working with complex-valued data in scientific computing and engineering.