NumPy fmod() Function:
Numpy fmod: The fmod() function of the NumPy module returns the remainder of the division element-by-element.
In terms of array broadcasting, it is equivalent to l1 % l2.
The remainder has the same sign as the dividend l1 in this NumPy implementation of the C library function fmod. It is not to be confused with the Python modulus operator l1 % l2. It is equivalent to the Matlab rem function.
Syntax:
numpy.fmod(l1, l2, out=None)
Parameters:
l1 and l2: (Required)
fmod() python: These are required arguments. These are the arrays that have to be divided, here l1 as dividend and l2 as the divisor.
They must be broadcastable to a common shape if l1.shape!= l2.shape.
out:
This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.
Return Value:
The element-by-element remainder of dividing l1 and l2 is returned.
NumPy fmod() Function in Python
Example
Approach:
- Import NumPy module using the import keyword.
- Pass some random list as an argument to the array() function to create an array.
Store it in a variable. - Create some sample arrays of different shapes to test the fmod() function.
- Pass the first array and some random number to fmod() function of NumPy module and print the result.
- Here it divides each element of the array1 with the given random value say 9 and gives the remainder.
- Pass the first array and second array to fmod() function of NumPy module and print the result.
- Here it divides second array elements for the first array element and gives the remainder
- Print the result array.
- Similarly, test it with other arrays of different shapes.
- The Exit of the Program.
Below is the implementation:
# Import NumPy module using the import keyword.
import numpy as np
# Pass some random list as an argument to the array() function to create an array.
# Store it in a variable.
arry1 = np.array([[32,64],[95,128],[150,180]])
# Create some sample arrays of different shapes to test the fmod() function.
arry2 = np.array([[10,20]])
arry3 = np.array([[100],[200],[300]])
arry4 = np.array([[55,65],[75,85],[95,105]])
# Pass the first array and some random number to fmod() function of NumPy module and print the result.
# Here it divides each element of the array1 with the given random value say 9 and gives the remainder.
print('Getting fmod value by dividing first array with 9: ')
print(np.fmod(arry1, 9))
# Pass the first array and second array to fmod() function of NumPy module and print the result.
# Here it divides second array elements for the first array element and gives the remainder
# Print the result array.
print('Getting fmod value by dividing first array with second array: ')
print(np.fmod(arry1, arry2))
# Similarly, test it with other arrays of different shapes.
print('Getting fmod value by dividing first array with third array: ')
print(np.fmod(arry1, arry3))
print('Getting fmod value by dividing first array with fourth array: ')
print(np.fmod(arry1, arry4))
Output:
Getting fmod value by dividing first array with 9: [[5 1] [5 2] [6 0]] Getting fmod value by dividing first array with second array: [[2 4] [5 8] [0 0]] Getting fmod value by dividing first array with third array: [[ 32 64] [ 95 128] [150 180]] Getting fmod value by dividing first array with fourth array: [[32 64] [20 43] [55 75]]