CodeNewbie Community ๐ŸŒฑ

Cover image for ๐Ÿ“ NumPy Math Functions Cheat Sheet
Hichem MG
Hichem MG

Posted on

๐Ÿ“ NumPy Math Functions Cheat Sheet

NumPy is Pythonโ€™s go-to library for numerical computing, especially with large datasets and arrays. Its math functions are vectorized, which means you can apply operations to entire arrays without loops โ€” making them fast and powerful.

Why Use NumPy for Math?

  • Element-wise operations on arrays
  • Handles multi-dimensional data
  • Functions are vectorized (fast & efficient)
  • Seamless with machine learning, data analysis, and scientific computing

๐Ÿ“ฆ Importing NumPy

import numpy as np
Enter fullscreen mode Exit fullscreen mode

Note:

The following are the most-used functions only, checkout the complete list on numpy.org.

๐Ÿ”ข Arithmetic Functions

Function Description
np.add(x1, x2) Element-wise addition
np.subtract(x1, x2) Element-wise subtraction
np.multiply(x1, x2) Element-wise multiplication
np.divide(x1, x2) Element-wise division
np.power(x1, x2) x1 raised to the power of x2
np.mod(x1, x2) Element-wise modulo

๐Ÿ“ Trigonometric Functions

Function Description
np.sin(x) Sine
np.cos(x) Cosine
np.tan(x) Tangent
np.arcsin(x) Inverse sine
np.arccos(x) Inverse cosine
np.arctan(x) Inverse tangent
np.arctan2(y, x) Arctangent of y/x
np.hypot(x, y) โˆš(xยฒ + yยฒ)
np.degrees(x) Radians โ†’ degrees
np.radians(x) Degrees โ†’ radians

๐Ÿ“Š Exponentials & Logarithms

Function Description
np.exp(x) e ** x
np.expm1(x) e ** x - 1
np.log(x) Natural log (base e)
np.log2(x) Base-2 logarithm
np.log10(x) Base-10 logarithm
np.log1p(x) log(1 + x) (accurate for small x)

๐Ÿง  Rounding, Absolute, and Sign Functions

Function Description
np.round(x) Round to nearest integer
np.floor(x) Round down
np.ceil(x) Round up
np.trunc(x) Truncate decimal
np.abs(x) Absolute value
np.sign(x) Sign of x (-1, 0, or 1)

๐Ÿ“ Aggregates & Reductions

Function Description
np.sum(x) Sum of all elements
np.prod(x) Product of all elements
np.mean(x) Average value
np.std(x) Standard deviation
np.var(x) Variance
np.max(x) Maximum value
np.min(x) Minimum value

๐Ÿ” Comparisons & Logical Checks

Function Description
np.isfinite(x) Finite check
np.isinf(x) Infinite check
np.isnan(x) NaN check
np.isclose(x1, x2) Check if values are close
np.equal(x1, x2) Element-wise equality

๐Ÿ“ Linear Algebra (Bonus)

Function Description
np.dot(x1, x2) Dot product
np.matmul(x1, x2) Matrix multiplication
np.linalg.inv(x) Inverse of a matrix
np.linalg.det(x) Determinant
np.linalg.eig(x) Eigenvalues and eigenvectors

๐Ÿ’ก Constants

Constant Description
np.pi ฯ€
np.e Eulerโ€™s number
np.inf Infinity
np.nan Not a number

โš™๏ธ Practical NumPy Example

You can run it and see the output online here: pythononline.net/#qBOCl4

import numpy as np

arr = np.array([1, 2, 3, 4])
arr2 = np.array([4, 3, 2, 1])

# Arithmetic
print("Add:", np.add(arr, arr2))
print("Subtract:", np.subtract(arr, arr2))
print("Multiply:", np.multiply(arr, arr2))
print("Divide:", np.divide(arr, arr2))
print("Power:", np.power(arr, 2))
print("Mod:", np.mod(arr, 3))

# Trigonometry
print("sin(pi/2):", np.sin(np.pi / 2))
print("cos(0):", np.cos(0))
print("tan(pi/4):", np.tan(np.pi / 4))
print("arcsin(1):", np.arcsin(1))
print("arccos(0):", np.arccos(0))
print("arctan(1):", np.arctan(1))
print("arctan2(1, 1):", np.arctan2(1, 1))
print("hypot(3, 4):", np.hypot(3, 4))
print("degrees(pi):", np.degrees(np.pi))
print("radians(180):", np.radians(180))

# Exponentials & logs
print("exp(1):", np.exp(1))
print("expm1(1e-5):", np.expm1(1e-5))
print("log(e):", np.log(np.e))
print("log2(8):", np.log2(8))
print("log10(1000):", np.log10(1000))
print("log1p(1e-5):", np.log1p(1e-5))

# Absolute, rounding
print("abs(-5):", np.abs(-5))
print("sign(-3):", np.sign(-3))
print("round(3.1415):", np.round(3.1415))
print("floor(3.9):", np.floor(3.9))
print("ceil(3.1):", np.ceil(3.1))
print("trunc(3.8):", np.trunc(3.8))

# Aggregates
print("sum:", np.sum(arr))
print("prod:", np.prod(arr))
print("mean:", np.mean(arr))
print("std:", np.std(arr))
print("var:", np.var(arr))
print("max:", np.max(arr))
print("min:", np.min(arr))

# Comparisons
print("isfinite([1, np.inf]):", np.isfinite([1, np.inf]))
print("isinf([1, np.inf]):", np.isinf([1, np.inf]))
print("isnan([1, np.nan]):", np.isnan([1, np.nan]))
print("isclose(1.0, 1.00001):", np.isclose(1.0, 1.00001))
print("equal([1, 2], [1, 3]):", np.equal([1, 2], [1, 3]))

# Constants
print("pi:", np.pi)
print("e:", np.e)
print("inf:", np.inf)
print("nan:", np.nan)
Enter fullscreen mode Exit fullscreen mode

NumPy offers a rich set of fast, vectorized math functions that go far beyond Pythonโ€™s built-ins. Whether you're handling basic arithmetic, advanced trigonometry, or high-performance numerical computations, NumPy is an essential tool.

Keep this cheat sheet handy as a quick reference to power up your data science, machine learning, or scientific computing projects!

Top comments (0)