NumPy fmod()

The numpy.fmod() function computes the element-wise remainder of division. Unlike the Python modulus operator (%), the remainder retains the same sign as the dividend (x1).

Syntax

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

Parameters

ParameterTypeDescription
x1array_likeDividend (numerator).
x2array_likeDivisor (denominator). Must be broadcastable with x1 to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the 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 an array with the remainder of the division of x1 by x2. If both inputs are scalars, a scalar is returned.


Examples

1. Computing the Remainder of Two Scalars

Computing the remainder of two numbers using numpy.fmod().

</>
Copy
import numpy as np

# Define dividend and divisor
x1 = 7
x2 = 3

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

# Print the result
print("Remainder of 7 / 3:", result)

Output:

Remainder of 7 / 3: 1

2. Computing fmod for an Array

Using numpy.fmod() to compute the remainder for an array of values.

</>
Copy
import numpy as np

# Define an array of dividends
x1 = np.array([7, 8, 9, 10])

# Define an array of divisors
x2 = np.array([3, 2, 4, 5])

# Compute element-wise remainder
result = np.fmod(x1, x2)

# Print the results
print("Dividends:", x1)
print("Divisors:", x2)
print("Remainders:", result)

Output:

Dividends: [ 7  8  9 10]
Divisors: [3 2 4 5]
Remainders: [1 0 1 0]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of dividends
x1 = np.array([10, 15, 20])

# Define an array of divisors
x2 = np.array([4, 6, 7])

# Create an output array with the same shape
output_array = np.empty_like(x1)

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

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

Output:

Computed remainders: [2 3 6]

4. Using the where Parameter

Using a condition to compute the remainder only for selected elements.

</>
Copy
import numpy as np

# Define an array of dividends
x1 = np.array([9, 10, 11, 12])

# Define an array of divisors
x2 = np.array([4, 5, 3, 6])

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

# Compute remainders where mask is True
result = np.fmod(x1, x2, where=mask)

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

Output:

Computed remainders with mask: [1 0 2 0]

The remainder is computed only for elements where mask=True.