Return the bases to different exponents in Python


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)

The exponents −

x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]

Display the exponents −

print("\nThe exponents...\n",x2)

To return the bases when first array elements are raised to powers from second array, use the float_power() method −

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

Example

import numpy as np

# The bases
x1 = range(6)

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

# The exponents
x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]

# Display the exponents
print("\nThe exponents...\n",x2)

# 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, x2))

Output

The bases...
range(0, 6)

The exponents...
[1.0, 2.0, 3.0, 3.0, 2.0, 1.0]

Result...
[ 0. 1. 8. 27. 16. 5.]

Updated on: 28-Feb-2022

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements