使用Python中的scimath计算自然对数
要使用scimath计算自然对数,请在Python的Numpy中使用np.emath.log()方法。该方法返回x值的对数。如果x是标量,则输出也是标量,否则将返回一个数组。第一个参数x是需要计算对数的值(们)。
步骤
首先导入所需的库 –
import numpy as np
创建一个使用array()方法的numpy数组−
arr = np.array([np.inf, -np.inf, np.exp(1), -np.exp(1)])
显示数组 –
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计算自然对数,在Python Numpy中使用np.emath.log()方法 –
print("\nResult (log)...\n",np.emath.log(arr))
示例
import numpy as np
# Creating a numpy array using the array() method
arr = np.array([np.inf, -np.inf, np.exp(1), -np.exp(1)])
# 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 natural logarithm with scimath, use the np.emath.log() method in Python Numpy.
print("\nResult (log)...\n",np.emath.log(arr))
输出
Our Array...
[ inf -inf 2.71828183 -2.71828183]
Dimensions of our Array...
1
Datatype of our Array object...
float64
Shape of our Array object...
(4,)
Result (log)...
[inf+0.j inf+3.14159265j 1.+0.j 1.+3.14159265j]