[2025-12-10]
Example 1.1 梯度
Grad[v[x, y, z], {x, y, z}]
$$\nabla _{\{x,y,z\}}v(x,y,z)=\left(\begin{array}{c} v^{(1,0,0)}(x,y,z) \\ v^{(0,1,0)}(x,y,z) \\ v^{(0,0,1)}(x,y,z) \\\end{array}\right)$$
Example 1.2 散度
Div[{v1[x, y, z], v2[x, y, z], v3[x, y, z]}, {x, y, z}]
$$\nabla _{\{x,y,z\}}\cdot \{\text{v1}(x,y,z),\text{v2}(x,y,z),\text{v3}(x,y,z)\}=\text{v1}^{(1,0,0)}(x,y,z)+\text{v2}^{(0,1,0)}(x,y,z)+\text{v3}^{(0,0,1)}(x,y,z)$$
Example 1.3 旋度
v = Function[{x, y, z}, {v1[x, y, z], v2[x, y, z], v3[x, y, z]}];
Curl[v[x, y, z], {x, y, z}]
$$\nabla _{\{x,y,z\}}\times v(x,y,z)=\left(\begin{array}{c} \text{v3}^{(0,1,0)}(x,y,z)-\text{v2}^{(0,0,1)}(x,y,z) \\ \text{v1}^{(0,0,1)}(x,y,z)-\text{v3}^{(1,0,0)}(x,y,z) \\ \text{v2}^{(1,0,0)}(x,y,z)-\text{v1}^{(0,1,0)}(x,y,z) \\\end{array}\right)$$
Example 1.5
v = Function[{x, y, z}, {x, y, z}];
Curl[v[x, y, z], {x, y, z}] (* => {0, 0, 0} *)
Div[v[x, y, z], {x, y, z}] (* => 3 *)
Example 1.6 $\nabla^2$
Laplacian[v[x, y, z], {x, y, z}]
$$\nabla _{\{x,y,z\}}^2v(x,y,z)=v^{(0,0,2)}(x,y,z)+v^{(0,2,0)}(x,y,z)+v^{(2,0,0)}(x,y,z)$$
Example 1.7 $\nabla \times \nabla$
Curl[Grad[v[x, y, z], {x, y, z}], {x, y, z}]
$$\nabla _{\{x,y,z\}}\times \nabla _{\{x,y,z\}}v(x,y,z)=\left(\begin{array}{c} 0 \\ 0 \\ 0 \\\end{array}\right)$$
Example 1.8 $\nabla \cdot (\nabla \times \star)$
Div[Curl[{v1[x, y, z], v2[x, y, z], v3[x, y, z]}, {x, y, z}], {x, y, z}]
$$\nabla _{\{x,y,z\}}\cdot (\nabla _{\{x,y,z\}}\times \{\text{v1}(x,y,z),\text{v2}(x,y,z),\text{v3}(x,y,z)\}) = 0$$
Example 1.9 非 Cartesian 坐标系
Grad[p[r, phi, z], {r, phi, z}, "Cylindrical"] (* 柱坐标系 *)
Laplacian[p[r, phi, z], {r, phi, z}, "Spherical"] (* 球坐标系 *)
$$\text{Grad}[p(r,\phi ,z),\{r,\phi ,z\},\text{Cylindrical}]=\left(\begin{array}{c} p^{(1,0,0)}(r,\phi ,z) \\ \frac{p^{(0,1,0)}(r,\phi ,z)}{r} \\ p^{(0,0,1)}(r,\phi ,z) \\\end{array}\right)$$
$$\text{Laplacian}[p(r,\phi ,z),\{r,\phi ,z\},\text{Spherical}]=\frac{\frac{p^{(0,2,0)}(r,\phi ,z)}{r}+p^{(1,0,0)}(r,\phi ,z)}{r}+p^{(2,0,0)}(r,\phi ,z)+\frac{\csc (\phi ) \left(\sin (\phi ) p^{(1,0,0)}(r,\phi ,z)+\frac{\cos (\phi ) p^{(0,1,0)}(r,\phi ,z)}{r}+\frac{\csc (\phi ) p^{(0,0,2)}(r,\phi ,z)}{r}\right)}{r}$$
特别的, 有以下几种坐标系可以使用:
"Cartesian": 笛卡尔坐标系
"Cylindrical": 柱坐标系
"Polar": 极坐标系
"Spherical": 球坐标系
概率分布和随机数生成器
从一个列表里面, 得到其所有的交换对:
Permutations[{a, b, c}] (* => {{a, b, c}, {a, c, b}, {b, a, c}, {b, c, a}, {c, a, b}, {c, b, a}} *)
如果想要设置子序列的长度为 2:
Permutations[{a, b, c}, {2}] (* => {{a, b}, {a, c}, {b, a}, {b, c}, {c, a}, {c, b}} *)
注: 如果只是想知道排列数:
如果想要知道一个列表中的子序列 (无序关系) 的全部子序列的值:
Subsets[{a, b, c, d, e}, {3}] (* => {{a, b, c}, {a, b, d}, {a, b, e}, {a, c, d}, {a, c, e}, {a, d, e}, {b, c, d}, {b, c, e}, {b, d, e}, {c, d, e}} *)
注: 如果只想知道组合数: Binonmial
Binomial[5, 3] (* => 10 *)
Example 2.3 Distributions
这里把几个通用的函数抽象一下 [1]:
| Distribution | PDF | CDF | Expecation | Variance |
| $\text{BernoulliDistribution}[p]$ | $\begin{cases} 1-p & x=0 \\ p & x=1\end{cases}$ | $\begin{cases} 0 & x<0 \\ 1-p & 0\leq x<1 \\ 1 & \text{True}\end{cases}$ | $p$ | $(1-p) p$ |
| $\text{BinomialDistribution}[n,p]$ | $\begin{cases} (1-p)^{n-x} p^x \binom{n}{x} & 0\leq x\leq n \\ 0 & \text{True}\end{cases}$ | $\begin{cases} I_{1-p}(n-\lfloor x\rfloor ,1+\lfloor x\rfloor ) & 0\leq x<n \\ 1 & x\geq n\end{cases}$ | $n p$ | $n (1-p) p$ |
| $\text{PoissonDistribution}[\mu ]$ | $\begin{cases} \frac{e^{-\mu } \mu ^x}{x!} & x\geq 0 \\ 0 & \text{True}\end{cases}$ | $\begin{cases} Q(1+\lfloor x\rfloor ,\mu ) & x\geq 0 \\ 0 & \text{True}\end{cases}$ | $\mu$ | $\mu$ |
| $\text{NormalDistribution}[\mu ,\sigma ]$ | $\frac{e^{-\frac{(x-\mu )^2}{2 \sigma ^2}}}{\sqrt{2 \pi } \sigma }$ | $\frac{1}{2} \text{erfc}\left(\frac{-x+\mu }{\sqrt{2} \sigma }\right)$ | $\mu$ | $\sigma ^2$ |
| $\text{StudentTDistribution}[\mu ,\sigma ,\nu ]$ | $\frac{\left(\frac{\nu }{\nu +\frac{(x-\mu )^2}{\sigma ^2}}\right)^{\frac{1+\nu }{2}}}{\sqrt{\nu } \sigma B\left(\frac{\nu }{2},\frac{1}{2}\right)}$ | $\begin{cases} \frac{1}{2} I_{\frac{\nu \sigma ^2}{(x-\mu )^2+\nu \sigma ^2}}\left(\frac{\nu }{2},\frac{1}{2}\right) & x\leq \mu \\ \frac{1}{2} \left(1+I_{\frac{(x-\mu )^2}{(x-\mu )^2+\nu \sigma ^2}}\left(\frac{1}{2},\frac{\nu }{2}\right)\right) & \text{True}\end{cases}$ | $\begin{cases} \mu & \nu >1 \\ \text{Indeterminate} & \text{True}\end{cases}$ | $\begin{cases} \frac{\nu \sigma ^2}{-2+\nu } & \nu >2 \\ \text{Indeterminate} & \text{True}\end{cases}$ |
| $\text{UniformDistribution}[\{\min ,\max \}]$ | $\begin{cases} \frac{1}{\max -\min } & \min \leq x\leq \max \\ 0 & \text{True}\end{cases}$ | $\begin{cases} \frac{-\min +x}{\max -\min } & \min \leq x\leq \max \\ 1 & x>\max \end{cases}$ | $\frac{\max +\min }{2}$ | $\frac{1}{12} (\max -\min )^2$ |
| $\text{GeometricDistribution}[p]$ | $\begin{cases} (1-p)^x p & x\geq 0 \\ 0 & \text{True}\end{cases}$ | $\begin{cases} 1-(1-p)^{1+\lfloor x\rfloor } & x\geq 0 \\ 0 & \text{True}\end{cases}$ | $\frac{1-p}{p}$ | $\frac{1-p}{p^2}$ |
Example 2.4 PoissonDistribution Example
平均 2 min 发生一次事件, 请问:
- 0 次发生
- 正好 3 次发生
- 最多 3 次发生
的概率
dist = PoissonDistribution[2];
PDF[dist, 0] (* 0 jobs *)
PDF[dist, 3] (* exactly 3 *)
CDF[dist, 3] (* at most 3 *)
Example 2.5 随机数生成器
RandomInteger[{1, 6}] (* 1-6 之间随机整数 *)
RandomInteger[1] (* 0-1 之间的随机整数 *)
RandomInteger[] (* 同上 *)
RandomReal[] (* 0-1 之间的随机实数 *)
Example 2.6 根据分布生成随机数
RandomVariate[NormalDistribution[0, 1]] (* RandomVariate[dist] *)
Example 2.7 Seed
在 Mathematica 里面, 用 $RandomGeneratorState 来管理当前的随机数状态 [2].
SeedRandom[]; (* 重置随机数种子 *)
SeedRandom[114514] (* 设置种子 *)
RandomInteger[114514] (* 应该是 6049 *)
Footnotes
[1] 其中生成的代码如下:
KernelTableForm[(dist |-> {
dist,
PDF[dist, x],(* 概率分布函数 *)
CDF[dist, x], (* 概率累计函数 *)
Expectation[x, x \[Distributed] dist], (* 值的期望, 可以是复杂的表达式 *)
Variance[dist](* 方差 *)
}) /@ {
BernoulliDistribution[p],
BinomialDistribution[n, p],
PoissonDistribution[\[Mu]]
},
Heads -> {"Distribution", "PDF", "CDF", "Expecation", "Variance"}]
其中 KernelTableForm 现已添加到 KernelTeXForm.wl 中