NumPy modf()

The numpy.modf() function returns the fractional and integral parts of each element in an input array, element-wise. The fractional and integral parts retain the sign of the input values.

Syntax

</>
Copy
numpy.modf(x, [out1, out2, ]/, [out=(None, None), ]*, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array for which the fractional and integral parts are computed.
outndarray, None, or tuple of ndarray and None, optionalOptional output arrays where the results are stored. If None, new arrays are created.
wherearray_like, optionalBoolean mask specifying where the operation should be applied. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior during computation.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output arrays.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

ReturnTypeDescription
y1ndarrayFractional part of x. Has the same sign as x.
y2ndarrayIntegral part of x. Has the same sign as x.

Examples

1. Splitting Fractional and Integral Parts of a Number

Here, we compute the fractional and integral parts of a single floating-point number.

</>
Copy
import numpy as np

# Define a floating-point number
num = 4.75

# Compute the fractional and integral parts
fractional, integral = np.modf(num)

# Print the results
print("Fractional part:", fractional)
print("Integral part:", integral)

Output:

Fractional part: 0.75
Integral part: 4.0

2. Applying modf to an Array

We compute the fractional and integral parts of multiple numbers in an array.

</>
Copy
import numpy as np

# Define an array of floating-point numbers
numbers = np.array([3.14, -2.75, 8.99, -6.5])

# Compute the fractional and integral parts
fractional_parts, integral_parts = np.modf(numbers)

# Print the results
print("Input Array:", numbers)
print("Fractional Parts:", fractional_parts)
print("Integral Parts:", integral_parts)

Output:

Input Array: [ 3.14 -2.75  8.99 -6.5 ]
Fractional Parts: [ 0.14 -0.75  0.99 -0.5 ]
Integral Parts: [ 3. -2.  8. -6. ]

3. Using the out Parameter

Using predefined arrays as output buffers for the results.

</>
Copy
import numpy as np

# Define an array of numbers
numbers = np.array([1.5, 3.75, -4.25, -7.6])

# Create empty arrays for output storage
fractional_output = np.empty_like(numbers)
integral_output = np.empty_like(numbers)

# Compute modf and store results in output arrays
np.modf(numbers, out=(fractional_output, integral_output))

# Print the results
print("Fractional Parts:", fractional_output)
print("Integral Parts:", integral_output)

Output:

Fractional Parts: [ 0.5   0.75 -0.25 -0.6 ]
Integral Parts: [ 1.  3. -4. -7. ]

4. Using the where Parameter

Computing modf selectively using a mask.

</>
Copy
import numpy as np

# Define an array of numbers
numbers = np.array([1.1, -3.4, 5.6, -2.9])

# Define a mask (compute modf only where mask is True)
mask = np.array([True, False, True, False])

# Compute modf values only where mask is True
fractional, integral = np.modf(numbers, where=mask)

# Print the results
print("Fractional Parts with mask:", fractional)
print("Integral Parts with mask:", integral)

Output:

Fractional Parts with mask: [ 0.1  0.   0.6  0. ]
Integral Parts with mask: [ 1.  0.  5.  0. ]

The function computes only for values where mask=True, while the other values remain unchanged.