在Python中使用scimath计算以2为底的对数
要使用scimath计算以2为底的对数,在Python的NumPy中使用np.emath.log2()方法。该方法返回x值的以2为底的对数。如果x是标量,则输出也是标量,否则返回一个数组。第一个参数x是需要计算对数的值。
步骤
首先,导入所需的库 –
import numpy as np
使用array()方法创建一个numpy数组−
arr = np.array([np.inf, -np.inf, 16, np.exp(1), -np.exp(1), -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)
使用Python NumPy中的np.emath.log2()方法来计算以2为底的对数
该方法返回x值的以2为底的对数。如果x是标量,则返回标量,否则返回数组−
print("\nResult (log2)...\n",np.emath.log2(arr))
示例
import numpy as np
# Creating a numpy array using the array() method
arr = np.array([np.inf, -np.inf, 16, np.exp(1), -np.exp(1), -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 compute the logarithm base 2 with scimath, use the np.emath.log2() method in Python Numpy
# The method returns the log base 2 of the x value(s). If x was a scalar, so is out, otherwise an array is returned.
print("\nResult (log2)...\n",np.emath.log2(arr))
输出
Our Array...
[ inf -inf 16. 2.71828183 -2.71828183
-32. ]
Dimensions of our Array...
1
Datatype of our Array object...
float64
Shape of our Array object...
(6,)
Result (log2)...
[ inf+0.j inf+4.53236014j 4. +0.j
1.44269504+0.j 1.44269504+4.53236014j 5. +4.53236014j]