目录
当前位置: 首页 > 文档资料 > NumPy 教程 >

算数函数

优质
小牛编辑
135浏览
2023-12-01

NumPy 拥有标准的三角函数,它为弧度制单位的给定角度返回三角函数比值。

示例

输出如下:

  1. 不同角度的正弦值:
  2. [ 0. 0.5 0.70710678 0.8660254 1. ]
  3. 数组中角度的余弦值:
  4. [ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
  5. 6.12323400e-17]
  6. 数组中角度的正切值:
  7. [ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
  8. 1.63312394e+16]

arcsinarccos,和arctan函数返回给定角度的sincostan的反三角函数。 这些函数的结果可以通过numpy.degrees()函数通过将弧度制转换为角度制来验证。

  1. import numpy as np
  2. a = np.array([0,30,45,60,90])
  3. print '含有正弦值的数组:'
  4. sin = np.sin(a*np.pi/180)
  5. print sin
  6. print '\n'
  7. print '计算角度的反正弦,返回值以弧度为单位:'
  8. inv = np.arcsin(sin)
  9. print inv
  10. print '通过转化为角度制来检查结果:'
  11. print '\n'
  12. print 'arccos 和 arctan 函数行为类似:'
  13. cos = np.cos(a*np.pi/180)
  14. print cos
  15. print '\n'
  16. print '反余弦:'
  17. inv = np.arccos(cos)
  18. print inv
  19. print '\n'
  20. print '角度制单位:'
  21. print np.degrees(inv)
  22. print '\n'
  23. print 'tan 函数:'
  24. tan = np.tan(a*np.pi/180)
  25. print tan
  26. print '\n'
  27. print '反正切:'
  28. inv = np.arctan(tan)
  29. print inv
  30. print '\n'
  31. print '角度制单位:'
  32. print np.degrees(inv)

输出如下:

舍入函数

这个函数返回四舍五入到所需精度的值。 该函数接受以下参数。

    其中:

    示例

    1. import numpy as np
    2. a = np.array([1.0,5.55, 123, 0.567, 25.532])
    3. print '原数组:'
    4. print '\n'
    5. print '舍入后:'
    6. print np.around(a)
    7. print np.around(a, decimals = 1)
    8. print np.around(a, decimals = -1)

    此函数返回不大于输入参数的最大整数。 即标量x 的下限是最大的整数i ,使得i <= x。 注意在Python中,向下取整总是从 0 舍入。

    示例

    1. import numpy as np
    2. a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
    3. print '提供的数组:'
    4. print a
    5. print '\n'
    6. print '修改后的数组:'
    7. print np.floor(a)

    输出如下:

    1. 提供的数组:
    2. [ -1.7 1.5 -0.2 0.6 10. ]
    3. 修改后的数组:
    4. [ -2. 1. -1. 0. 10.]

    ceil()函数返回输入值的上限,即,标量x的上限是最小的整数i ,使得i> = x

    输出如下:

    1. 提供的数组:
    2. [ -1.7 1.5 -0.2 0.6 10. ]
    3. [ -1. 2. -0. 1. 10.]