使用 Python 中的 scimath 計算 10 為底的對數


若要使用 scimath 計算 10 為底的對數,請在 Python Numpy 中使用 scimath.log10() 方法。此方法返回 x 值(值)的以 10 為底的對數。如果 x 是標量,則 out 也為標量,否則返回陣列物件。

對於在 real x < 0 時返回 NAN 的 log10(),請使用 numpy.log10(不過請注意,否則 numpy.log10 和此 log10 是相同的,即對於 x = 0 都返回 -inf,對於 x = inf 返回 inf,特別是當 x.imag != 0 時返回復主值)。第一個引數 x 是(是)要計算其(其)以 10 為底的對數的值(值)。

步驟

首先匯入必需的庫 -

import numpy as np

使用 array() 方法建立 numpy 陣列 -

arr = np.array([10**1, -10**1, -10**2, 10**2, -10**3, 10**3])

顯示陣列 -

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 計算 10 為底的對數,請使用 scimath.log10() 方法 -

print("\nResult (log10)...\n",np.emath.log10(arr))

示例

import numpy as np

# Creating a numpy array using the array() method
arr = np.array([10**1, -10**1, -10**2, 10**2, -10**3, 10**3])

# 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 10 with scimath, use the scimath.log10() method in Python Numpy
print("\nResult (log10)...\n",np.emath.log10(arr))

輸出

Our Array...
[ 10 -10 -100 100 -1000 1000]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(6,)

Result (log10)...
[1.+0.j 1.+1.36437635j 2.+1.36437635j 2.+0.j
3.+1.36437635j 3.+0.j ]

已更新日期: 2022 年 3 月 1 日

129 次瀏覽

開啟你 事業

完成課程獲取認證

入門
廣告