Skip to main content
Pandas & NumPy
CHAPTER 06 Beginner

NumPy Mathematical Functions

Updated: May 18, 2026
5 min read

# CHAPTER 6

NumPy Mathematical Functions

1. Chapter Introduction

NumPy provides over 60 universal functions (ufuncs) that operate element-wise on arrays at C-speed. From basic arithmetic to advanced linear algebra, these functions are the computational engine behind scientific Python.

2. Universal Functions (ufuncs)

python
123456789101112131415
import numpy as np

arr = np.array([1, 4, 9, 16, 25])

# Basic math
print(np.sqrt(arr))     # [1. 2. 3. 4. 5.]
print(np.square(arr))   # [1 16 81 256 625]
print(np.abs([-3, -1, 2, -5]))   # [3 1 2 5]
print(np.power(arr, 0.5))        # Same as sqrt

# Rounding
data = np.array([1.567, 2.345, 3.891, 4.212])
print(np.round(data, 1))   # [1.6 2.3 3.9 4.2]
print(np.floor(data))      # [1. 2. 3. 4.]
print(np.ceil(data))       # [2. 3. 4. 5.]

3. Trigonometric Functions

python
1234567891011
angles = np.array([0, 30, 45, 60, 90])
radians = np.radians(angles)   # Convert degrees to radians

print(np.sin(radians))  # [0.    0.5   0.707 0.866 1.   ]
print(np.cos(radians))  # [1.    0.866 0.707 0.5   0.   ]
print(np.tan(radians))  # [0.    0.577 1.    1.732 inf  ]

# Inverse trig
values = np.array([0, 0.5, 1])
print(np.degrees(np.arcsin(values)))  # [0. 30. 90.]
print(np.degrees(np.arccos(values)))  # [90. 60. 0.]

4. Logarithm and Exponential

python
1234567891011
x = np.array([1, np.e, np.e**2, 10, 100])

print(np.log(x))     # Natural log:   [0. 1. 2. 2.303 4.605]
print(np.log2(x))    # Base-2 log:    [0. 1.443 2.885 3.322 6.644]
print(np.log10(x))   # Base-10 log:   [0. 0.434 0.868 1.   2.   ]
print(np.exp([0, 1, 2, 3]))  # e^x: [1. 2.718 7.389 20.086]

# Log1p and expm1 for numerical stability near 0
small = np.array([0.001, 0.01, 0.1])
print(np.log1p(small))   # log(1+x) — more accurate than log(1+x)
print(np.expm1(small))   # exp(x)-1 — more accurate near 0

5. Linear Algebra

python
1234567891011121314151617181920212223
A = np.array([[2, 1], [5, 3]])
b = np.array([4, 7])

# Solve Ax = b
x = np.linalg.solve(A, b)
print(x)               # [5. -6.]

# Matrix properties
print(np.linalg.det(A))   # Determinant: 1.0
print(np.linalg.matrix_rank(A))  # Rank: 2

# Eigenvalues and eigenvectors
A_square = np.array([[4, 2], [1, 3]])
eigenvals, eigenvecs = np.linalg.eig(A_square)
print("Eigenvalues:", eigenvals)   # [5. 2.]

# Norm
v = np.array([3, 4])
print(np.linalg.norm(v))    # 5.0 (Euclidean distance)

# Matrix decomposition
U, S, Vt = np.linalg.svd(A)  # Singular Value Decomposition
print("Singular values:", S)

6. Aggregation Functions

python
123456789101112131415161718192021
data = np.array([[4, 2, 7],
                 [1, 8, 3],
                 [9, 5, 6]])

print(np.min(data))          # 1 (overall)
print(np.max(data))          # 9 (overall)
print(np.sum(data))          # 45 (overall)
print(np.prod([1,2,3,4]))    # 24 (product)
print(np.cumsum(data.flatten()))   # Running cumulative sum

# Along axes
print(np.min(data, axis=0))  # Column mins: [1 2 3]
print(np.max(data, axis=1))  # Row maxes: [7 8 9]
print(np.ptp(data))          # Peak-to-peak (max-min): 8

# Percentiles and quantiles
sales = np.array([120, 340, 280, 510, 90, 450, 220, 380])
print(np.percentile(sales, 25))   # 25th percentile (Q1)
print(np.percentile(sales, 50))   # Median
print(np.percentile(sales, 75))   # 75th percentile (Q3)
print(np.percentile(sales, [0, 25, 50, 75, 100]))  # All quartiles

7. Common Mistakes

  • Degrees vs radians: np.sin(90) does NOT give 1.0 — it computes sin(90 radians). Always convert with np.radians(90) first.
  • log(0) is undefined: Returns -inf with a warning. Check for zeros before applying log.

8. MCQs

Question 1

np.radians(90) converts?

Question 2

np.sqrt(arr) applies?

Question 3

np.linalg.solve(A, b) solves?

Question 4

np.percentile(arr, 75) returns?

Question 5

np.log(x) computes?

Question 6

np.linalg.norm(v) for [3,4] returns?

Question 7

np.floor(3.7) returns?

Question 8

ufunc stands for?

Question 9

np.ptp(arr) computes?

Question 10

np.linalg.eig(A) returns?

9. Interview Questions

  • Q: What are NumPy universal functions (ufuncs)?
  • Q: How do you solve a system of linear equations with NumPy?

10. Summary

NumPy's mathematical functions cover every scientific computing need — trigonometry, logarithms, exponentials, linear algebra, and statistical aggregations. All operate element-wise on arrays at C-speed. Always work in radians for trigonometric functions and check for edge cases like log(0).

11. Next Chapter Recommendation

In Chapter 7: NumPy Broadcasting and Vectorization, we master one of NumPy's most powerful features — performing operations on arrays of different shapes without loops.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·