NumPy divmod()

The numpy.divmod() function returns the quotient and remainder of element-wise division simultaneously. It is equivalent to (x // y, x % y) but optimized for performance.

Syntax

</>
Copy
numpy.divmod(x1, x2, [out1, out2], /, [out=(None, None)], *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
x1array_likeDividend array.
x2array_likeDivisor array. If x1.shape != x2.shape, they must be broadcastable to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output arrays where the results are stored. If None, new arrays are created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original values.
castingstr, optionalDefines the casting behavior when computing the quotient and remainder.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns a tuple (quotient, remainder). Both are arrays or scalars depending on input type.


Examples

1. Computing Quotient and Remainder for Single Values

Here, we compute the quotient and remainder of two scalar values.

</>
Copy
import numpy as np

# Define dividend and divisor
x = 10
y = 3

# Compute quotient and remainder
quotient, remainder = np.divmod(x, y)

# Print results
print("Quotient:", quotient)
print("Remainder:", remainder)

Output:

Quotient: 3
Remainder: 1

2. Using NumPy divmod with Arrays

We compute element-wise quotient and remainder for two NumPy arrays.

</>
Copy
import numpy as np

# Define two arrays (dividends and divisors)
x = np.array([10, 20, 30, 40])
y = np.array([3, 4, 5, 6])

# Compute quotient and remainder
quotient, remainder = np.divmod(x, y)

# Print results
print("Quotient:", quotient)
print("Remainder:", remainder)

Output:

Quotient: [3 5 6 6]
Remainder: [1 0 0 4]

3. Using the out Parameter

Using output arrays to store results instead of creating new ones.

</>
Copy
import numpy as np

# Define two arrays (dividends and divisors)
x = np.array([15, 25, 35])
y = np.array([4, 6, 7])

# Create output arrays
quot_out = np.empty_like(x)
rem_out = np.empty_like(x)

# Compute quotient and remainder with out parameter
np.divmod(x, y, out=(quot_out, rem_out))

# Print results
print("Quotient (stored in out):", quot_out)
print("Remainder (stored in out):", rem_out)

Output:

Quotient (stored in out): [3 4 5]
Remainder (stored in out): [3 1 0]

4. Using the where Parameter

Computing quotient and remainder only for selected elements using a condition.

</>
Copy
import numpy as np

# Define two arrays
x = np.array([15, 20, 25, 30])
y = np.array([3, 4, 5, 6])

# Define a mask (compute only for selected values)
mask = np.array([True, False, True, False])

# Compute quotient and remainder where mask is True
quotient, remainder = np.divmod(x, y, where=mask)

# Print results
print("Quotient with mask:", quotient)
print("Remainder with mask:", remainder)

Output:

Quotient with mask: [5 0 5 0]
Remainder with mask: [0 0 0 0]

Here, divmod is computed only for elements where mask=True, while the rest remain unchanged.