使用Python中的scimath返回输入值负次幂的结果
要返回使用scimath计算输入值次幂的结果,请使用Python中的scimath.power()方法。返回x的p次幂的结果,即x**p的结果。如果x和p是标量,则输出也是标量,否则返回数组。如果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...
",arr)
检查维度:
print("
Dimensions of our Array...
",arr.ndim)
获取数据类型:
print("
Datatype of our Array object...
",arr.dtype)
获取形状:
print("
Shape of our Array object...
",arr.shape)
要返回使用scimath计算输入值次幂的结果,请使用Python中的scimath.power()方法:
print("
Result...
",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...
",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))
输出
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]
广告