数学函数

三角函数

angle_num = np.array([0, 30, 45, 60, 90])
print(f'各个角度的正弦值:{np.sin(angle_num * np.pi/180)}')

# 输出结果:
#  各个角度的正弦值:[0.         0.5        0.70710678 0.8660254  1.        ]

print(f'各个角度的余弦值:{np.cos(angle_num * np.pi/180)}')

# 输出结果:
#  各个角度的余弦值:[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
 6.12323400e-17]

print(f'各个角度的正切值:{np.tan(angle_num * np.pi/180)}')

# 输出结果:
#  各个角度的正切值:[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
 1.63312394e+16]

反正弦函数:

angle_num = np.array([0, 30, 45, 60, 90])
sin_val = np.sin(angle_num * np.pi/180)
print(f'各个角度的正弦值:{sin_val}')

# 输出结果:
#  各个角度的正弦值:[0.         0.5        0.70710678 0.8660254  1.        ]

inv = np.arcsin(sin_val)
print(f'各个角度的反正弦弧度值:{inv}')

# 输出结果:
#  各个角度的反正弦弧度值:[0.         0.52359878 0.78539816 1.04719755 1.57079633]

inv_degree = np.degrees(inv)
print(f'反正弦角度:{inv_degree}')

# 输出结果:
#  反正弦角度:[ 0. 30. 45. 60. 90.]

舍入函数

返回指定数字的四舍五入值

语法:

numpy.around(a, decimals)

参数 说明
a 数组
decimals 舍入的小数位数,默认值为 0.如果为负,则整数将四舍五入到小数点左侧的位置
np.set_printoptions(suppress=True)  # 以小数显示
num_np = np.array([0.5, 1.0, 2.55, 13.312, 68,6789, 125.132, 357.78])
print(f'原始数组:{num_np}')

# 输出结果:
#  原始数组:[   0.5      1.       2.55    13.312   68.    6789.     125.132  357.78 ]

print(f'舍入一位小数:{np.around(num_np,1)}')

# 输出结果:
#  舍入一位小数:[   0.5    1.     2.6   13.3   68.  6789.   125.1  357.8]

print(f'舍入到十位小数:{np.around(num_np,-1)}')
# 输出结果:
#  舍入到十位小数:[   0.    0.    0.   10.   70. 6790.  130.  360.]

np.floor()

返回数字的下舍整数

num_np = np.array([-11.7, 3.6, -1.2, 0.32, 6.3, 10, 218.29])
print(f'初始数组:{num_np}')

# 输出结果:
#  初始数组:[-11.7    3.6   -1.2    0.32   6.3   10.   218.29]

print(f'下舍后的数组:{np.floor(num_np)}')

# 输出结果:
#  下舍后的数组:[-12.   3.  -2.   0.   6.  10. 218.]

np.ceil()

返回数组的上入整数

num_np = np.array([-11.7, 3.6, -1.2, 0.32, 6.3, 10, 218.29])

print(f'初始数组:{num_np}')

# 输出结果:
#  初始数组:[-11.7    3.6   -1.2    0.32   6.3   10.   218.29]

print(f'上入后的数组:{np.ceil(num_np)}')

# 输出结果:
#  上入后的数组:[-11.   4.  -1.   1.   7.  10. 219.]