NumPy - 数学函数
NumPy - 数学函数
Section titled “NumPy - 数学函数”NumPy 提供丰富的数学函数库,这些函数在数组上按元素进行操作。这些通用函数(Universal Functions,ufuncs)包括标准三角函数运算、舍入函数、算术运算、复数函数等。
NumPy 包含标准的三角函数,如 np.sin()、np.cos() 和 np.tan()。这些函数期望输入的角度单位为弧度(radians)。您可以通过乘以 np.pi / 180 将角度(degrees)转换为弧度。
示例:计算正弦、余弦和正切
将三角函数应用于角度数组(以度为单位)。
import numpy as np
angles_deg = np.array([0, 30, 45, 60, 90])angles_rad = angles_deg * np.pi / 180 # Convert degrees to radians
print('Angles in Degrees:', angles_deg)print('Angles in Radians:', angles_rad)print('\n')
print('Sine values:')print(np.sin(angles_rad))print('\n')
print('Cosine values:')print(np.cos(angles_rad))print('\n')
print('Tangent values:')print(np.tan(angles_rad))输出(浮点数值可能存在微小差异):
Angles in Degrees: [ 0 30 45 60 90]Angles in Radians: [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Sine values:[0. 0.5 0.70710678 0.8660254 1. ]
Cosine values:[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01 6.12323400e-17]
Tangent values:[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16]也提供了反三角函数(np.arcsin()、np.arccos()、np.arctan())。它们返回与给定三角比对应的以弧度为单位的角度。您可以使用 np.degrees() 将结果转换回角度以进行验证。
示例:反三角函数
计算反正弦并转换回角度。
import numpy as np
angles_deg = np.array([0, 30, 45, 60, 90])angles_rad = np.deg2rad(angles_deg) # Use np.deg2rad for conveniencesine_values = np.sin(angles_rad)
print('Original angles (degrees):', angles_deg)print('Sine values:', sine_values)print('\n')
print('Inverse sine (result in radians):')inv_sine_rad = np.arcsin(sine_values)print(inv_sine_rad)print('\n')
print('Inverse sine converted back to degrees:')inv_sine_deg = np.degrees(inv_sine_rad)print(inv_sine_deg)print('\n')
# Similar process applies to arccos and arctancosine_values = np.cos(angles_rad)inv_cosine_deg = np.degrees(np.arccos(cosine_values))print('Inverse cosine (degrees):', inv_cosine_deg)输出(浮点数值可能存在微小差异):
Original angles (degrees): [ 0 30 45 60 90]Sine values: [0. 0.5 0.70710678 0.8660254 1. ]
Inverse sine (result in radians):[0. 0.52359878 0.78539816 1.04719755 1.57079633]
Inverse sine converted back to degrees:[ 0. 30. 45. 60. 90.]
Inverse cosine (degrees): [ 0. 30. 45. 60. 90.]类似的过程也适用于 arccos 和 arctan。
numpy.around()
Section titled “numpy.around()”将数组元素四舍五入到指定的小数位数(decimal places)。对于恰好介于两个可表示数字之间的值,它使用“四舍六入五成双”(round half to even)的策略(例如,2.5 舍入到 2.0,3.5 舍入到 4.0)。
numpy.around(a, decimals=0)参数:
| 参数 | 描述 |
|---|---|
| a | 输入数组。 |
| decimals | 要舍入的小数位数(默认为 0)。如果为负,则舍入到小数点左侧的位数。 |
示例:使用 np.around
演示如何舍入到不同的小数位数。
import numpy as np
a = np.array([1.0, 5.55, 123, 0.567, 25.532, -1.25])
print('Original array:')print(a)print('\n')
print('Rounded to 0 decimals (default):')print(np.around(a))
print('\nRounded to 1 decimal place:')print(np.around(a, decimals=1))
print('\nRounded to -1 decimal place (nearest 10):')print(np.around(a, decimals=-1))输出:
Original array:[ 1. 5.55 123. 0.567 25.532 -1.25 ]
Rounded to 0 decimals (default):[ 1. 6. 123. 1. 26. -1.]
Rounded to 1 decimal place:[ 1. 5.6 123. 0.6 25.5 -1.2]
Rounded to -1 decimal place (nearest 10):[ 0. 10. 120. 0. 30. -0.]numpy.floor()
Section titled “numpy.floor()”返回每个元素的向下取整值(floor),即小于或等于该元素的最大整数。
示例:使用 np.floor
找到不大于每个元素的最大整数。
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10.0])
print('Original array:')print(a)print('\n')
print('After applying np.floor():')print(np.floor(a))输出:
Original array:[-1.7 1.5 -0.2 0.6 10. ]
After applying np.floor():[-2. 1. -1. 0. 10.]numpy.ceil()
Section titled “numpy.ceil()”返回每个元素的向上取整值(ceiling),即大于或等于该元素的最小整数。
示例:使用 np.ceil
找到不小于每个元素的最小整数。
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10.0])
print('Original array:')print(a)print('\n')
print('After applying np.ceil():')print(np.ceil(a))输出:
Original array:[-1.7 1.5 -0.2 0.6 10. ]
After applying np.ceil():[-1. 2. -0. 1. 10.]