NumPy: Math Functions
Last updated: 2026-08-26
1. What You'll Learn
- ❶ Trigonometric functions: sin, cos, tan, arcsin, arccos, arctan2
- ❷ Exponential and logarithmic: exp, log, log2, log10
- ❸ Rounding: floor, ceil, round, trunc, rint
- ❹ Special functions: sqrt, square, sign, abs, clip
2. Key Concepts
PYTHON
import numpy as np
# Trigonometric
angles = np.array([0, np.pi/4, np.pi/2])
print(np.sin(angles)) # [0. 0.707 1.]
print(np.cos(angles)) # [1. 0.707 0.]
print(np.tan(angles)) # [0. 1. inf]
# arctan2: safe angle computation
print(np.arctan2(1, 1)) # 0.785 (pi/4)
# Exponential and log
print(np.exp([0, 1, 2])) # [1. 2.718 7.389]
print(np.log([1, 10, 100])) # [0. 2.303 4.605]
print(np.log10([1, 10, 100])) # [0. 1. 2.]
# Rounding
a = np.array([1.2, 1.7, -1.2, -1.7])
print(np.floor(a)) # [1. 1. -2. -2.]
print(np.ceil(a)) # [2. 2. -1. -1.]
print(np.round(a)) # [1. 2. -1. -2.]
print(np.trunc(a)) # [1. 1. -1. -1.]
# Clipping
data = np.array([1, 5, 10, 15, 20])
print(np.clip(data, 5, 15)) # [5 5 10 15 15]
TEXT
📖 Display only
> **Output:** Run NumPy 2.x in your local Python environment to see the ndarray output. The Piston server does not have NumPy pre-installed — install it locally (`pip install numpy`) and compare. Actual values may vary by NumPy version and random seed.
▶ Example: Trigonometric functions (Difficulty ⭐)
PYTHON
import numpy as np
angles = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2])
print("Angles:", angles)
print("sin:", np.sin(angles).round(4))
print("cos:", np.cos(angles).round(4))
print("tan:", np.tan(angles).round(4))
# Inverse trig
print("arcsin(0.5):", np.arcsin(0.5))
print("arccos(0.5):", np.arccos(0.5))
Output:
TEXT 📖 Display onlyAngles: [0. 0.52359878 0.78539816 1.04719755 1.57079633] sin: [0. 0.5 0.7071 0.866 1. ] cos: [1. 0.866 0.7071 0.5 0. ] tan: [0. 0.5774 1. 1.7321 inf] arcsin(0.5): 0.5235987755982989 arccos(0.5): 1.0471975511965979
▶ Example: Exponential and logarithmic functions (Difficulty ⭐⭐)
PYTHON
import numpy as np
x = np.array([1, 2, 3, 4, 5])
print("exp:", np.exp(x).round(2))
print("log:", np.log(x).round(2))
print("log2:", np.log2(x).round(2))
print("log10:", np.log10(x).round(2))
# Verify: exp(log(x)) ≈ x
restored = np.exp(np.log(x))
print("exp(log(x)):", restored.round(6))
print("Match:", np.allclose(x, restored))
Output:
TEXT 📖 Display onlyexp: [ 2.72 7.39 20.09 54.6 148.41] log: [0. 0.69 1.1 1.39 1.61] log2: [0. 1. 1.58 2. 2.32] log10: [0. 0.3 0.48 0.6 0.7 ] exp(log(x)): [1. 2. 3. 4. 5.] Match: True
▶ Example: Rounding and clipping (Difficulty ⭐)
PYTHON
import numpy as np
a = np.array([1.2, 1.7, -1.2, -1.7, 2.5, -2.5])
print("floor:", np.floor(a))
print("ceil:", np.ceil(a))
print("trunc:", np.trunc(a))
print("round:", np.round(a))
# Clipping
data = np.array([-10, 5, 15, 20, 100, -5])
clipped = np.clip(data, 0, 50)
print("Original:", data)
print("Clipped [0, 50]:", clipped)
Output:
TEXT 📖 Display onlyfloor: [ 1. 1. -2. -2. 2. -3.] ceil: [ 2. 2. -1. -1. 3. -2.] trunc: [ 1. 1. -1. -1. 2. -2.] round: [ 1. 2. -1. -2. 2. -2.] Original: [-10 5 15 20 100 -5] Clipped [0, 50]: [ 0 5 15 20 50 0]
Q Are NumPy math functions faster than Python's math module?
A Yes — for arrays, NumPy is vectorized (C-level). For single values, Python's math module is slightly faster. Use NumPy for arrays, Python's math for scalars.
Q What's the difference between np.round and np.around?
A They're identical —
np.around is an alias for np.round. Both round to the nearest integer (or specified decimals).Q What is np.clip used for?
A Clipping limits values to a range — useful for removing outliers, clamping pixel values, or ensuring numerical stability.
❓ FAQ
Q What is the most important thing to remember?
A NumPy operations are vectorized — avoid Python loops for better performance.
Q Where can I learn more?
A Check the official NumPy documentation at numpy.org for detailed references and advanced topics.
Q Does this work with NumPy 2.x?
A Yes — all examples are compatible with NumPy 2.x. Some older APIs (like np.random.seed) are still supported but the modern alternatives are recommended.
📖 Summary
- Trig: sin, cos, tan, arcsin, arccos, arctan, arctan2
- Exp/log: exp, log, log2, log10, expm1, log1p
- Rounding: floor, ceil, round, trunc, rint
- Clip:
np.clip(data, min, max)— limits to range - All functions are vectorized and NumPy-aware
📝 Exercises
-
Beginner (Difficulty ⭐): Create an array of angles from 0 to 2π at π/4 intervals. Compute sin, cos, and tan for each.
-
Intermediate (Difficulty ⭐⭐): Create an array of 100 values between 0.1 and 10. Compute log10, ln, and exp for each. Verify that exp(log(x)) ≈ x.
-
Advanced (Difficulty ⭐⭐⭐): Use
np.clipto remove outliers from a dataset — replace values below the 5th percentile with the 5th percentile value, and values above the 95th percentile with the 95th percentile value.