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

在學習機器學習中會接觸到大量的數學公式,因此在寫博客是會很是的麻煩。用公式編輯器一個一個寫會很是的麻煩,這時候咱們可使用LaTeX來插入公式。算法

寫這篇博文的目的在於,你們若是要編輯一些簡單的公式,就沒必要本身寫,直接copy過去修改下就能用了。因此下面僅列出些經常使用的grammar。隨着、機器學習的深刻會添加更多的相關公式。express

LaTeX公式基礎

這裏的基礎嫌煩的話能夠先不看,直接看雜例,有不理解的地方在回來看這裏的內容。此處知識摘取了一些簡單的語法,若是須要完整的LaTeX書寫數學公式的文檔,見參考文獻。機器學習

排版方式

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

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

經常使用西文符號

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

上標與下標

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

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

括號

小括號和中括號直接使用,大括號因爲用來分組,因此須要轉義。\{1+2\}:\(\{1+2\}\)code

運算

  • 分數:\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書寫數學公式的語法,有須要的能夠去看看。

參考文獻

相關文章
相關標籤/搜索