Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return the bases when first array elements are raised to powers from second array in Python
To return the bases when first array elements are raised to powers from second array, use the np.float_power() method in Python NumPy. This function raises each base in the first array to the positionally-corresponding power in the second array, returning floating-point results with a minimum precision of float64.
The float_power() method differs from the standard power() function by promoting integers, float16, and float32 to floats with higher precision, ensuring the result is always inexact. This provides more usable results for negative powers and reduces overflow for positive powers.
Syntax
numpy.float_power(x1, x2)
Parameters
x1: Array-like, the bases to be raised to powers
x2: Array-like, the exponents
Both arrays must be broadcastable to the same shape.
Basic Example
Let's start with a simple example using a range of numbers as bases ?
import numpy as np
# The bases
bases = np.array([0, 1, 2, 3, 4, 5])
print("The bases:", bases)
# Raise each base to the power of 2
result = np.float_power(bases, 2)
print("Result (bases^2):", result)
The bases: [0 1 2 3 4 5] Result (bases^2): [ 0. 1. 4. 9. 16. 25.]
Using Two Arrays
Here's an example where each element in the first array is raised to the corresponding power in the second array ?
import numpy as np
# Bases and exponents arrays
bases = np.array([2, 3, 4, 5])
exponents = np.array([1, 2, 3, 2])
print("Bases:", bases)
print("Exponents:", exponents)
# Apply float_power element-wise
result = np.float_power(bases, exponents)
print("Result:", result)
print("Data type:", result.dtype)
Bases: [2 3 4 5] Exponents: [1 2 3 2] Result: [ 2. 9. 64. 25.] Data type: float64
Handling Negative Powers
The float_power() function handles negative exponents gracefully ?
import numpy as np
bases = np.array([2, 4, 8, 16])
negative_exponents = np.array([-1, -2, -3, -0.5])
print("Bases:", bases)
print("Negative exponents:", negative_exponents)
result = np.float_power(bases, negative_exponents)
print("Result:", result)
Bases: [ 2 4 8 16] Negative exponents: [-1. -2. -3. -0.5] Result: [0.5 0.0625 0.00195312 0.25 ]
Special Cases
When dealing with negative values raised to non-integral powers, the result will be NaN ?
import numpy as np
# Negative base with fractional exponent
negative_base = np.array([-2, -4])
fractional_exp = np.array([0.5, 1.5])
result = np.float_power(negative_base, fractional_exp)
print("Negative bases with fractional exponents:", result)
# For complex results, convert to complex type
complex_result = np.float_power(negative_base.astype(complex), fractional_exp)
print("Complex result:", complex_result)
Negative bases with fractional exponents: [nan nan] Complex result: [0.+1.41421356j 0.+8.j ]
Comparison with np.power()
| Function | Output Type | Precision | Best For |
|---|---|---|---|
np.power() |
Preserves input type | Input precision | Integer calculations |
np.float_power() |
Always float | Minimum float64 | Precise floating-point results |
Conclusion
The np.float_power() function is ideal when you need precise floating-point results from exponentiation operations. It automatically promotes to higher precision and handles negative powers more gracefully than the standard power function.
