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


要計算陣列元素的雙曲正弦,請在 Python NumPy 中使用 numpy.sinh() 方法。此方法等效於 1/2 * (np.exp(x) - np.exp(-x)) 或 -1j * np.sin(1j*x)。返回相應的雙曲正弦值。如果 x 是標量,則這是一個標量。第一個引數 x 是輸入陣列。第二個和第三個引數是可選的。

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

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

步驟

首先,匯入所需的庫:

import numpy as np

獲取陣列元素的三角雙曲正弦。使用 NumPy 中的 array() 方法建立一個數組:

arr = np.array((0., 30., 45., 60., 90., 180., np.pi*1j/2, np.pi*1j))

顯示我們的陣列:

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.sinh() 方法:

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

示例

import numpy as np

# To compute the Hyperbolic sine of the array elements, use the numpy.sinh() method in Python Numpy
# The method is equivalent to 1/2 * (np.exp(x) - np.exp(-x)) or -1j * np.sin(1j*x).

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

# Create an array using the array() method in Numpy
arr = np.array((0., 30., 45., 60., 90., 180., np.pi*1j/2, np.pi*1j))

# Display the array
print("Array...\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 hyperbolic sines of the array elements, use the numpy.sinh() method in Python Numpy
print("\nResult...",np.sinh(arr))

輸出

Get the Trigonometric Hyperbolic sine of the array elements...
Array...
[ 0.+0.j    30.+0.j    45.+0.j           60.+0.j
90.+0.j    180.+0.j    0.+1.57079633j    0.+3.14159265j]

Our Array type...
complex128

Our Array Dimensions...
1

Number of elements...
8

Result... [ 0.00000000e+00+0.0000000e+00j 5.34323729e+12+0.0000000e+00j
1.74671355e+19+0.0000000e+00j 5.71003695e+25+0.0000000e+00j
6.10201647e+38+0.0000000e+00j 7.44692100e+77+0.0000000e+00j
0.00000000e+00+1.0000000e+00j -0.00000000e+00+1.2246468e-16j]

更新於:2022年2月25日

165 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告