Return the bases when first array elements are raised to powers from second array in Python

PythonNumpyServer Side ProgrammingProgramming

To return the bases when first array elements are raised to powers from second array, use the float_power() method in Python Numpy. The method returns the bases in x1 raised to the exponents in x2. This is a scalar if both x1 and x2 are scalars. The parameter x1 are the bases. The parameter x2 are the exponents.

Raise each base in x1 to the positionally-corresponding power in x2. x1 and x2 must be broadcastable to the same shape. This differs from the power function in that integers, float16, and float32 are promoted to floats with a minimum precision of float64 so that the result is always inexact. The intent is that the function will return a usable result for negative powers and seldom overflow for positive powers.

Negative values raised to a non-integral value will return nan. To get complex results, cast the input to complex, or specify the dtype to be complex

Steps

At first, import the required libraries −

import numpy as np

The bases −

x1 = range(6)

Display the bases −

print("The bases...\n",x1)

To return the bases when first array elements are raised to powers from second array, use the float_power() method in Python Numpy. The method returns the bases in x1 raised to the exponents in x2. This is a scalar if both x1 and x2 are scalars −

print("\nResult...\n",np.float_power(x1, 2))

Example

import numpy as np

# The bases
x1 = range(6)

# Display the bases
print("The bases...\n",x1)

# To return the bases when first array elements are raised to powers from second array, use the float_power() method in Python Numpy
print("\nResult...\n",np.float_power(x1, 2))

Output

The bases...
range(0, 6)

Result...
[ 0. 1. 4. 9. 16. 25.]
raja
Updated on 28-Feb-2022 09:23:04

Advertisements