NumPy 數(shù)學(xué)函數(shù)

三角函數(shù)

NumPy 提供了標(biāo)準(zhǔn)的三角函數(shù):sin()、cos()、tan()。

 import numpy as np 
 a = np.array([0,30,45,60,90])
 print ('不同角度的正弦值:')
 # 通過(guò)乘 pi/180 轉(zhuǎn)化為弧度 
 print (np.sin(a*np.pi/180))
 print ('\n')
 print ('數(shù)組中角度的余弦值:')
 print (np.cos(a*np.pi/180))
 print ('\n')
 print ('數(shù)組中角度的正切值:')
 print (np.tan(a*np.pi/180))

輸出結(jié)果為:

 不同角度的正弦值:
 [0. 0.5 0.70710678 0.8660254 1. ]
 數(shù)組中角度的余弦值:
 [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
  6.12323400e-17]
 數(shù)組中角度的正切值:
 [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
  1.63312394e+16]

arcsin,arccos,和 arctan 函數(shù)返回給定角度的 sin,cos 和 tan 的反三角函數(shù)。

這些函數(shù)的結(jié)果可以通過(guò) numpy.degrees() 函數(shù)將弧度轉(zhuǎn)換為角度。

 import numpy as np 
 a = np.array([0,30,45,60,90]) 
 print ('含有正弦值的數(shù)組:')
 sin = np.sin(a*np.pi/180) 
 print (sin)
 print ('\n')
 print ('計(jì)算角度的反正弦,返回值以弧度為單位:')
 inv = np.arcsin(sin) 
 print (inv)
 print ('\n')
 print ('通過(guò)轉(zhuǎn)化為角度制來(lái)檢查結(jié)果:')
 print (np.degrees(inv))
 print ('\n')
 print ('arccos 和 arctan 函數(shù)行為類似:')
 cos = np.cos(a*np.pi/180) 
 print (cos)
 print ('\n')
 print ('反余弦:')
 inv = np.arccos(cos) 
 print (inv)
 print ('\n')
 print ('角度制單位:')
 print (np.degrees(inv))
 print ('\n')
 print ('tan 函數(shù):')
 tan = np.tan(a*np.pi/180) 
 print (tan)
 print ('\n')
 print ('反正切:')
 inv = np.arctan(tan) 
 print (inv)
 print ('\n')
 print ('角度制單位:')
 print (np.degrees(inv))

輸出結(jié)果為:

 含有正弦值的數(shù)組:
 [0. 0.5 0.70710678 0.8660254 1. ]
 計(jì)算角度的反正弦,返回值以弧度為單位:
 [0. 0.52359878 0.78539816 1.04719755 1.57079633]
 通過(guò)轉(zhuǎn)化為角度制來(lái)檢查結(jié)果:
 [ 0. 30. 45. 60. 90.]
 arccos 和 arctan 函數(shù)行為類似:
 [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
  6.12323400e-17]
 反余弦:
 [0. 0.52359878 0.78539816 1.04719755 1.57079633]
 角度制單位:
 [ 0. 30. 45. 60. 90.]
 tan 函數(shù):
 [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
  1.63312394e+16]
 反正切:
 [0. 0.52359878 0.78539816 1.04719755 1.57079633]
 角度制單位:
 [ 0. 30. 45. 60. 90.]

舍入函數(shù)

numpy.around() 函數(shù)返回指定數(shù)字的四舍五入值。

 numpy.around(a,decimals)

參數(shù)說(shuō)明:

a: 數(shù)組 decimals: 舍入的小數(shù)位數(shù)。 默認(rèn)值為0。 如果為負(fù),整數(shù)將四舍五入到小數(shù)點(diǎn)左側(cè)的位置

 import numpy as np 
 a = np.array([1.0,5.55, 123, 0.567, 25.532]) 
 print ('原數(shù)組:')
 print (a)
 print ('\n')
 print ('舍入后:')
 print (np.around(a))
 print (np.around(a, decimals = 1))
 print (np.around(a, decimals = -1))

輸出結(jié)果為:

 原數(shù)組:
 [ 1. 5.55 123. 0.567 25.532]
 舍入后:
 [ 1. 6. 123. 1. 26.]
 [ 1. 5.6 123. 0.6 25.5]
 [ 0. 10. 120. 0. 30.]

numpy.floor()

numpy.floor() 返回小于或者等于指定表達(dá)式的最大整數(shù),即向下取整。

 import numpy as np 
 a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
 print ('提供的數(shù)組:')
 print (a)
 print ('\n')
 print ('修改后的數(shù)組:')
 print (np.floor(a))

輸出結(jié)果為:

 提供的數(shù)組:
 [-1.7 1.5 -0.2 0.6 10. ]
 修改后的數(shù)組:
 [-2. 1. -1. 0. 10.]

numpy.ceil()

numpy.ceil() 返回大于或者等于指定表達(dá)式的最小整數(shù),即向上取整。

 import numpy as np 
 a = np.array([-1.7, 1.5, -0.2, 0.6, 10]) 
 print ('提供的數(shù)組:')
 print (a)
 print ('\n')
 print ('修改后的數(shù)組:')
 print (np.ceil(a))
 提供的數(shù)組:
 [-1.7 1.5 -0.2 0.6 10. ]
 修改后的數(shù)組:
 [-1. 2. -0. 1. 10.]
丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清