使用Python獲取以度數表示的角度陣列的三角餘弦值
要查詢以度數表示的角度陣列的三角餘弦值,請在Python NumPy中使用numpy.cos()方法。該方法返回第一個引數x的每個元素的餘弦值。第一個引數x是以弧度表示的角度(2pi表示360度)。這裡,它是一個角度陣列。第二個和第三個引數是可選的。
第二個引數是一個ndarray,結果儲存到其中的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果未提供或為None,則返回一個新分配的陣列。元組的長度必須等於輸出的數量。
第三個引數是條件,在輸入上進行廣播。在條件為True的位置,out陣列將設定為ufunc結果。在其他位置,out陣列將保留其原始值。請注意,如果透過預設的out=None建立未初始化的out陣列,則其中條件為False的位置將保持未初始化狀態。
步驟
首先,匯入所需的庫:
import numpy as np
以下是角度陣列,求cos 0、cos 30、cos 45、cos 60、cos 90、cos 180:
arr = np.array((0., 30., 45., 60., 90., 180.))
顯示我們的陣列:
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中使用cos()方法:
print("\nResult...",np.cos(arr * np.pi / 180. ))示例
import numpy as np
# To find the Trigonometric cosine of an array of angles given in degrees, use the numpy.cos() method in Python Numpy
# The method returns the cosine of each element of the 1st parameter x.
print("The Trigonometric cosine of an array of angles...")
# Array of angles
# finding cos 0, cos 30, cos 45, cos 60, cos 90, cos 180
arr = np.array((0., 30., 45., 60., 90., 180.))
# 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 cosines of an array of angles given in degrees, use the cos() method in Python Numpy
print("\nResult...",np.cos(arr * np.pi / 180. ))輸出
The Trigonometric cosine of an array of angles... Array... [ 0. 30. 45. 60. 90. 180.] Our Array type... float64 Our Array Dimensions... 1 Number of elements... 6 Result... [ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01 6.12323400e-17 -1.00000000e+00]
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP