用 Python 計算雙曲正切


要計算雙曲正切,在 Python Numpy 中使用 numpy.tanh() 方法。等同於 np.sinh(x)/np.cosh(x) 或 -1j * np.tan(1j*x)。返回相應的雙曲正切值。如果 x 是標量,則它是標量。第一個引數 x 是輸入陣列。第二個和第三個引數是可選的。

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

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

步驟

首先,匯入所需的庫 −

import numpy as np

獲取三角雙曲正切。查詢 tanh −

print("\nResult...",np.tanh(np.pi*1j))

查詢 tanh 90 度 −

print("\nResult...",np.tanh(np.pi/2.))

查詢 tanh 60 度 −

print("\nResult...",np.tanh(np.pi/3.))

查詢 tanh 45 度 −

print("\nResult...",np.tanh(np.pi/4.))

查詢 tanh 30 度 −

print("\nResult...",np.tanh(np.pi/6.))

查詢 tanh 0 度 −

print("\nResult...",np.tanh(0))

示例

import numpy as np

# To compute the Hyperbolic tangent, use the numpy.tanh() method in Python Numpy
# Equivalent to np.sinh(x)/np.cosh(x) or -1j * np.tan(1j*x).
# Returns the corresponding hyperbolic tangent values. This is a scalar if x is a scalar.

print("Get the Trigonometric Hyperbolic tangent...")

# find tanh
print("\nResult...",np.tanh(np.pi*1j))

# finding tanh 90 degrees
print("\nResult...",np.tanh(np.pi/2.))

# finding tanh 60 degrees
print("\nResult...",np.tanh(np.pi/3.))

# finding tanh 45 degrees
print("\nResult...",np.tanh(np.pi/4.))

# finding tanh 30 degrees
print("\nResult...",np.tanh(np.pi/6.))

# finding tanh 0 degrees
print("\nResult...",np.tanh(0))

輸出

Get the Trigonometric Hyperbolic tangent...

Result... -1.2246467991473532e-16j

Result... 0.9171523356672744

Result... 0.7807144353592677

Result... 0.6557942026326724

Result... 0.4804727781564516

Result... 0.0

更新日期:2022 年 2 月 24 日

405 次瀏覽

開始你的 職業

完成課程認證

入門
廣告