NumPy Math Functions
NumPy contains a large number of functions for various mathematical operations, including trigonometric functions, arithmetic operation functions, complex number processing functions, etc.
Trigonometric Functions
NumPy provides standard trigonometric functions: sin(), cos(), tan().
Example
The output result is:
不同角度的正弦值: [0. 0.5 0.70710678 0.8660254 1. ] 数组中角度的余弦值: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01 6.12323400e-17] 数组中角度的正切值: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16]
The arcsin, arccos, and arctan functions return the inverse trigonometric functions of sin, cos, and tan for given angles.
The results of these functions can be converted from radians to degrees using the numpy.degrees() function.
Example
The output result is:
含有正弦值的数组: [0. 0.5 0.70710678 0.8660254 1. ] 计算角度的反正弦,返回值以弧度为单位: [0. 0.52359878 0.78539816 1.04719755 1.57079633] 通过转化为角度制来检查结果: [ 0. 30. 45. 60. 90.] arccos 和 arctan 函数行为类似: [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 函数: [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.]
Rounding Functions
The numpy.around() function returns the rounded value of the specified number.
numpy.around(a,decimals)
Parameter description:
- a: array
- decimals: The number of decimal places to round to. The default is 0. If negative, rounding is done to the position to the left of the decimal point.
Example
The output result is:
原数组: [ 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() returns the largest integer less than or equal to the specified expression, i.e., rounding down.
Example
The output result is:
提供的数组: [-1.7 1.5 -0.2 0.6 10. ] 修改后的数组: [-2. 1. -1. 0. 10.]
numpy.ceil()
numpy.ceil() returns the smallest integer greater than or equal to the specified expression, i.e., rounding up.
Example
The output result is:
提供的数组: [-1.7 1.5 -0.2 0.6 10. ] 修改后的数组: [-1. 2. -0. 1. 10.]Other Extensions