NumPy: Math Functions

Last updated: 2026-08-26

1. What You'll Learn



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 only
Angles: [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 only
exp: [  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 only
floor: [ 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



📝 Exercises

  1. Beginner (Difficulty ⭐): Create an array of angles from 0 to 2π at π/4 intervals. Compute sin, cos, and tan for each.

  2. 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.

  3. Advanced (Difficulty ⭐⭐⭐): Use np.clip to 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.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏