• NumPy数学算数函数


    NumPy - 算数函数

    很容易理解的是,NumPy 包含大量的各种数学运算功能。 NumPy 提供标准的三角函数,算术运算的函数,复数处理函数等。

    三角函数

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

    示例

    import numpy as np
    a = np.array([0,30,45,60,90])  
    print  '不同角度的正弦值:'  
    # 通过乘 pi/180 转化为弧度  
    print np.sin(a*np.pi/180)  
    print  '
    '  
    print  '数组中角度的余弦值:'  
    print np.cos(a*np.pi/180)  
    print  '
    '  
    print  '数组中角度的正切值:'  
    print np.tan(a*np.pi/180)
    
    Python

    输出如下:

    不同角度的正弦值:                                                   
    [ 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]
    

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

    示例

    import numpy as np
    a = np.array([0,30,45,60,90])  
    print  '含有正弦值的数组:'
    sin = np.sin(a*np.pi/180)  
    print sin
    print  '
    '  
    print  '计算角度的反正弦,返回值以弧度为单位:'
    inv = np.arcsin(sin)  
    print inv
    print  '
    '  
    print  '通过转化为角度制来检查结果:'  
    print np.degrees(inv)  
    print  '
    '  
    print  'arccos 和 arctan 函数行为类似:'
    cos = np.cos(a*np.pi/180)  
    print cos
    print  '
    '  
    print  '反余弦:'
    inv = np.arccos(cos)  
    print inv
    print  '
    '  
    print  '角度制单位:'  
    print np.degrees(inv)  
    print  '
    '  
    print  'tan 函数:'
    tan = np.tan(a*np.pi/180)  
    print tan
    print  '
    '  
    print  '反正切:'
    inv = np.arctan(tan)  
    print inv
    print  '
    '  
    print  '角度制单位:'  
    print np.degrees(inv)
    
    Python

    输出如下:

    含有正弦值的数组:
    [ 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.]
    

    舍入函数

    numpy.around()

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

    numpy.around(a,decimals)
    
    Python

    其中:

    序号参数及描述
    1. a 输入数组
    2. decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置

    示例

    import numpy as np
    a = np.array([1.0,5.55,  123,  0.567,  25.532])  
    print  '原数组:'  
    print a
    print  '
    '  
    print  '舍入后:'  
    print np.around(a)  
    print np.around(a, decimals =  1)  
    print np.around(a, decimals =  -1)
    
    Python

    输出如下:

    原数组:                                                          
    [   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()

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

    示例

    import numpy as np
    a = np.array([-1.7,  1.5,  -0.2,  0.6,  10])  
    print  '提供的数组:'  
    print a
    print  '
    '  
    print  '修改后的数组:'  
    print np.floor(a)
    
    Python

    输出如下:

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

    numpy.ceil()

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

    示例

    import numpy as np
    a = np.array([-1.7,  1.5,  -0.2,  0.6,  10])  
    print  '提供的数组:'  
    print a
    print  '
    '  
    print  '修改后的数组:'  
    print np.ceil(a)
    
    Python

    输出如下:

    提供的数组:
    [ -1.7   1.5  -0.2   0.6  10. ]
    
    修改后的数组:
    [ -1.   2.  -0.   1.  10.]
  • 相关阅读:
    Shell编程(一)为什么使用Shell编程
    ALSA驱动Debian声卡
    Shell编程(五)find与grep命令简介及正则表达式
    Shell编程(三)控制结构及函数
    初识Linux程序
    Gentoo的哲学
    学习Emacs
    Shell编程(二)Shell基本语法
    第一杯咖啡在Debian 上安装Java环境
    Fvwm 笔记
  • 原文地址:https://www.cnblogs.com/navysummer/p/9640795.html
Copyright © 2020-2023  润新知