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

</>
Copy
numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
x1array_likeThe base values.
x2array_likeThe exponent values. If x1.shape is not equal to x2.shape, they must be broadcastable to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing power.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines 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.

</>
Copy
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.

</>
Copy
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.

</>
Copy
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.

</>
Copy
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.