Return the result of the power to which the negative input value is raised with scimath in Python
Last Updated :
22 Apr, 2022
In this article, we will discuss how to Return the result of the power to which the negative input value is raised with scimath in Python and NumPy.
Example
Input: [-1,-2,-3]
Output: [1.-0.j 4.-0.j 9.-0.j]
Explanation: It return x to the power p, (x**p), If x contains negative values, the output is converted to the complex domain.
NumPy.lib.scimath.power method
The lib.scimath.power() from the NumPy package is used to return the power to which the negative input value is raised. The result is (x**p) returns x to the power p. The result is translated to the complex domain if x contains negative values.
Syntax: numpy.lib.scimath.power(x,p)
Parameters:
- x: Input array or scalar.
- p: The number of times x is multiplied.
Return: return x to the power p, (x**p), If x contains negative values, the output is converted to the complex domain.
Example 1:
In this example, we are importing the NumPy package and an array is created using the np.array() method. Information about the array such as shape, datatype, and dimension can be found using the .shape, .dtype, and .ndim attributes. An array of negative values are created in this example and it is raised to a power 2 using the lib.scimath.power() method. As we can see x contains negative values, the output is converted to the complex domain.
Python3
import numpy as np
array = np.array([ - 1 , - 2 , - 3 ])
print (array)
print ( "Shape of the array is : " ,array.shape)
print ( "The dimension of the array is : " ,array.ndim)
print ( "Datatype of our Array is : " ,array.dtype)
print (np.lib.scimath.power(array, 2 ))
|
Output:
[-1 -2 -3]
Shape of the array is : (3,)
The dimension of the array is : 1
Datatype of our Array is : int64
[1.-0.j 4.-0.j 9.-0.j]
Example 2:
In this example, an array of positive values are passed in the lib.scimath.power() method.
Python3
import numpy as np
array = np.array([ 1 , 2 , 3 ])
print (array)
print ( "Shape of the array is : " ,array.shape)
print ( "The dimension of the array is : " ,array.ndim)
print ( "Datatype of our Array is : " ,array.dtype)
print (np.lib.scimath.power(array, 2 ))
|
Output:
[1 2 3]
Shape of the array is : (3,)
The dimension of the array is : 1
Datatype of our Array is : int64
[1 4 9]
Example 3:
In this example instead of giving a positive ‘p-value’, NEGATIVE power is given.
Python3
import numpy as np
array = np.array([ 25 , 36 ])
print (array)
print ( "Shape of the array is : " ,array.shape)
print ( "The dimension of the array is : " ,array.ndim)
print ( "Datatype of our Array is : " ,array.dtype)
print (np.lib.scimath.power(array, - 2 ))
|
Output:
[25 36]
Shape of the array is : (2,)
The dimension of the array is : 1
Datatype of our Array is : int64
[0.0016 0.0007716]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...