使用scimath.power()方法返回Python中输入值的幂结果
要返回使用scimath将输入值提高到的幂的结果,请在Python中使用scimath.power()方法。返回x的p次幂,即x**p的结果。如果x和p是标量,则out也是标量,否则将返回数组。如果x包含负值,则输出将转换为复数域。参数x是输入值
参数p是将x提高到的幂。如果x包含多个值,p必须是标量或包含与x相同数量的值。在后一种情况下,结果是x[0]**p[0],x[1]**p[1],....
步骤
首先,导入所需的库-
import numpy as np
使用array()方法创建一个numpy数组:
arr = np.array([2, 4, 8, 16, 32])
显示数组−
print("Our Array...\n",arr)
检查尺寸 −
print("\nDimensions of our Array...\n",arr.ndim)
获取数据类型 −
print("\nDatatype of our Array object...\n",arr.dtype)
获取形状 –
print("\nShape of our Array object...\n",arr.shape)
用scimath.power()方法在Python中返回输入值的幂运算结果-
print("\nResult...\n",np.emath.power(arr, 2))
示例
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...\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))
结果
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 16 64 256 1024]