NumPy remainder()

The numpy.remainder() function computes the element-wise remainder of division, similar to the modulus (%) operator in Python. It complements numpy.floor_divide() and ensures that the remainder has the same sign as the divisor.

Syntax

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

Parameters

ParameterTypeDescription
x1array_likeDividend array (numerator values).
x2array_likeDivisor array (denominator values). Must be broadcastable with x1.
outndarray, None, or tuple of ndarray and None, optionalOptional output array to store results. If not provided, a new array is created.
wherearray_like, optionalBoolean condition array determining which elements are computed.
castingstr, optionalSpecifies casting behavior for the output array.
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 an array containing the element-wise remainder from floor_divide(x1, x2). If both x1 and x2 are scalars, a scalar is returned.


Examples

1. Computing Remainder of Two Scalars

We compute the remainder of two scalar values using numpy.remainder().

</>
Copy
import numpy as np

# Define two scalar values
x1 = 10
x2 = 3

# Compute the remainder
result = np.remainder(x1, x2)

# Print the result
print("Remainder of 10 divided by 3:", result)

Output:

Remainder of 10 divided by 3: 1

2. Computing Remainder for Arrays

We apply numpy.remainder() element-wise to an array.

</>
Copy
import numpy as np

# Define dividend and divisor arrays
x1 = np.array([10, 20, 30, 40])
x2 = np.array([3, 4, 5, 6])

# Compute the remainder for each element
remainder_values = np.remainder(x1, x2)

# Print the results
print("Dividends:", x1)
print("Divisors:", x2)
print("Remainder values:", remainder_values)

Output:

Dividends: [10 20 30 40]
Divisors: [3 4 5 6]
Remainder values: [1 0 0 4]

3. Using the out Parameter

We use an output array to store the remainder values instead of creating a new array.

</>
Copy
import numpy as np

# Define input arrays
x1 = np.array([15, 25, 35, 45])
x2 = np.array([4, 5, 6, 7])

# Create an empty output array
output_array = np.empty_like(x1)

# Compute remainder and store it in output_array
np.remainder(x1, x2, out=output_array)

# Print the results
print("Computed remainder values:", output_array)

Output:

Computed remainder values: [3 0 5 3]

4. Using the where Parameter

The where parameter allows computing the remainder only for selected elements.

</>
Copy
import numpy as np

# Define input arrays
x1 = np.array([12, 22, 32, 42])
x2 = np.array([5, 4, 3, 2])

# Define a mask (compute remainder only where mask is True)
mask = np.array([True, False, True, False])

# Compute remainder values where mask is True
result = np.remainder(x1, x2, where=mask)

# Print the results
print("Computed remainder values with mask:", result)

Output:

Computed remainder values with mask: [2 0 2 0]

Only elements where mask=True are computed.