NumPy power()
The numpy.power()
function raises elements of an array to the power of corresponding elements in another array, element-wise. Both arrays must be broadcastable to the same shape.
Syntax
numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1 | array_like | The base values. |
x2 | array_like | The exponent values. If x1.shape is not equal to x2.shape , they must be broadcastable to a common shape. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing power. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array where each element of x1
is raised to the corresponding power in x2
. If both inputs are scalars, a scalar is returned.
Examples
1. Computing Power of Scalars
Computing the power of a single base raised to a single exponent.
import numpy as np
# Define base and exponent
base = 2
exponent = 3
# Compute power
result = np.power(base, exponent)
# Print result
print("2^3 =", result)
Output:
2^3 = 8

2. Element-wise Power Computation
Computing the power for each element in an array.
import numpy as np
# Define arrays
bases = np.array([1, 2, 3, 4, 5])
exponents = np.array([2, 3, 2, 1, 0])
# Compute power element-wise
result = np.power(bases, exponents)
# Print results
print("Bases:", bases)
print("Exponents:", exponents)
print("Result:", result)
Output:
Bases: [1 2 3 4 5]
Exponents: [2 3 2 1 0]
Result: [ 1 8 9 4 1]

3. Using the out
Parameter
Storing the output in a pre-allocated array instead of creating a new one.
import numpy as np
# Define arrays
bases = np.array([2, 3, 4])
exponents = np.array([3, 2, 1])
# Create an output array
output_array = np.empty_like(bases)
# Compute power and store result in output_array
np.power(bases, exponents, out=output_array)
# Print results
print("Computed power values:", output_array)
Output:
Computed power values: [8 9 4]

4. Handling Negative Bases with Non-Integer Exponents
When a negative base is raised to a non-integer exponent, the result is nan
unless converted to complex type.
import numpy as np
# Define base and exponent arrays
bases = np.array([-2, -3])
exponents = np.array([0.5, 1.5]) # Non-integer exponents
# Compute power (results in NaN)
result = np.power(bases, exponents)
# Convert to complex for valid computation
result_complex = np.power(bases.astype(complex), exponents)
# Print results
print("Result without complex conversion:", result)
print("Result with complex conversion:", result_complex)
Output:
/Users/tutorialkart/main.py:8: RuntimeWarning: invalid value encountered in power
result = np.power(bases, exponents)
Result without complex conversion: [nan nan]
Result with complex conversion: [ 8.65956056e-17+1.41421356j -9.54517715e-16-5.19615242j]

The first computation returns nan
since negative bases cannot be raised to fractional powers in real numbers.
Casting the base to complex allows the correct computation.