在 Python 中計算陣列元素的反雙曲正切


反雙曲正切是一個多值函式:對於每個 x,都有無限多個數字 z 使得 tanh(z) = x。慣例是返回虛部位於 [-pi/2, pi/2] 的 z。反雙曲正切也稱為 atanh 或 tanh^-1。

要計算陣列元素的反雙曲正切,請在 Python Numpy 中使用 numpy.arctanh() 方法。該方法返回與 x 形狀相同的陣列。如果 x 是標量,則為標量。第一個引數 x 是輸入陣列。第二個和第三個引數是可選的。

第二個引數是 ndarray,結果儲存到的位置。如果提供,則其形狀必須是輸入廣播到的形狀。如果未提供或為 None,則返回一個新分配的陣列。

第三個引數是條件在輸入上廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。

步驟

首先,匯入所需的庫 -

import numpy as np

使用 Numpy 中的 array() 方法建立一個數組 -

arr = np.array((0, 0.2, 0.3, 0.5, 0.11))

顯示我們的陣列 -

print("Array...\n",arr)

獲取資料型別 -

print("\nArray datatype...\n",arr.dtype)

獲取陣列的維度 -

print("\nArray Dimensions...\n",arr.ndim)

獲取陣列的元素個數 -

print("\nNumber of elements in the Array...\n",arr.size)

要查詢陣列元素的反雙曲正切,請在 Python Numpy 中使用 numpy.arctanh() 方法 -

print("\nResult...",np.arctanh(arr))

示例

import numpy as np

# To compute the inverse Hyperbolic tangent of array elements, use the numpy.arctanh() method in Python Numpy
# The method returns the array of the same shape as x. This is a scalar if x is a scalar.
# The 1st parameter, x is input array

print("Get the Trigonometric inverse Hyperbolic tangent of array elements...")

# Create an array using the array() method in Numpy
arr = np.array((0, 0.2, 0.3, 0.5, 0.11))

# Display the array
print("\nArray...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimensions...\n",arr.ndim)

# Get the number of elements in the Array
print("\nNumber of elements...\n", arr.size)

# To find the inverse hyperbolic tangent of the array elements, use the numpy.arctanh() method in Python Numpy
print("\nResult...",np.arctanh(arr))

輸出

Get the Trigonometric inverse Hyperbolic tangent of array elements...

Array...
[0. 0.2 0.3 0.5 0.11]

Our Array type...
float64

Our Array Dimensions...
1

Number of elements...
5

Result... [0. 0.20273255 0.3095196 0.54930614 0.11044692]

更新於: 2022-02-28

138 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告