Return the result of the power to which the negative input value is raised with scimath in Python



To return the result of the power to which the input value is raised with scimath, use the scimath.power() method in Python. Returns x to the power p, i.e. the result of x**p. If x and p are scalars, so is out, otherwise an array is returned.

If x contains negative values, the output is converted to the complex domain. The parameter x is the input value. The parameter p is the power(s) to which x is raised. If x contains multiple values, p has to either be a scalar, or contain the same number of values as x. In the latter case, the result is x[0]**p[0], x[1]**p[1], ....

Steps

At first, import the required libraries −

import numpy as np

Create a numpy array using the array() method. The array elements also includes negative values −

arr = np.array([2, -4, -8, 16, -32])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array object...\n",arr.shape)

To return the result of the power to which the input value is raised with scimath, use the scimath.power() method −

print("\nResult...\n",np.emath.power(arr, 2))

Example

import numpy as np

# Create a numpy array using the array() method
# The array elements also includes negative values
arr = np.array([2, -4, -8, 16, -32])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To return the result of the power to which the input value is raised with scimath, use the scimath.power() method in Python
print("\nResult...\n",np.emath.power(arr, 2))

Output

Our Array...
[ 2 -4 -8 16 -32]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(5,)

Result...
[ 4.+0.j 16.-0.j 64.-0.j 256.+0.j 1024.-0.j]

Advertisements