使用 Python 中的 scimath 计算以 n 为底的对数
要使用 scimath 计算以 n 为底的对数,请在 Python Numpy 中使用 scimath.logn() 方法。此方法返回 x 值的对数(以 n 为底)。如果 x 是标量,则输出也是标量;否则,返回数组。
如果 x 包含负输入,则计算答案并在复数域中返回。第一个参数 n 是取对数的整数底数。第二个参数 x 是需要计算以 n 为底的对数的值。
步骤
首先,导入所需的库:
import numpy as np
使用 array() 方法创建一个 NumPy 数组:
arr = np.array([-4, -8, 8])
显示数组:
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 计算以 n 为底的对数,请在 Python Numpy 中使用 scimath.logn() 方法。此方法返回 x 值的对数(以 n 为底)。如果 x 是标量,则输出也是标量;否则,返回数组:
print("\nResult (logn)...\n",np.emath.logn(2, arr))
示例
import numpy as np # Creating a numpy array using the array() method arr = np.array([-4, -8, 8]) # 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 n with scimath, use the scimath.logn() method in Python Numpy # The method returns the log base n of the x value(s). If x was a scalar, so is out, otherwise an array is returned. print("\nResult (logn)...\n",np.emath.logn(2, arr))
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
输出
Our Array... [-4 -8 8] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (3,) Result (logn)... [2.+4.53236014j 3.+4.53236014j 3.+0.j ]
广告