写机器学习相关的博客时,插入一些复杂的数学公式是不可避免的事,总是截图并不是一个完美的解决方法,在此总结一下如何使用LaTeX编辑数学公式,包括常用的符号,以及机器学习经常用到的公式。
什么是LaTeX
LaTeX(LATEX,音译“拉泰赫”)是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用者没有排版和程序设计的知识也可以充分发挥由TeX所提供的强大功能,能在几天,甚至几小时内生成很多具有书籍质量的印刷品。对于生成复杂表格和数学公式,这一点表现得尤为突出。因此它非常适用于生成高印刷质量的科技和数学类文档。这个系统同样适用于生成从简单的信件到完整书籍的所有其他种类的文档。
LaTeX编辑数学公式基本语法元素
排版方式
- 行间公式(inline):用
$...$
将公式括起来。
例如:输入$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$
显示: x = − b ± b 2 − 4 a c 2 a x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} x=2a−b±b2−4ac - 块间公式(displayed),用
$$...$$
将公式括起来是无编号的形式。块间元素默认是居中显示的。
例如:输入$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$
显示: x = − b ± b 2 − 4 a c 2 a x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} x=2a−b±b2−4ac
希腊字母编辑表
常用符号
-
上标(superscripts)和下标(subscripts)
使用^
和_
表示上标和下标. 例如,$x_i^2$
: x i 2 x_i^2 xi2 ,$\log_2 x$
: log 2 x \log_2 x log2x。 -
分组(groups)
上标和下标作用于分组,使用花括号(curly braces)。
$10^10$
: 1 0 1 0 10^10 1010(错误,非预期)(应在第2个10前后加花括号{})。
$x^{y^z}$
: x y z x^{y^z} xyz,${x^y}^z$
: x y z {x^y}^z xyz,注意后面的z
比较低。 -
括号
小括号和中括号直接使用,大括号由于用来分组,所以需要转义。
$\{1+2\}$
: { 1 + 2 } \{1+2\} {1+2} -
分数
$\frac{}{}$
,例如:$\frac{1+1}{2}+1$
: 1 + 1 2 + 1 \frac{1+1}{2}+1 21+1+1 -
求和
$\sum_1^n$
: ∑ 1 n \sum_1^n ∑1n -
聚合和积分
$\prod$
: ∏ \prod ∏
$\sum_{i=0}i^2$
: ∑ i = 0 i 2 \sum_{i=0}i^2 ∑i=0i2
$\int$
: ∫ \int ∫,$\iint$
: ∬ \iint ∬ ,$\iiint$
: ∭ \iiint ∭,$\bigcup$
: ⋃ \bigcup ⋃,$\bigcap$
: ⋂ \bigcap ⋂ -
极限
$lim_{x \to \infty}$
: l i m x → ∞ lim_{x \to \infty} limx→∞ -
开方
$\sqrt {x^3}$
: x 3 \sqrt {x^3} x3
$\sqrt[4]{\frac xy}$
: x y 4 \sqrt[4]{\frac xy} 4yx
$\left(\frac xy \right)^{1/4}$
: ( x y ) 1 / 4 \left(\frac xy \right)^{1/4} (yx)1/4 -
运算符
小的运算符(operator)例如 + - * /等可以直接输入,其他特殊的有$\pm$ $\times$ $\div$ $\cdot$ $\cap$ $\cup$ $\geq$ $\leq$ $\neq$ $\approx$ $\equiv$
:
± \pm ± × \times × ÷ \div ÷ ⋅ \cdot ⋅ ∩ \cap ∩ ∪ \cup ∪ ≥ \geq ≥ ≤ \leq ≤ ≠ \neq = ≈ \approx ≈ ≡ \equiv ≡转义字符(escape character)
\
,反斜线\backslash
: ∖ ∖ ∖,
$\lt$ $\gt$ $\le$ $\leq$ $\leqq$ $\ge$ $\geq$ $\geqq$ $\neq$
: < \lt < > \gt > ≤ \le ≤ ≤ \leq ≤ ≦ \leqq ≦ ≥ \ge ≥ ≥ \geq ≥ ≧ \geqq ≧ ≠ \neq =
$\land$ $\lor$ $\lnot$ $\forall$ $\exists$ $\top$ $\bot$ $\vdash$ $\vDash$
: ∧ \land ∧ ∨ \lor ∨ ¬ \lnot ¬ ∀ \forall ∀ ∃ \exists ∃ ⊤ \top ⊤ ⊥ \bot ⊥ ⊢ \vdash ⊢ ⊨ \vDash ⊨ -
矩阵
$$\begin{matrix}…\end{matrix}$$
,使用&
分隔同行元素,\\
换行。例如:$$ \left( \begin{matrix} 1 & x & x^2 \\ 1 & y & y^2 \\ 1 & z & z^2 \\ \end{matrix} \right) $$
( 1 x x 2 1 y y 2 1 z z 2 ) \left( \begin{matrix} 1 & x & x^2 \\ 1 & y & y^2 \\ 1 & z & z^2 \\ \end{matrix} \right) 111xyzx2y2z2
- 行列式
$$
X=\left|
\begin{matrix}
x_{11} & x_{12} & \cdots & x_{1d}\\
x_{21} & x_{22} & \cdots & x_{2d}\\
\vdots & \vdots & \ddots & \vdots\\
x_{m1} & x_{m2} & \cdots & x_{md}\\
\end{matrix}
\right|
$$
X = ∣ x 11 x 12 ⋯ x 1 d x 21 x 22 ⋯ x 2 d ⋮ ⋮ ⋱ ⋮ x m 1 x m 2 ⋯ x m d ∣ X=\left| \begin{matrix} x_{11} & x_{12} & \cdots & x_{1d}\\ x_{21} & x_{22} & \cdots & x_{2d}\\ \vdots & \vdots & \ddots & \vdots\\ x_{m1} & x_{m2} & \cdots & x_{md}\\ \end{matrix} \right| X= x11x21⋮xm1x12x22⋮xm2⋯⋯⋱⋯x1dx2d⋮xmd
- 分段函数
$$
f(n) =
\begin{cases}
n/2, & \text{if $n$ is even} \\
3n+1, & \text{if $n$ is odd}
\end{cases}
$$
f ( n ) = { n / 2 , if n is even 3 n + 1 , if n is odd f(n) = \begin{cases} n/2, & \text{if $n$ is even} \\ 3n+1, & \text{if $n$ is odd} \end{cases} f(n)={n/2,3n+1,if n is evenif n is odd
- 方程组
$$
\left\{
\begin{array}{c}
a_1x+b_1y+c_1z=d_1 \\
a_2x+b_2y+c_2z=d_2 \\
a_3x+b_3y+c_3z=d_3
\end{array}
\right.
$$
{ a 1 x + b 1 y + c 1 z = d 1 a 2 x + b 2 y + c 2 z = d 2 a 3 x + b 3 y + c 3 z = d 3 \left\{ \begin{array}{c} a_1x+b_1y+c_1z=d_1 \\ a_2x+b_2y+c_2z=d_2 \\ a_3x+b_3y+c_3z=d_3 \end{array} \right. ⎩ ⎨ ⎧a1x+b1y+c1z=d1a2x+b2y+c2z=d2a3x+b3y+c3z=d3
机器学习常用到的公式
-
线性模型
$$h(\theta)=\sum_{j=0}^n \theta_jx_j$$
: h ( θ ) = ∑ j = 0 n θ j x j h(\theta)=\sum_{j=0}^n \theta_jx_j h(θ)=j=0∑nθjxj -
均方误差
$$J(\theta)=\frac1{2m}\sum_{i=0}^m(h_\theta(x^i)-y^i)^2$$
: J ( θ ) = 1 2 m ∑ i = 0 m ( h θ ( x i ) − y i ) 2 J(\theta)=\frac1{2m}\sum_{i=0}^m(h_\theta(x^i)-y^i)^2 J(θ)=2m1i=0∑m(hθ(xi)−yi)2 -
线性回归梯度下降
∂ J ( θ ) ∂ θ j = 1 m ∑ i = 0 m ( h θ ( x i ) − y i ) x j i \frac{\partial J(\theta)}{\partial\theta_j}=\frac1m\sum_{i=0}^m(h_\theta(x^i)-y^i)x^i_j ∂θj∂J(θ)=m1i=0∑m(hθ(xi)−yi)xji
以后自己用到再回来更新吧。。。
方便的网站
推荐两个网站,可以很方便我们直接得到公式,毕竟LaTeX的语法很复杂。
-
在线latex编辑器
可以在线编辑公式,得出相应的LaTeX的字符串,也可以用来验证自己写的LaTeX是否正确。
链接:在线latex编辑器 -
在线识别手写公式
这个就厉害了,我们可以在上面直接手写公式,只要一般写的标准点字体,都能识别出来,生成相应的图片,也可以生成latex格式的公式,我们把这个部分拷贝出来,然后插入到任何我们想使用这个公式的地方就可以了。
链接:在线手写公式识别 -
在线图片orc识别公式
https://www.latexlive.com/home
https://tool.lu/latexocr/