NumPy modf()- Return the fractional and integral parts of an array, element-wise

Numpy Modf

NumPy stands for numerical python and offers various mathematical functions to work on arrays and multidimensional matrices. With the help of this open-source package, you may conduct a wide range of intricate mathematical operations on arrays or matrices.

The modf() function is a useful addition to your toolset whether you are working with numerical data in numerical computation or just need to manage arrays in your Python scripts.

Also read: How to Use Numpy log1p in Python?

What is modf() in NumPy?

The modf() function in NumPy is used to retrieve an array’s element-by-element fractional and integral components. Also, if the supplied number is negative, both the fractional and integral components are negative.

The function will return an array, the first element of the output array is the fractional part of the input number. And the second element of the output array is an integral part of the input number. If the input number (x) is a scalar, then these are too.

Syntax of numpy modf()

numpy.modf(x, [out1, out2, ], [out=(None, None), ]*, where=True)

Parameters

  • x: array_like, Required
    • The input array
  • 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. A newly allocated array is returned in the absence of any input or None. The length of a tuple (available only as a keyword parameter) 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 formed with the default out=None will continue to be uninitialized if the condition is False.
  • **kwargs, Optional
    • For other keyword-only arguments

Implementing Numpy Modf()

Ensure to import the NumPy package into your IDE before implementing the function. To do so run the following line of code in your IDE.

import numpy as np

Example 1: Positive Integer as input

Note that the return values for integer input are float.

x1 = 5
np.modf(x1)

Output

Example 1
Example 1: Positive Integer as input

Example 2: Positive values as input

When multiple values are passed in the single input, then the output generated will have two arrays, the first array will consist of fractional parts of all the input values and the second array will consist of the integral parts of all the input values.

x2 = (0.5, 1.5, 2.5)
np.modf(x2)

Output

Example 2
Example 2: Positive values as input

Example 3: Negative values as input

Note that the output also has the negative sign associated in both cases- the fractional part as well as the integral part.

x3 = (-0.20, -1.15, -2.5)
np.modf(x3)

Output

Example 3
Example 3: Negative values as input

Summary

In conclusion, the modf() function in NumPy is a useful mathematical function that returns the fractional and integral parts of the given elements of the array in a sequential format. The return type of all elements is always float type.

Also, the output is influenced by the sign of input, which means that if the input value is negative, then the output will also be a negative value. Note the difference between divmod(x, 1) and modf is that the return values are switched, but the residual is always positive.

Reference

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