在 Python 中獲取反餘弦值


反餘弦是一個多值函式:對於每個 x,都有無限多個數字 z 使得 cos(z) = x。慣例是返回實部位於 [0, pi] 範圍內的角度 z。對於實數值輸入資料型別,反餘弦始終返回實數輸出。對於每個不能表示為實數或無窮大的值,它會產生 nan 並設定無效浮點數錯誤標誌。對於複數值輸入,反餘弦是一個復解析函式,具有分支切割 [-inf, -1] 和 [1, inf],並且在前者上從上方連續,在後者上從下方連續。反餘弦也稱為 acos 或 cos^-1。

要查詢反餘弦,請在 Python Numpy 中使用 numpy.arccos() 方法。該方法返回以弧度表示的陣列與單位圓在給定 x 座標處相交的角度 [0, pi]。如果 x 是標量,則這是一個標量。第一個引數 x 是單位圓上的 x 座標。對於實數引數,域為 [-1, 1]。第二個和第三個引數是可選的。

第二個引數是一個 ndarray,結果儲存到其中。如果提供,它必須具有輸入廣播到的形狀。如果未提供或為 None,則返回一個新分配的陣列。元組的長度必須等於輸出的數量。

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

步驟

首先,匯入所需的庫 -

import numpy as np

獲取反餘弦。查詢 1 的反餘弦 -

print("\nResult...",np.arccos(1))

查詢 -1 的反餘弦 -

print("\nResult...",np.arccos(-1))

查詢 0 的反餘弦 -

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

查詢 0.3 的反餘弦 -

print("\nResult...",np.arccos(0.3))

示例

import numpy as np

# To find the Trigonometric inverse cosine, use the numpy.arccos() method in Python Numpy
# The method returns the angle of the array intersecting the unit circle at the given x-coordinate in radians [0, pi]. This is a scalar if x is a scalar.

print("Get the Trigonometric inverse cosine...")

# finding arccos for 1
print("\nResult...",np.arccos(1))

# finding arccos for -1
print("\nResult...",np.arccos(-1))

# finding arccos for 0
print("\nResult...",np.arccos(0))

# finding arccos for 0.3
print("\nResult...",np.arccos(0.3))

輸出

Get the Trigonometric inverse cosine...

Result... 0.0

Result... 3.141592653589793

Result... 1.5707963267948966

Result... 1.2661036727794992

更新於: 2022年2月25日

5K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.