NumPy 数学函数

2024 年 8 月 29 日 | 阅读 3 分钟

Numpy 包含大量可用于执行各种数学运算的数学函数。数学函数包括三角函数、算术函数和用于处理复数的函数。让我们讨论一下数学函数。

三角函数

Numpy 包含三角函数,用于计算不同角度的正弦、余弦和正切(以弧度为单位)。

sin、cos 和 tan 函数返回指定角度的三角比。请看以下示例。

示例

输出

The sin value of the angles 
[0.00000000e+00 5.00000000e-01 8.66025404e-01 1.00000000e+00
 8.66025404e-01 5.00000000e-01 1.22464680e-16]

The cosine value of the angles 
[ 1.00000000e+00  8.66025404e-01  5.00000000e-01  6.12323400e-17
 -5.00000000e-01 -8.66025404e-01 -1.00000000e+00]

The tangent value of the angles [ 0.00000000e+00  5.77350269e-01  1.73205081e+00  1.63312394e+16
 -1.73205081e+00 -5.77350269e-01 -1.22464680e-16]

另一方面,arcsin()、arccos() 和 arctan() 函数返回指定角度的三角逆函数。

numpy.degrees() 函数可用于验证这些三角函数的结果。请看以下示例。

示例

输出

printing the sin values of different angles
[0.        0.5       0.8660254 1.       ]
printing the inverse of the sin
[0.         0.52359878 1.04719755 1.57079633]
printing the values in degrees
[ 0. 30. 60. 90.]

printing the cos values of different angles
[1.00000000e+00 8.66025404e-01 5.00000000e-01 6.12323400e-17]
printing the inverse of the cos
[0.         0.52359878 1.04719755 1.57079633]

printing the values in degrees
[ 0. 30. 60. 90.]

printing the tan values of different angles
[0.00000000e+00 5.77350269e-01 1.73205081e+00 1.63312394e+16]
printing the inverse of the tan
[0.         0.52359878 1.04719755 1.57079633]

printing the values in degrees
[ 0. 30. 60. 90.]

舍入函数

Numpy 提供了各种可用于截断小数浮点数的值,将其四舍五入到特定精度的小数位数的函数。让我们讨论一下舍入函数。

numpy.around() 函数

此函数返回一个四舍五入到所需小数位数的十进制值。该函数的语法如下所示。

它接受以下参数。

序号参数描述
1num它是输入数字。
2小数位数这是要将数字四舍五入到的小数位数。默认值为 0。如果此值为负数,则小数点将向左移动。

请看以下示例。

示例

输出

printing the original array values: [ 12.202   90.2312 123.02    23.202 ]
Array values rounded off to 2 decimal position [ 12.2   90.23 123.02  23.2 ]
Array values rounded off to -2 decimal position [ 10.  90. 120.  20.]

numpy.floor() 函数

此函数用于返回输入数据的向下取整值,即不大于输入值的最大整数。请看以下示例。

示例

输出

[ 12.  90. 123.  23.]

numpy.ceil() 函数

此函数用于返回数组值的向上取整值,即大于数组元素的最小整数值。请看以下示例。

示例

输出

[ 13.  91. 124.  24.]

下一个主题Numpy 统计函数