在博客中使用LaTeX插入數學公式


在學習機器學習中會接觸到大量的數學公式,所以在寫博客是會非常的麻煩。用公式編輯器一個一個寫會非常的麻煩,這時候我們可以使用LaTeX來插入公式。

寫這篇博文的目的在於,大家如果要編輯一些簡單的公式,就不必自己寫,直接copy過去修改下就能用了。所以下面僅列出些常用的grammar。隨着、機器學習的深入會添加更多的相關公式。

LaTeX公式基礎

這里的基礎嫌煩的話可以先不看,直接看雜例,有不理解的地方在回來看這里的內容。此處知識摘取了一些簡單的語法,如果需要完整的LaTeX書寫數學公式的文檔,見參考文獻。

排版方式

行級元素(inline),行級元素使用$...$,兩個$表示公式的首尾。

塊級元素(displayed),塊級元素使用$$...$$。塊級元素默認是居中顯示的。

常用西文符號

\alpha, \beta, …, \omega代表α,β,…ω. 大寫字母,使用 \Gamma, \Delta, …, \Omega代表Γ,Δ,…,Ω.

上標與下標

使用 ^和 _ 表示上標和下標. 例如, x_i^2:\(x_i^2\)\log_2 x: \(\log_2 x\)

使用{}來消除二義性——優先級問題。例如10^10:\(10^10\),顯然是錯誤的,要顯示\(10^{10}\),正確的語法應該是10^{10}。同樣的,還有個例子,x_i^2:\(x_i^2\)x_{i^2}:\(x_{i^2}\)的區別。

括號

小括號和中括號直接使用,大括號由於用來分組,所以需要轉義。\{1+2\}:\(\{1+2\}\)

運算

  • 分數:\frac{}{}。例如,\frac{1+1}{2}+1: \(\frac{1+1}{2}+1\)
  • 求和:\sum_1^n:\(\sum_1^n\)
  • 積分:\int_1^n:\(\int_1^n\)
  • 極限:lim_{x \to \infty:\(\lim_{x \to \infty}\)
  • 矩陣:$$\begin{matrix}…\end{matrix}$$,使用&分隔同行元素,\\換行。例如:
$$
        \begin{matrix}
        1 & x & x^2 \\
        1 & y & y^2 \\
        1 & z & z^2 \\
        \end{matrix}
$$

得到的公式為:

\[ \begin{matrix} 1 & x & x^2 \\ 1 & y & y^2 \\ 1 & z & z^2 \\ \end{matrix} \]

雜例

  • $$h(\theta)=\sum_{j=0}^n \theta_jx_j$$

\[h(\theta)=\sum_{j=0}^n \theta_jx_j(線性模型) \]

  • $$J(\theta)=\frac1{2m}\sum_{i=0}(y^i-h_\theta(x^i))^2$$

\[J(\theta)=\frac1{2m}\sum_{i=0}^m(y^i-h_\theta(x^i))^2(均方誤差\;or\;cost function) \]

  • $$\frac{\partialJ(\theta)}{\partial\theta_j}=-\frac1m\sum_{i=0}^m(y^i-h_\theta(x^i))x^i_j $$

\[\frac{\partial J(\theta)}{\partial\theta_j }=-\frac1m\sum_{i=0}^m(y^i-h_\theta(x^i))x^i_j (批量梯度下降的梯度算法) \]

$$
f(n) =
    \begin{cases}
    n/2,  & \text{if $n$ is even} \\
    3n+1, & \text{if $n$ is odd}
    \end{cases}
$$

\[f(n) = \begin{cases} n/2, & \text{if $n$ is even} \\ 3n+1, & \text{if $n$ is odd} \end{cases} \]

$$
\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. 
$$

\[\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. \]

$$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)
    =\left(
         \begin{matrix}
                x_1^T \\
                x_2^T \\
                \vdots\\
                x_m^T \\
            \end{matrix}
    \right)
$$

\[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) =\left( \begin{matrix} x_1^T \\ x_2^T \\ \vdots\\ x_m^T \\ \end{matrix} \right) \]

$$
\begin{align}
\frac{\partial J(\theta)}{\partial\theta_j}
& = -\frac1m\sum_{i=0}^m(y^i-h_\theta(x^i)) \frac{\partial}{\partial\theta_j}(y^i-h_\theta(x^i)) \\
& = -\frac1m\sum_{i=0}^m(y^i-h_\theta(x^i)) \frac{\partial}{\partial\theta_j}(\sum_{j=0}^n\theta_jx_j^i-y^i) \\
& = -\frac1m\sum_{i=0}^m(y^i-h_\theta(x^i))x^i_j
\end{align}
$$

\[\begin{align} \frac{\partial J(\theta)}{\partial\theta_j} & = -\frac1m\sum_{i=0}^m(y^i-h_\theta(x^i)) \frac{\partial}{\partial\theta_j}(y^i-h_\theta(x^i)) \\ & = -\frac1m\sum_{i=0}^m(y^i-h_\theta(x^i)) \frac{\partial}{\partial\theta_j}(\sum_{j=0}^n\theta_jx_j^i-y^i) \\ & = -\frac1m\sum_{i=0}^m(y^i-h_\theta(x^i))x^i_j \end{align} \]

總結

本文主要寫了些用LaTeX來寫數學公式的方法以及幾個例子。雜例的前3個可以看到是用梯度法解決線性模型的幾個公式,后面的幾個是隨意摘取的,盡可能包含大部分LaTeX的用法。雜例會在我學習機器學習的過程中不斷添加,希望可以給大家帶來方便吧。下面的參考文獻包含了中英文,幾乎包含了所有LaTeX書寫數學公式的語法,有需要的可以去看看。

參考文獻


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM