使用 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))

輸出

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 ]

更新於:2022年3月1日

331 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告