NumPy pow()
The numpy.pow()
function computes the power of elements in one array raised to the corresponding elements in another array.
It operates element-wise, meaning each base in x1
is raised to the power in x2
at the same position.
Syntax
numpy.pow(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 that will be raised to a power. |
x2 | array_like | The exponents to which each base in x1 is raised. If x1 and x2 have different shapes, they must be broadcastable to the same 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 the power function. |
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 is the result of raising x1
to the power of x2
.
If both inputs are scalars, a scalar is returned.
Examples
1. Computing Power of a Single Value
Here, we compute the power of a single base raised to a single exponent.
import numpy as np
# Define base and exponent
base = 3
exponent = 4
# Compute power
result = np.pow(base, exponent)
# Print the result
print("3 raised to the power of 4:", result)
Output:
3 raised to the power of 4: 81

2. Computing Power for Arrays
We compute the power for corresponding elements in two arrays.
import numpy as np
# Define base and exponent arrays
bases = np.array([2, 3, 4, 5])
exponents = np.array([3, 2, 1, 0])
# Compute power
result = np.pow(bases, exponents)
# Print the results
print("Bases:", bases)
print("Exponents:", exponents)
print("Power results:", result)
Output:
Bases: [2 3 4 5]
Exponents: [3 2 1 0]
Power results: [8 9 4 1]

3. Using the out
Parameter
Using an output array to store the results instead of creating a new array.
import numpy as np
# Define base and exponent arrays
bases = np.array([2, 3, 4, 5])
exponents = np.array([3, 2, 1, 0])
# Create an output array with the same shape
output_array = np.empty_like(bases)
# Compute power and store the result in output_array
np.pow(bases, exponents, out=output_array)
# Print the results
print("Computed power values:", output_array)
Output:
Computed power values: [8 9 4 1]

4. Handling Negative Bases with Non-Integer Exponents
When raising negative numbers to fractional powers, the result is NaN unless explicitly converted to a complex type.
import numpy as np
# Define base and exponent arrays
bases = np.array([-4, -9])
exponents = np.array([0.5, 1/3]) # Square root and cube root
# Compute power
result = np.pow(bases, exponents)
# Print the results
print("Computed power values:", result)
Output:
[nan nan]

To get complex results, convert the input to a complex data type.
import numpy as np
# Define base and exponent arrays
bases = np.array([-4, -9])
exponents = np.array([0.5, 1/3]) # Square root and cube root
# Convert to complex
complex_result = np.pow(bases.astype(complex), exponents)
print("Complex results:", complex_result)
Output:
Complex results: [1.22464680e-16+2.j 1.04004191e+00+1.80140543j]

Now, the negative bases return valid complex numbers.