Hello and welcome to this tutorial on Numpy divmod. In this tutorial, we will be learning about the NumPy divmod() method and also seeing a lot of examples regarding the same. So let us begin!
Also read: NumPy mod – A Complete Guide to the Modulus Operator in Numpy
What is NumPy divmod?
The divmod()
method in NumPy returns the element-wise quotient and the remainder of the division of two given arrays. The //
in Python returns the floor of the quotient and the %
operator returns the remainder of the division, similar to the mod()
function.
We will see examples demonstrating the use of this function in the upcoming sections of this tutorial.
Syntax of NumPy divmod
numpy.divmod(x1, x2, out=None)
Parameter | Description | Required/Optional |
x1 (array_like) | Dividend array. | Required |
x2 (array_like) | Divisor array. | Required |
out | An alternative output array in which to place the result. It must have the same shape as the expected output. | Optional |
Returns:
Returns the element-wise quotient and remainder of the division. If both x1 and x2 are scalars, then the result is also a scalar value.
Some common observations about the divmod() function:
- If the first argument, i.e., x1 is 0, then the function returns (0, 0).
- If the second argument, i.e., x2 is 0, then the function returns a ‘Zero Division Error’.
- If x1 is a float value, the function returns (q, x1%x2) where q is the whole part of the quotient.
Examples of numpy.divmod()
Let’s now get right into using the numpy.divmod method so we can understand the outputs.
Using numpy.divmod() when both the elements are scalar
import numpy as np
dividend = 23
divisor = 6
ans = np.divmod(dividend, divisor)
print("Dividend =", dividend, "\nDivisor =", divisor)
print("Result =", ans)
Output:
Dividend = 23
Divisor = 6
Result = (3, 5)
Here both the elements are scalar. 6*3=18 and 6*4=24, so 23 is not completely divisible by 6. When 23 is divided by 6, the quotient is 3 and remainder is 23-18=5, which is returned as (3, 5).
Using numpy.divmod() when one element is a scalar and the other an array
import numpy as np
dividend = [30, 19, 8]
divisor = 6
ans = np.divmod(dividend, divisor)
print("Dividend =", dividend, "\nDivisor =", divisor)
print("Result =", ans)
Output:
Dividend = [30, 19, 8]
Divisor = 6
Result = (array([5, 3, 1], dtype=int32), array([0, 1, 2], dtype=int32))
In this case, all the elements in the dividend array are one-by-one divided by the divisor and the quotient and remainder for each of these divisions are stored in the respective result array.
The first array in the output is the quotient array and the second one is the remainder array.
The output is computed as:
Quotient array:
30//6 = 5
19//6 = 3
8//6 = 1
Remainder array:
30%6 = 0
19%6 = 1
8%6 = 2
Using numpy.divmod() when both the elements are 1-dimensional arrays
import numpy as np
dividend = [72, 60, 30]
divisor = [3, 15, 24]
ans = np.divmod(dividend, divisor)
print("Dividend =", dividend, "\nDivisor =", divisor)
print("Result =", ans)
Output:
Dividend = [72, 60, 30]
Divisor = [3, 15, 24]
Result = (array([24, 4, 1], dtype=int32), array([0, 0, 6], dtype=int32))
Here, the elements at the same places in both arrays undergo the division operation and the quotient and remainder is calculated. That is, dividend[0] is divided by divisor[0] and so on. This is nothing but element-wise division.
Quotient array:
dividend[0] // divisor[0] = 72//3 = 24
dividend[1] // divisor[1] = 60//15 = 4
dividend[2] // divisor[2] = 30//24 = 1
Remainder array:
dividend[0] % divisor[0] = 72%3 = 0
dividend[1] % divisor[1] = 60%15 = 0
dividend[2] % divisor[2] = 30%24 = 6
Using numpy.divmod() when both the elements are 2-dimensional arrays
import numpy as np
dividend = [[18, 35], [10, 7]]
divisor = [[5, 7], [10, 4]]
ans = np.divmod(dividend, divisor)
print("Dividend =", dividend, "\nDivisor =", divisor)
print("Result =\n", ans)
Output:
Dividend = [[18, 35], [10, 7]]
Divisor = [[5, 7], [10, 4]]
Result =
(array([[3, 5],
[1, 1]], dtype=int32), array([[3, 0],
[0, 3]], dtype=int32))
Same as the above example of 1-dimensional arrays, here as well the element-wise division takes place and the quotient and remainder are calculated as:
Quotient array:
dividend[0][0] // divisor[0][0] = 18//5 = 3
dividend[0][1] // divisor[0][1] = 35//7 = 5
dividend[1][0] // divisor[1][0] = 10//10 = 1
dividend[1][1] // divisor[1][1] = 7//4 = 1
Resulting in [[3, 5], [1, 1]].
Remainder array:
dividend[0][0] // divisor[0][0] = 18%5 = 3
dividend[0][1] // divisor[0][1] = 35%7 = 0
dividend[1][0] // divisor[1][0] = 10%10 = 0
dividend[1][1] // divisor[1][1] = 7%4 = 3
Resulting in [[3, 0], [0, 3]].
Summary
That’s all! In this tutorial, we learned about the Numpy divmod method and practiced different types of examples using the same. If you want to learn more about NumPy, feel free to go through our NumPy tutorials.