Skip to main content
NumPy provides specialized mathematical functions including roots, powers, and number theory operations.

Root Functions

numpy.sqrt

numpy.sqrt(x, /, out=None, *, where=True, **kwargs)
Return the non-negative square-root of an array, element-wise. Parameters:
  • x : array_like - The values whose square-roots are required.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - An array of the same shape as x, containing the positive square-root of each element in x.
Mathematical Formula: y=xy = \sqrt{x} The square root satisfies: x×x=x\sqrt{x} \times \sqrt{x} = x Notes:
  • For real input, negative values return nan.
  • For complex input, returns complex results.
  • Has a branch cut on the real interval [,0)[-\infty, 0), continuous from above.
Examples:
import numpy as np

np.sqrt([1, 4, 9])
# array([ 1.,  2.,  3.])

np.sqrt([4, -1, -3+4j])
# array([ 2.+0.j,  0.+1.j,  1.+2.j])

np.sqrt([4, -1, np.inf])
# array([ 2., nan, inf])

# Verify: sqrt(x)^2 = x
x = 16.0
np.sqrt(x) ** 2
# 16.0

numpy.cbrt

numpy.cbrt(x, /, out=None, *, where=True, **kwargs)
Return the cube-root of an array, element-wise. Parameters:
  • x : array_like - The values whose cube-roots are required.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - An array of the same shape as x, containing the cube root of each element in x.
Mathematical Formula: y=x3y = \sqrt[3]{x} The cube root satisfies: x3×x3×x3=x\sqrt[3]{x} \times \sqrt[3]{x} \times \sqrt[3]{x} = x Examples:
import numpy as np

np.cbrt([1, 8, 27])
# array([ 1.,  2.,  3.])

# Works for negative numbers
np.cbrt([-1, -8, -27])
# array([-1., -2., -3.])

# Verify: cbrt(x)^3 = x
x = 64.0
np.cbrt(x) ** 3
# 64.0

Power Functions

numpy.square

numpy.square(x, /, out=None, *, where=True, **kwargs)
Return the element-wise square of the input. Parameters:
  • x : array_like - Input data.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • out : ndarray or scalar - Element-wise x*x, of the same shape and dtype as x.
Mathematical Formula: y=x2y = x^2 Examples:
import numpy as np

np.square([-1j, 1])
# array([-1.+0.j,  1.+0.j])

np.square([1, 2, 3, 4, 5])
# array([ 1,  4,  9, 16, 25])

# More efficient than using power
x = np.arange(1000000)
%timeit np.square(x)  # Faster
%timeit np.power(x, 2)  # Slower

numpy.reciprocal

numpy.reciprocal(x, /, out=None, *, where=True, **kwargs)
Return the reciprocal of the argument, element-wise. Parameters:
  • x : array_like - Input array.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - Output array, element-wise reciprocal of x.
Mathematical Formula: y=1xy = \frac{1}{x} Notes:
  • For integer input, the result is truncated toward zero.
  • For floating-point input, returns exact reciprocal.
  • Division by zero returns inf for float types.
Examples:
import numpy as np

np.reciprocal(2.0)
# 0.5

np.reciprocal([1, 2, 4, 8])
# array([1.  , 0.5 , 0.25, 0.125])

# Integer reciprocal truncates
np.reciprocal([1, 2, 3], dtype=int)
# array([1, 0, 0])

# Equivalent to 1/x
x = np.array([1.0, 2.0, 4.0])
np.allclose(np.reciprocal(x), 1/x)
# True

Number Theory Functions

numpy.gcd

numpy.gcd(x1, x2, /, out=None, *, where=True, **kwargs)
Returns the greatest common divisor of |x1| and |x2|. Parameters:
  • x1, x2 : array_like, int - Arrays of values. If x1.shape != x2.shape, they must be broadcastable to a common shape.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray or scalar - The greatest common divisor of the absolute value of the inputs.
Mathematical Formula: gcd(a,b)=largest positive integer that divides both a and b\gcd(a, b) = \text{largest positive integer that divides both } a \text{ and } b Examples:
import numpy as np

np.gcd(12, 20)
# 4

# Reduce to find GCD of multiple numbers
np.gcd.reduce([15, 25, 35])
# 5

np.gcd(np.arange(6), 20)
# array([20,  1,  2,  1,  4,  5])

# Works with arrays
np.gcd([12, 18, 24], [20, 27, 36])
# array([4, 9, 12])

numpy.lcm

numpy.lcm(x1, x2, /, out=None, *, where=True, **kwargs)
Returns the lowest common multiple of |x1| and |x2|. Parameters:
  • x1, x2 : array_like, int - Arrays of values. If x1.shape != x2.shape, they must be broadcastable to a common shape.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray or scalar - The lowest common multiple of the absolute value of the inputs.
Mathematical Formula: lcm(a,b)=smallest positive integer divisible by both a and b\text{lcm}(a, b) = \text{smallest positive integer divisible by both } a \text{ and } b Relationship with GCD: lcm(a,b)=a×bgcd(a,b)\text{lcm}(a, b) = \frac{|a \times b|}{\gcd(a, b)} Examples:
import numpy as np

np.lcm(12, 20)
# 60

# Reduce to find LCM of multiple numbers
np.lcm.reduce([3, 12, 20])
# 60

np.lcm.reduce([40, 12, 20])
# 120

np.lcm(np.arange(6), 20)
# array([ 0, 20, 20, 60, 20, 20])

# Works with arrays
np.lcm([12, 18, 24], [20, 27, 36])
# array([ 60, 162, 72])

Relationships and Properties

Root and Power Relationships

Inverse Relationships:
  • x2=x\sqrt{x^2} = |x| (absolute value for real xx)
  • (x)2=x(\sqrt{x})^2 = x (for x0x \geq 0)
  • (x3)3=x(\sqrt[3]{x})^3 = x (for all real xx)
  • x×reciprocal(x)=1x \times \text{reciprocal}(x) = 1 (for x0x \neq 0)
Examples:
import numpy as np

# Square and square root are inverses (for positive values)
x = np.array([1, 4, 9, 16])
np.allclose(np.sqrt(np.square(np.sqrt(x))), np.sqrt(x))
# True

# Reciprocal is its own inverse
x = np.array([2.0, 4.0, 8.0])
np.allclose(np.reciprocal(np.reciprocal(x)), x)
# True

# Cube root works for negative numbers
x = np.array([-8, -1, 1, 8, 27])
np.allclose(np.cbrt(x)**3, x)
# True

GCD and LCM Relationships

Properties:
  • gcd(a,b)×lcm(a,b)=a×b\gcd(a, b) \times \text{lcm}(a, b) = |a \times b|
  • gcd(a,b)min(a,b)\gcd(a, b) \leq \min(|a|, |b|)
  • lcm(a,b)max(a,b)\text{lcm}(a, b) \geq \max(|a|, |b|)
  • gcd(a,0)=a\gcd(a, 0) = |a|
  • lcm(a,0)=0\text{lcm}(a, 0) = 0
Examples:
import numpy as np

# Verify GCD * LCM = |a * b|
a, b = 12, 20
np.gcd(a, b) * np.lcm(a, b) == abs(a * b)
# True

# GCD divides both numbers
a, b = 48, 18
gcd_val = np.gcd(a, b)
print(f"GCD: {gcd_val}")
print(f"{a} / {gcd_val} = {a // gcd_val}")
print(f"{b} / {gcd_val} = {b // gcd_val}")
# GCD: 6
# 48 / 6 = 8
# 18 / 6 = 3

# Both numbers divide LCM
a, b = 12, 18
lcm_val = np.lcm(a, b)
print(f"LCM: {lcm_val}")
print(f"{lcm_val} / {a} = {lcm_val // a}")
print(f"{lcm_val} / {b} = {lcm_val // b}")
# LCM: 36
# 36 / 12 = 3
# 36 / 18 = 2

Performance Tips

  • Use np.square(x) instead of np.power(x, 2) for better performance.
  • Use np.sqrt(x) instead of np.power(x, 0.5) for better performance.
  • Use np.reciprocal(x) instead of 1/x when working with large arrays for slight performance gains.
  • gcd.reduce() and lcm.reduce() are efficient for finding GCD/LCM of multiple numbers.

See Also

Arithmetic Functions

Basic arithmetic and power operations

Trigonometric Functions

sin, cos, tan, and hypot

Exponential Functions

exp, log, and logarithms

Rounding Functions

floor, ceil, round, and truncation

Build docs developers (and LLMs) love