1. 程式人生 > >在Jupyter Notebook裡面寫Python程式碼和數學公式

在Jupyter Notebook裡面寫Python程式碼和數學公式

轉載:http://blog.163.com/[email protected]/blog/static/167296895201221242646561/

這裡做了更多漢化和語法錯誤修改。

1、數學公式的前後要加上 $ 或 \( 和 \),比如:$f(x) = 3x + 7$ 和 \(f(x) = 3x + 7\) 效果是一樣的;如果用 \[ 和 \],或者使用 $$ 和 $$,則該公式獨佔一行;如果用\begin{equation} 和 \end{equation},則公式除了獨佔一行還會自動被新增序號, 如何公式不想編號則使用 \begin{equation*} 和\end{equation*}

.

2、字元
除了# $ % & ~ _ ^ \ { }普通字元在數學公式中含義一樣,若要在數學環境中表示這些符號# $ % & _ { },需要分別表示為\# \$ \% \& \_ \{ \},即在個字元前加上\


3、上標和下標
用 ^ 來表示上標,用 _ 來表示下標,看一簡單例子:

$$\sum_{i=1}^n a_i=0$$
$$f(x)=x^{x^x}$$

效果:

LaTeX技巧10:LaTeX數學公式輸入初級入門

LaTeX可以通過這符號 $^$ 和 $_$ 來設定上標和下標。使用可以參見:技巧十。

用 ^ 來表示上標,用 _ 來表示下標,如果上標的內容多於一個字元,注意用 { } 把上標括起來,上下標是可以巢狀的,下面是一些簡單例子:

$\sum_{i=1}^n a_i=0$
$f(x)=x^{x^x}$


4、希臘字母
更多請參見
這裡


5、數學函式

例如sin x, 輸入應該為\sin x

6、在公式中插入文字可以通過 \mbox{text} 在公式中新增text,比如:

\documentclass{article}
\usepackage{CJK}
\begin{CJK*}{GBK}{song}
\begin{document}
$$\mbox{對任意的$x>0$}, \mbox{有 }f(x)>0. $$
\end{CJK*}
\end{document}

效果:

LaTeX技巧10:LaTeX數學公式輸入初級入門


7、分數及開方

\frac{numerator}{denominator} \sqrt{expression_r_r_r}

表示開平方,
\sqrt[n]{expression_r_r_r} 表示開 n 次方.


8、省略號(3個點)

\ldots 表示跟文字底線對齊的省略號;\cdots 表示跟文字中線對齊的省略號,

比如:

LaTeX技巧10:LaTeX數學公式輸入初級入門

表示為 $$f(x_1,x_x,\ldots,x_n) = x_1^2 + x_2^2 + \cdots + x_n^2 $$


9、括號和分隔符
() 和 [ ] 和  對應於自己;
{} 對應於 \{ \}
|| 對應於 \|
當要顯示大號的括號或分隔符時,要對應用 \left 和 \right,如:

\[f(x,y,z) = 3y^2 z \left( 3 + \frac{7x+5}{1 + y^2} \right).\]對應於

\left. 和 \right. 只用與匹配,本身是不顯示的,比如,要輸出:

LaTeX技巧10:LaTeX數學公式輸入初級入門

則用 $$\left. \frac{du}{dx} \right|_{x=0}.$$


10、多行的數學公式

LaTeX技巧10:LaTeX數學公式輸入初級入門

可以表示為:

\begin{eqnarray*}
\cos 2\theta & = & \cos^2 \theta - \sin^2 \theta \\
& = & 2 \cos^2 \theta - 1.
\end{eqnarray*}

其中&是對其點,表示在此對齊。
*使latex不自動顯示序號,如果想讓latex自動標上序號,則把*去掉


11、矩陣

LaTeX技巧10:LaTeX數學公式輸入初級入門

表示為:

The \emph{characteristic polynomial} $\chi(\lambda)$ of the
$3 \times 3$~matrix
\[ \left( \begin{array}{ccc}
a & b & c \\
d & e & f \\
g & h & i \end{array} \right)\]
is given by the formula
\[ \chi(\lambda) = \left| \begin{array}{ccc}
\lambda - a & -b & -c \\
-d & \lambda - e & -f \\
-g & -h & \lambda - i \end{array} \right|.\]

c表示向中對齊,l表示向左對齊,r表示向右對齊。


12、導數、極限、求和、積分(Derivatives, Limits, Sums and Integrals)

The expression_r_r_rs

LaTeX技巧10:LaTeX數學公式輸入初級入門

上面的公式可以輸入下面程式碼:

\frac{du}{dt} and \frac{d^2 u}{dx^2}

相應的微分和偏微分 LaTeX技巧10:LaTeX數學公式輸入初級入門 的程式碼是 \partial. 熱力學公式

LaTeX技巧10:LaTeX數學公式輸入初級入門

的程式碼如下:

\[ \frac{\partial u}{\partial t}
= h^2 \left( \frac{\partial^2 u}{\partial x^2}
+ \frac{\partial^2 u}{\partial y^2}
+ \frac{\partial^2 u}{\partial z^2}\right)\]

為了顯示以下公式:

LaTeX技巧10:LaTeX數學公式輸入初級入門

程式碼是 \lim_{x \to +\infty}, \inf_{x > s}and \sup_K respectively. 

以下公式

LaTeX技巧10:LaTeX數學公式輸入初級入門

(in LaTeX) 我們輸入

\[ \lim_{x \to 0} \frac{3x^2 +7x^3}{x^2 +5x^4} = 3.\]

以下公式

LaTeX數學公式 - goldman - 重頭再來

的程式碼如下

\frac{1}{\lim_{u \rightarrow \infty}}, \frac{1}{\lim\limits_{u \rightarrow \infty}} or

\frac{1}{ \displaystyle \lim_{u \rightarrow \infty}} respectively.

To obtain a summation sign such as

LaTeX技巧10:LaTeX數學公式輸入初級入門

we type \sum_{i=1}^{2n}. Thus

LaTeX技巧10:LaTeX數學公式輸入初級入門

is obtained by typing

\[ \sum_{k=1}^n k^2 = \frac{1}{2} n (n+1).\]

We now discuss how to obtain integrals in mathematical documents. A typical integral is the following:

LaTeX技巧10:LaTeX數學公式輸入初級入門

This is typeset using

\[ \int_a^b f(x)\,dx.\]

The integral sign is typeset using the control sequence \int, and the limits of integration (in this case a and b are treated as a subscript and a superscript on the integral sign.
Most integrals occurring in mathematical documents begin with an integral sign and contain one or more instances of d followed by another (Latin or Greek) letter, as in dx, dy and dt. To obtain the correct appearance one should put extra space before the d, using \,. Thus

LaTeX技巧10:LaTeX數學公式輸入初級入門

LaTeX技巧10:LaTeX數學公式輸入初級入門

LaTeX技巧10:LaTeX數學公式輸入初級入門

and

LaTeX技巧10:LaTeX數學公式輸入初級入門

are obtained by typing

\[ \int_0^{+\infty} x^n e^{-x} \,dx = n!.\]

\[ \int \cos \theta \,d\theta = \sin \theta.\]

\[ \int_{x^2 + y^2 \leq R^2} f(x,y)\,dx\,dy
= \int_{\theta=0}^{2\pi} \int_{r=0}^R
f(r\cos\theta,r\sin\theta) r\,dr\,d\theta.\]

and

\[ \int_0^R \frac{2x\,dx}{1+x^2} = \log(1+R^2).\]

respectively.

In some multiple integrals (i.e., integrals containing more than one integral sign) one finds that LaTeX puts too much space between the integral signs. The way to improve the appearance of of the integral is to use the control sequence \! to remove a thin strip of unwanted space. Thus, for example, the multiple integral

LaTeX技巧10:LaTeX數學公式輸入初級入門

is obtained by typing

\[ \int_0^1 \! \int_0^1 x^2 y^2\,dx\,dy.\]

Had we typed

\[ \int_0^1 \int_0^1 x^2 y^2\,dx\,dy.\]

we would have obtained

LaTeX技巧10:LaTeX數學公式輸入初級入門

A particularly noteworthy example comes when we are typesetting a multiple integral such as

LaTeX技巧10:LaTeX數學公式輸入初級入門

Here we use \! three times to obtain suitable spacing between the integral signs. We typeset this integral using

\[ \int \!\!\! \int_D f(x,y)\,dx\,dy.\]

Had we typed

\[ \int \int_D f(x,y)\,dx\,dy.\]

we would have obtained

LaTeX技巧10:LaTeX數學公式輸入初級入門

The following (reasonably complicated) passage exhibits a number of the features which we have been discussing:

LaTeX技巧10:LaTeX數學公式輸入初級入門

One would typeset this in LaTeX by typing In non-relativistic wave mechanics, the wave function
$\psi(\mathbf{r},t)$ of a particle satisfies the
\emph{Schr\"{o}dinger Wave Equation}
\[ i\hbar\frac{\partial \psi}{\partial t}
= \frac{-\hbar^2}{2m} \left(
\frac{\partial^2}{\partial x^2}
+ \frac{\partial^2}{\partial y^2}
+ \frac{\partial^2}{\partial z^2}
\right) \psi + V \psi.\]
It is customary to normalize the wave equation by
demanding that
\[ \int \!\!\! \int \!\!\! \int_{\textbf{R}^3}
\left| \psi(\mathbf{r},0) \right|^2\,dx\,dy\,dz = 1.\]
A simple calculation using the Schr\"{o}dinger wave
equation shows that
\[ \frac{d}{dt} \int \!\!\! \int \!\!\! \int_{\textbf{R}^3}
\left| \psi(\mathbf{r},t) \right|^2\,dx\,dy\,dz = 0,\]
and hence
\[ \int \!\!\! \int \!\!\! \int_{\textbf{R}^3}
\left| \psi(\mathbf{r},t) \right|^2\,dx\,dy\,dz = 1\]
for all times~$t$. If we normalize the wave function in this
way then, for any (measurable) subset~$V$ of $\textbf{R}^3$
and time~$t$,
\[ \int \!\!\! \int \!\!\! \int_V
\left| \psi(\mathbf{r},t) \right|^2\,dx\,dy\,dz\]
represents the probability that the particle is to be found
within the region~$V$ at time~$t$.