在Python中獲取陣列元素的反三角餘弦


反餘弦是一個多值函式:對於每個x,都有無限多個數字z使得cos(z) = x。慣例是返回實部位於[0, π]範圍內的角度z。反餘弦也稱為acos或cos^-1。

對於實數值輸入資料型別,arccos始終返回實數輸出。對於不能表示為實數或無窮大的每個值,它會產生nan並設定無效浮點錯誤標誌。對於複數值輸入,arccos是一個復解析函式,它具有分支切割[-inf, -1]和[1, inf],並且在前者上從上方連續,在後者上從下方連續。

要查詢陣列元素的反三角餘弦,請在Python NumPy中使用numpy.arccos()方法。該方法返回以弧度[0, π]表示的與單位圓在給定x座標處相交的角度。如果x是標量,則這是一個標量。

第一個引數x是單位圓上的x座標。對於實數引數,域為[-1, 1],第二個和第三個引數是可選的。第二個引數是ndarray,結果儲存在其中的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果未提供或為None,則返回一個新分配的陣列。

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

步驟

首先,匯入所需的庫:

import numpy as np

獲取陣列元素的反三角餘弦。使用numpy.array()方法建立陣列:

arr = np.array((1, -1, 0, 0.3))

顯示我們的陣列:

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)

查詢陣列元素的反三角餘弦:

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

示例

import numpy as np

# To find the Trigonometric inverse cosine of the array elements, 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.
# The 1st parameter, x is the x-coordinate on the unit circle. For real arguments, the domain is [-1, 1].

print("Get the Trigonometric inverse cosine of the array elements...\n")

# Array created using the numpy.array() method
arr = np.array((1, -1, 0, 0.3))

# 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)

# Finding the Trigonometric inverse cosine of the array elements
print("\nResult...",np.arccos(arr))

輸出

Get the Trigonometric inverse cosine of the array elements...

Array...
[ 1. -1. 0. 0.3]

Our Array type...
float64

Our Array Dimensions...
1

Number of elements...
4

Result... [0. 3.14159265 1.57079633 1.26610367]

更新於:2022年2月25日

瀏覽量:183

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.