NumPy is an abbreviation of ‘Numerical Python’ and is a fundamental package primarily used on arrays and multi-dimensional matrices. This open-source package provides a wide range of functions to perform comprehensive mathematical operations on arrays or matrices.
One such function is
fmod(), it provides the divisional remainder in an element-by-element manner. The remainder obtained by this function has the same sign as that of its dividend (x1).
This function works the exact same way as the fmod() function in the C library. It is also comparable to the Matlab(TM)
rem function and must not be confused with the Python modulus operator x1 % x2 (This is achieved by the mod() function in NumPy.) In this article, let’s thoroughly try to understand the syntax and use cases of the
fmod() function in Python.
Why is fmod() used?
The fmod() function is used to calculate the remainder element by element for given arrays. The required parameters include two arrays or tuples of integer/s- the first is considered as the dividend and the latter is considered as the divisor. Conventions constrain the outcome of the modulo operation for negative dividends and divisors.
In the case of fmod, the sign of the outcome is the dividend, whereas, in the case of the remainder, the sign of the result is the divisor. The Matlab(TM)
rem function and the fmod function are equivalent.
Syntax of Numpy fmod()
numpy.fmod(x1, x2, /, out=None, *, where=True)
- x1: array_like, Required
- array considered as the dividend
- x2: array_like, Required
- array considered as the divisor
- out: ndarray, None, or tuple of ndarray and None, Optional
- a place where the outcome is saved. It must have a shape that the inputs broadcast to if it is provided. In the absence of any input or None, a newly allocated array is returned. The length of a tuple (available only as a keyword parameter) must match the number of outputs.
- where: array_like, Optional
- This circumstance is presented over the input. The ufunc result will be set to the out array at points where the condition is True. The out array will keep its initial value in all other places. It should be noted that places within an uninitialized out array that is formed with the default out=None will continue to be uninitialized if the condition is False.
- **kwargs, Optional
- For other keyword-only arguments
Implementation of Numpy fmod()
Make sure to import the NumPy package in your IDE before implementing the function. To do so run the following line of code in your IDE.
import numpy as np
Let’s try to return the remainder element-wise, in the simplest way by just providing the two required parameters- the dividend array and the divisor array. Also notice, in the second example the divisor is singular integer and hence will be same for all elements of the dividend array.
x1 = ([2,-4,11]) x2 = ([1,3,5]) ans = np.fmod(x1, x2) print(x1) print(x2) print(ans) y1 = ([9,8,7,6,5,4]) y2 = np.fmod(y1,2) print(y1) print(y2)
Let’s take another set of simple examples to understand the sign conventions in the fmod() function.
z1 = [-5, 6, 11] z2 = [2, 3, -4] z3 = np.fmod(z1, z2) print(z3)
The outcome of the first element is negative because the dividend is negative but in the case of the third element the outcome remains positive because the dividend is positive, hence we can conclude that the sign of divisor does to affect the sign of output, it is totally dependent on the dividend.
In the next example, ones() is used to create an array with all elements assigned as ‘1’. Notice that even zero gets assigned a negative sign only because the dividend has a negative sign assigned to it.
w1 = np.full((1,3),[-5, 9, 0]) w2 = np.ones((1,3)) w3 = np.fmod(w1,w2) print(w3)
NumPy package is built for working on arrays and multi-dimensional matrices. fmod() is a straightforward way to get the remainder of the division of x1(dividend) by x2(divisor). This is a scalar if both x1 and x2 are scalars.