使用scimath在Python中返回输入值的负幂的结果
要使用scimath.power()方法返回输入值的幂结果,请使用Python。 返回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...
",arr)
检查尺寸 –
print("
Dimensions of our Array...
",arr.ndim)
获取数据类型 –
print("
Datatype of our Array object...
",arr.dtype)
获取形状-
print("
Shape of our Array object...
",arr.shape)
要返回提供的输入值的幂运算结果,可以使用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]