One of the essential Python modules for scientific computing is NumPy. A multidimensional array object, various derived objects (like masked arrays and matrices), and a variety of routines for quick mathematical operations on arrays are provided by this Python library.

The `remainder()`

is one such mathematical function in NumPy which is equivalent to the `mod()`

function of MATLAB. Also, do not confuse the NumPy remainder() function with the C `% operator`

, they do not work in the same manner.

## What is the remainder() in NumPy

In NumPy remainder() is used to calculate the remainder of two numbers provided as input. It is the same as the modulus operator. Considering the input values as ‘x’ is the dividend and ‘y’ is the divisor, the remainder() function is used to calculate x%y (% is the modulus operator in Python). The sign of the output will always be the same as the sign of the divisor. This function complements the `floor_divide`

function by calculating the residual which is the element-wise remainder of the quotient.

The shorthand for the remainder() is ” % ” in NumPy.

### Syntax

```
numpy.remainder(x1, x2, out=None, *, where=True, order='K', dtype=None)
```

**x1**: array_like, Required- array with dividend values.

**x2**: array_like, Required- array with divisor values.

**out:**ndarray, optional- a space where the outcome is saved. It must have a shape that the inputs propagate to if it is provided. A newly allocated array is returned without any feedback or None. The length of a tuple must match the number of outputs.

**where:**array_like, optional- This circumstance is announced 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

**order:**{‘C’, ‘F’, ‘A’, or ‘K’}, optional- Changes the result’s memory layout. “C” stands for C-order, “F” for F-order, and “A” stands for “C” unless “a” is Fortran contiguous. “K” denotes a layout that closely resembles “a”.

**dtype:**data type, optional- Overrides the data type of the result.

## Implementing the Numpy Remainder function

Make sure to import the NumPy package in your IDE before the function. To do so run the following line of code in your IDE.

```
import numpy as np
```

### Example 1: Positive values as Input

```
x1 = 10
y1 = 7
np.remainder(x1,y1)
x = (1,2,3,4,5,6)
np.remainder(x,3)
np.remainder([4,5,6],[2,3,4])
```

**Output**

### Example 2: Negative values as Input

```
#only dividend is negative
x2 = -16
y2 = 3
np.remainder(x2,y2)
#both dividend and divisor are negative
x3 = -17
y3 = -4
np.remainder(x3, y3)
#only divisor is negative
x4 = 15
y4 = -2
np.remainder(x4, y4)
```

**Output**

Observe that the output in the first case is positive, this is because the divisor is positive, and the output will always have the same sign as the divisor.

### Example 3: Using shorthand for the function

```
x = 9
y = 5
x % y
x1 = np.arange(10)
x1 % 4
```

**Output**

In the second example, the `arange()`

method is used to generate a linear sequence of numbers.

Instead of using `np.remainder()`

as performed in the previous examples, if we use “%”, it works the same way in NumPy.

## Summary

In conclusion, the remainder() function is used to calculate the remainder for given input values in an element-by-element manner. The function helps in mathematical operations when working on arrays or matrices.

## References

https://numpy.org/doc/stable/reference/generated/numpy.remainder.html