一、基本函数
| 函数类别 | 数学表示 | Python函数名(math库) | Python函数名(numpy库) | 示例 |
|---|---|---|---|---|
| 幂函数 | $x^a$ | math.pow(x, a) 或 x**a |
np.power(x, a) |
math.pow(2, 3) → 8.0 |
| 指数函数(自然底数) | $e^x$ | math.exp(x) |
np.exp(x) |
math.exp(2) ≈ 7.389 |
| 指数函数(一般底数) | $a^x$ | 无直接函数,使用 a**x |
np.power(a, x) 或 np.exp2(x)(仅限$2^x$) |
2**3 → 8 |
| 自然对数 | $\ln x$ | math.log(x) |
np.log(x) |
$math.log(10) ≈ 2.3026$ |
| 常用对数 | $\lg x$ | math.log10(x) |
np.log10(x) |
math.log10(100) → 2.0 |
| 以2为底的对数 | $\log_2 x$ | math.log2(x) |
np.log2(x) |
math.log2(8) → 3.0 |
| 一般底数的对数 | $\log_a x$ | math.log(x, a) |
$np.log(x)/np.log(a)$(换底公式) | math.log(100, 10) → 2.0 |
| 绝对值函数 | $\lvert x \rvert$ | abs(x) |
np.abs(x) |
abs(-3.5) → 3.5 |
| 向下取整 | $\lfloor x \rfloor$ | math.floor(x) |
np.floor(x) |
math.floor(3.7) → 3 |
| 向上取整 | $\lceil x \rceil$ | math.ceil(x) |
np.ceil(x) |
math.ceil(3.2) → 4 |
| 四舍五入 | 四舍五入 | round(x) |
np.round(x) |
round(3.6) → 4 |
二、三角函数
| 函数类别 | 数学表示 | Python函数名(math库) | Python函数名(numpy库) | 注意事项 |
|---|---|---|---|---|
| 正弦函数 | $\sin x$ | math.sin(x) |
np.sin(x) |
参数为弧度 |
| 余弦函数 | $\cos x$ | math.cos(x) |
np.cos(x) |
参数为弧度 |
| 正切函数 | $\tan x$ | math.tan(x) |
np.tan(x) |
参数为弧度 |
| 反正弦函数 | $\arcsin x$ | math.asin(x) |
np.arcsin(x) |
返回值为弧度 |
| 反余弦函数 | $\arccos x$ | math.acos(x) |
np.arccos(x) |
返回值为弧度 |
| 反正切函数 | $\arctan x$ | math.atan(x) |
np.arctan(x) |
返回值为弧度 |
| 弧度转角度 | - | math.degrees(x) |
np.degrees(x) |
|
| 角度转弧度 | - | math.radians(x) |
np.radians(x) |
三、其他常见函数
| 函数类别 | 数学表示 | Python实现 | 示例 |
|---|---|---|---|
| 分段函数 | 例如: $f(x) = \begin{cases} x, & x \geq 0 \ -x, & x < 0 \end{cases}$ |
使用 if/else 或 np.where |
np.where(x>=0, x, -x) |
| 符号函数 | $\operatorname{sgn}(x)$ | np.sign(x) |
np.sign([-5, 0, 5]) → [-1, 0, 1] |
| 最大值函数 | $\max(x_1, x_2)$ | max(x1, x2) 或 np.maximum(x1, x2) |
np.maximum([1,3], [2,2]) → [2,3] |
| 最小值函数 | $\min(x_1, x_2)$ | min(x1, x2) 或 np.minimum(x1, x2) |
np.minimum([1,3], [2,2]) → [1,2] |
| 平方根函数 | $\sqrt{x}$ | math.sqrt(x) |
np.sqrt(x) |
| 开n次方 | $\sqrt[n]{x}$ | x**(1/n) 或 np.power(x, 1/n) |
8**(1/3) → 2.0 |
四、使用示例
import math
import numpy as np
# 1. 幂函数与指数函数
print("幂函数示例:")
print(f"math.pow(2, 3) = {math.pow(2, 3)}")
print(f"2**3 = {2**3}")
print(f"np.power(2, 3) = {np.power(2, 3)}")
print("\n指数函数示例:")
print(f"math.exp(2) = {math.exp(2)}")
print(f"np.exp(2) = {np.exp(2)}")
# 2. 对数函数
print("\n对数函数示例:")
print(f"math.log(100, 10) = {math.log(100, 10)}")
print(f"np.log10(100) = {np.log10(100)}")
# 3. 三角函数(角度转弧度)
print("\n三角函数示例:")
angle = 30
rad = math.radians(angle)
print(f"sin(30°) = {math.sin(rad)}")
print(f"cos(30°) = {math.cos(rad)}")
# 4. 分段函数
print("\n分段函数示例:")
x = np.array([-2, 0, 3])
y = np.where(x >= 0, x**2, -x)
print(f"x = {x}")
print(f"y = {y}")
# 5. 反三角函数
print("\n反三角函数示例:")
print(f"arcsin(0.5)的角度值 = {math.degrees(math.asin(0.5))}")
五、注意事项
- math库与numpy库的区别:
math库主要用于处理单个数值,是Python标准库的一部分numpy库适用于数组和矩阵运算,处理大量数据时效率更高-
三角函数参数均为弧度,使用前需进行角度转换
-
定义域限制:
- 对数函数要求真数>0(
math.log(负数)会报错) - 开偶次方要求底数≥0(
math.sqrt(-1)会报错) -
反三角函数定义域:arcsin和arccos为[-1, 1]
-
精度问题:
- 浮点数计算可能存在微小误差(如
math.sin(math.pi)不精确等于0) - 可设置小数位数或使用
round()函数控制精度
建议:科学计算或处理数组时优先使用 numpy,基础数学运算用 math 即可。