Return the result of the negative power to which the 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

Creating a numpy array using the array() method −

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

Display the array −

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

Check the Dimensions −

print("
Dimensions of our Array...
",arr.ndim)

Get the Datatype −

print("
Datatype of our Array object...
",arr.dtype)

Get the Shape −

print("
Shape of our Array object...
",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("
Result...
",np.emath.power(arr, -2))

Example

import numpy as np

# Creating a numpy array using the array() method
arr = np.array([2, 4, 8, 16, 32])

# Display the array
print("Our Array...
",arr) # Check the Dimensions print("
Dimensions of our Array...
",arr.ndim) # Get the Datatype print("
Datatype of our Array object...
",arr.dtype) # Get the Shape print("
Shape of our Array object...
",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("
Result...
",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...
[0.25 0.0625 0.015625 0.00390625 0.00097656]

Advertisements