Mathematica入门
解方程
1
| Solve[x^2 - 3 x + 2 == 0, x]
|
输出
在整数范围内求方程的解
1
| Solve[x^2 - 3 x + 2 == 0 && 0 <= x <= 2, x, Integers]
|
输出
解方程组
1
| Solve[{x + y == 3, x - y == 1}, {x, y}]
|
输出
解不等式
1
| Reduce[x^2 - 3 x + 2 > 0, x]
|
输出
微分
输出
积分
输出
求极限
1
| Limit[1/x, x -> 0, Direction -> "FromAbove"]
|
输出
x 从右侧趋近 0 时,极限为 Infinity。双侧实数极限不存在。可以用 Direction 指定单侧趋近方向。
求级数
1
| Sum[1/n^2, {n, 1, Infinity}]
|
输出
创建矩阵
求矩阵乘积
输出
求逆矩阵
输出
输出
1
| {{(5 + Sqrt[33])/2, (5 - Sqrt[33])/2}, {{2, (3 + Sqrt[33])/2}, {2, (3 - Sqrt[33])/2}}}
|
此矩阵的特征值为 (5±√33)/2。特征向量的顺序或倍数可能不同,但每组值与向量都满足 m.v=λv。
求向量的点积
输出
求向量的叉积
1
| Cross[{1, 2, 3}, {4, 5, 6}]
|
输出
求向量的模(大小)
输出
求向量的夹角
1
| ArcCos[{1, 2} . {3, 4}/(Norm[{1, 2}] Norm[{3, 4}])]
|
输出
求向量的投影
1
| Projection[{1, 2}, {3, 4}]
|
输出
将 {1,2} 正交投影到 {3,4} 时,系数为内积之比 11/25,结果是 {33/25,44/25}。
求向量的旋转
1
| RotationMatrix[π/2].{1, 0}
|
输出
求向量的平移
1
| TranslationTransform[{1, 2}][{3, 4}]
|
输出
求向量的缩放
1
| ScalingTransform[{2, 3}][{1, 1}]
|
输出
求向量的反转(反射)
1
| ReflectionTransform[{0, 1}][{1, 1}]
|
输出
ReflectionTransform 的第一个参数是镜面的法向量。法向量 {0,1} 表示 x 轴,因此 {1,1} 映射为 {1,-1}。
下方随机数输出仅作示例,每次计算都会变化。RandomInteger[] 返回 0 或 1。SeedRandom 可在相同生成器和版本下重现随机序列。
生成随机数
输出
生成随机数(整数)
输出
生成随机数(指定范围)
输出
生成随机数(整数、指定范围)
输出
生成随机数(指定分布)
1
| RandomVariate[NormalDistribution[0, 1]]
|
输出
生成随机数(指定分布、指定数量)
1
| RandomVariate[NormalDistribution[0, 1], 10]
|
输出
1
| {0.123456, 0.234567, 0.345678, 0.456789, 0.567890, 0.678901, 0.789012, 0.890123, 0.901234, 0.012345}
|
生成随机数(指定分布、指定数量、指定种子)
1
2
| SeedRandom[12345]
RandomVariate[NormalDistribution[0, 1], 10]
|
输出
1
| (* Output intentionally omitted: exact sequence depends on the Wolfram Language version. *)
|
对数组元素应用函数
1
2
3
| Map[Sqrt, {1, 4, 9}]
Sqrt /@ {1, 4, 9}
Map[#^(1/2)&, {1, 4, 9}]
|
输出
1
2
| f = Function[x, x^2]
f[3]
|
输出
复合函数
1
2
3
4
| f = Function[x, x^2]
g = Function[x, x + 1]
h = Function[x, f[g[x]]]
h[3]
|
输出
引用上一次的计算结果
输出
纯函数
输出
数组提取
1
2
| Select[{1, 2, 3, 4, 5}, EvenQ]
Select[{1, 2, 3, 4, 5}, Mod[#,2]==0&]
|
输出
函数官方文档
Solve · Reduce · D · Integrate · Limit · Sum · Inverse · Eigensystem · Projection · RotationMatrix · ReflectionTransform · RandomReal · RandomInteger · RandomVariate