Skip to main content
NumPy provides a comprehensive set of universal functions (ufuncs) for performing element-wise arithmetic operations on arrays.

Basic Arithmetic Operations

numpy.add

numpy.add(x1, x2, /, out=None, *, where=True, **kwargs)
Add arguments element-wise. Parameters:
  • x1, x2 : array_like - The arrays to be added. 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:
  • add : ndarray or scalar - The sum of x1 and x2, element-wise.
Notes: Equivalent to x1 + x2 in terms of array broadcasting. Examples:
import numpy as np

np.add(1.0, 4.0)
# 5.0

x1 = np.arange(9.0).reshape((3, 3))
x2 = np.arange(3.0)
np.add(x1, x2)
# array([[  0.,   2.,   4.],
#        [  3.,   5.,   7.],
#        [  6.,   8.,  10.]])

# Using the + operator
x1 + x2
# array([[ 0.,  2.,  4.],
#        [ 3.,  5.,  7.],
#        [ 6.,  8., 10.]])

numpy.subtract

numpy.subtract(x1, x2, /, out=None, *, where=True, **kwargs)
Subtract arguments element-wise. Parameters:
  • x1, x2 : array_like - The arrays to be subtracted from each other.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - The difference of x1 and x2, element-wise.
Notes: Equivalent to x1 - x2 in terms of array broadcasting. Examples:
import numpy as np

np.subtract(1.0, 4.0)
# -3.0

x1 = np.arange(9.0).reshape((3, 3))
x2 = np.arange(3.0)
np.subtract(x1, x2)
# array([[ 0.,  0.,  0.],
#        [ 3.,  3.,  3.],
#        [ 6.,  6.,  6.]])

# Using the - operator
x1 - x2
# array([[0., 0., 0.],
#        [3., 3., 3.],
#        [6., 6., 6.]])

numpy.multiply

numpy.multiply(x1, x2, /, out=None, *, where=True, **kwargs)
Multiply arguments element-wise. Parameters:
  • x1, x2 : array_like - Input arrays to be multiplied.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - The product of x1 and x2, element-wise.
Notes: Equivalent to x1 * x2 in terms of array broadcasting. Examples:
import numpy as np

np.multiply(2.0, 4.0)
# 8.0

x1 = np.arange(9.0).reshape((3, 3))
x2 = np.arange(3.0)
np.multiply(x1, x2)
# array([[  0.,   1.,   4.],
#        [  0.,   4.,  10.],
#        [  0.,   7.,  16.]])

# Using the * operator
x1 * x2
# array([[  0.,   1.,   4.],
#        [  0.,   4.,  10.],
#        [  0.,   7.,  16.]])

numpy.divide

numpy.divide(x1, x2, /, out=None, *, where=True, **kwargs)
Divide arguments element-wise. Parameters:
  • x1 : array_like - Dividend array.
  • x2 : array_like - Divisor 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 or scalar - The quotient x1/x2, element-wise.
Notes: Equivalent to x1 / x2 in terms of array broadcasting. The true_divide(x1, x2) function is an alias for divide(x1, x2). Examples:
import numpy as np

np.divide(2.0, 4.0)
# 0.5

x1 = np.arange(9.0).reshape((3, 3))
x2 = 2 * np.ones(3)
x1 / x2
# array([[0. , 0.5, 1. ],
#        [1.5, 2. , 2.5],
#        [3. , 3.5, 4. ]])

numpy.power

numpy.power(x1, x2, /, out=None, *, where=True, **kwargs)
First array elements raised to powers from second array, element-wise. Parameters:
  • x1 : array_like - The bases.
  • x2 : array_like - The exponents.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - The bases in x1 raised to the exponents in x2.
Mathematical Formula: y=x1x2y = x_1^{x_2} Notes:
  • An integer type raised to a negative integer power will raise a ValueError.
  • Negative values raised to a non-integral value will return nan.
  • To get complex results, cast the input to complex or specify dtype=complex.
Examples:
import numpy as np

# Cube each element
x1 = np.arange(6)
np.power(x1, 3)
# array([  0,   1,   8,  27,  64, 125])

# Raise to different exponents
x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
np.power(x1, x2)
# array([  0.,   1.,   8.,  27.,  16.,   5.])

# Using the ** operator
x1 ** 2
# array([ 0,  1,  4,  9, 16, 25])

# Complex results
x3 = np.array([-1.0, -4.0])
np.power(x3, 1.5, dtype=np.complex128)
# array([-1.83697020e-16-1.j, -1.46957616e-15-8.j])

Modular Arithmetic

numpy.mod

numpy.mod(x1, x2, /, out=None, *, where=True, **kwargs)
Returns the element-wise remainder of division. This is an alias for numpy.remainder. Parameters:
  • x1 : array_like - Dividend array.
  • x2 : array_like - Divisor array.
Returns:
  • y : ndarray - The element-wise remainder.
Mathematical Formula: y=x1modx2y = x_1 \bmod x_2 Equivalent to the Python modulus operator x1 % x2.

numpy.remainder

numpy.remainder(x1, x2, /, out=None, *, where=True, **kwargs)
Returns the element-wise remainder of division. Parameters:
  • x1 : array_like - Dividend array.
  • x2 : array_like - Divisor 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 - The element-wise remainder of the quotient floor_divide(x1, x2).
Notes:
  • Computes the remainder complementary to the floor_divide function.
  • Equivalent to the Python modulus operator x1 % x2 and has the same sign as the divisor x2.
  • Returns 0 when x2 is 0 and both x1 and x2 are integers.
  • mod is an alias of remainder.
This should not be confused with Python’s math.remainder and C’s remainder, which compute the IEEE remainder (complement to round(x1 / x2)), or MATLAB’s rem function (complement to int(x1 / x2)).
Examples:
import numpy as np

np.remainder([4, 7], [2, 3])
# array([0, 1])

np.remainder(np.arange(7), 5)
# array([0, 1, 2, 3, 4, 0, 1])

# Using the % operator
x1 = np.arange(7)
x1 % 5
# array([0, 1, 2, 3, 4, 0, 1])

numpy.divmod

numpy.divmod(x1, x2, /, out=None, *, where=True, **kwargs)
Return element-wise quotient and remainder simultaneously. Parameters:
  • x1 : array_like - Dividend array.
  • x2 : array_like - Divisor array.
  • out : tuple of ndarrays, optional - A tuple of two arrays for output.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • out1 : ndarray - Element-wise quotient.
  • out2 : ndarray - Element-wise remainder.
Notes: np.divmod(x, y) is equivalent to (x // y, x % y), but faster because it avoids redundant work. Used to implement Python’s built-in divmod function on NumPy arrays. Examples:
import numpy as np

np.divmod(np.array([7, 14, 21]), 3)
# (array([2, 4, 7]), array([1, 2, 0]))

Sign and Absolute Value

numpy.absolute

numpy.absolute(x, /, out=None, *, where=True, **kwargs)
Calculate the absolute value element-wise. np.abs is a shorthand for this function. 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:
  • absolute : ndarray - An ndarray containing the absolute value of each element in x.
Mathematical Formula:
  • For real numbers: x|x|
  • For complex numbers a+iba + ib: a2+b2\sqrt{a^2 + b^2}
Examples:
import numpy as np

x = np.array([-1.2, 1.2])
np.absolute(x)
# array([ 1.2,  1.2])

np.absolute(1.2 + 1j)
# 1.5620499351813308

# Using abs() shorthand
abs(np.array([-1.2, 1.2]))
# array([1.2, 1.2])

numpy.sign

numpy.sign(x, /, out=None, *, where=True, **kwargs)
Returns an element-wise indication of the sign of a number. Parameters:
  • x : array_like - Input values.
  • out : ndarray, optional - A location into which the result is stored.
  • where : array_like, optional - Condition to broadcast over the input.
Returns:
  • y : ndarray - The sign of x.
Mathematical Formula:
  • Returns -1 if x < 0
  • Returns 0 if x == 0
  • Returns 1 if x > 0
  • Returns nan for nan inputs
  • For complex inputs: returns x / abs(x) (and 0 if x == 0)
Notes: For complex numbers, the definition changed in NumPy 2.0 to follow the Array API standard, using x/xx/|x| instead of the previous definition. Examples:
import numpy as np

np.sign([-5., 4.5])
# array([-1.,  1.])

np.sign(0)
# 0

np.sign([3-4j, 8j])
# array([0.6-0.8j, 0. +1.j ])

See Also

Trigonometric Functions

sin, cos, tan, and their inverses

Exponential Functions

exp, log, and power functions

Rounding Functions

floor, ceil, round, and truncation

Special Functions

sqrt, square, gcd, and lcm

Build docs developers (and LLMs) love