Return the result of the power to which the input value is raised with scimath in Python
Last Updated :
02 Aug, 2022
This article will discuss how to Return the result of the power to which the input value is raised with sci math in Python using NumPy.
Example
Input: [1,2,3]
Output: [1 4 9]
Explanation: It returns x to the power p, (x**p).
NumPy.lib.scimath.power method
The lib.scimath.power() method from the NumPy package is used to return the power to which the negative input value gets raised. The result is (x**p) where, if x contains positive values, it returns integers or float. and, if x contains negative values, the output is converted to the complex domain.
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 import the NumPy package and create an array 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 positive values is created in this example and raised to a power 2 using 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 2:
In this example array of complex numbers is given as input.
Python3
import numpy as np
array = np.array([ 1 + 1j , 2 + 2j , 3 + 3j ])
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.+1.j 2.+2.j 3.+3.j]
Shape of the array is : (3,)
The dimension of the array is : 1
Datatype of our Array is : complex128
[0. +2.j 0. +8.j 0.+18.j]
Share your thoughts in the comments
Please Login to comment...