What is the numpy.frexp() function in NumPy?

Overview

The numpy.frexp() function in NumPy is used to decompose the elements of an input array x into the mantissa and twos exponent (2exponent)(2^{exponent}).

Syntax

numpy.frexp(x, out1, out2, out=None, *, where=True)
Syntax for the numpy.frexp() function

Parameter value

  • x: This is the input array of numbers to be decomposed. It is a required parameter.
  • out1: This is the output array for the mantissa. It must have the same shape as the input array, x. It is an optional parameter.
  • out2: This is the output array for the exponent. It must have the same shape as the input array, x. It is an optional parameter.
  • out: This represents the location where the result is stored. It is an optional parameter. 
  • where: This is the condition over which the input is being broadcast. The resulting array will be set to the ufunc result at a given location where this condition is True. Otherwise, the resulting array will retain its original value. It is an optional parameter.
  • **kwargs: This represents other keyword arguments. It is an optional parameter.
  • Return value

    The numpy.frexp() function returns an output array showing an array of mantissa values for each element, and another array of 2exponent2^{exponent} for each element.

    Example

    import numpy as np
    # creating an array
    x = np.array([1, 2, 3, 4, 5])
    # obtaining the frexp values
    myarray = np.frexp(x)
    print("Input: " , x)
    print("Mantissas : ", myarray[0])
    print("Exponents : ", myarray[1])

    Explanation

    • Line 1: We import the numpy module.
    • Line 4: We create an input array of values called x.
    • Line 7: We implement the numpy.frexp() function on the array x. We assign the result to a variable myarray.
    • Line 9: We print the input array x to the console.
    • Line 10: We print the variable myarray to the console.

    Free Resources