There are two types of blocks in a Jupyter notebook: code and markdown. Code is for entering Python code while markdown is for markdown and Latex.
To execute a block, press shift+enter key.
Title and sections are made by
# Main title
## Section
### Subsection
#### Subsubsection
gives
Itemize
* First item
* Second item
* Third item
* sub item
* sub item
gives
Enumerate
1. First item
1. Second item
1. Third item
1. sub item
1. sub item
gives
Verbatim text can be put inline like this: Python
and as a code block like this (double click to see the code)
x = 0
while abs(f(x)) < eps:
x = x - f(x)/df(x)
Quoted text: begin with a greater than symbol
> One reason why mathematics enjoys special esteem, above all other sciences, is that its laws are absolutely certain and indisputable, while those of all other sciences are to some extent debatable and in constant danger of being overthrown by newly discovered facts.
One reason why mathematics enjoys special esteem, above all other sciences, is that its laws are absolutely certain and indisputable, while those of all other sciences are to some extent debatable and in constant danger of being overthrown by newly discovered facts.
In spite of this, the investigator in another department of science would not need to envy the mathematician if the laws of mathematics referred to objects of our mere imagination, and not to objects of reality. For it cannot occasion surprise that different persons should arrive at the same logical conclusions when they have already agreed upon the fundamental laws (axioms), as well as the methods by which other laws are to be deduced therefrom. But there is another reason for the high repute of mathematics, in that it is mathematics which affords the exact natural sciences a certain measure of security, to which without mathematics they could not attain.
At this point an enigma presents itself which in all ages has agitated inquiring minds. How can it be that mathematics, being after all a product of human thought which is independent of experience, is so admirably appropriate to the objects of reality? Is human reason, then, without experience, merely by taking thought, able to fathom the properties of real things.
In my opinion the answer to this question is, briefly, this:- As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality.
(Albert Einstein, "Geometry and Experience", Prussian Academy of Science, Berlin, 27 January 1921)
Jupyter supports only a small subset of Latex.
Inline math is enclosed in dollars: $y = f(x)$
gives $y = f(x)$
Equations are enclosed in double dollars
$$
y = f(x)
$$
$$
y = f(x)
$$
Math functions: use \sin
, \cos
, etc.
$$
\sin(x), \qquad \cos(x), \qquad \log(x), \qquad \exp(x)
$$
$$
\sin(x), \qquad \cos(x), \qquad \log(x), \qquad \exp(x)
$$
We can put space in math using \quad
and \qquad
.
Greek symbols are obtained naturally by adding a forward slash
$$
\alpha, \qquad \beta, \qquad \gamma, \qquad \delta, \qquad \epsilon, \qquad \varepsilon
$$
$$
\alpha, \qquad \beta, \qquad \gamma, \qquad \delta, \qquad \epsilon, \qquad \varepsilon
$$
Subscripts and superscripts
$$
a_m, \qquad a_{mn}, \qquad x^n, \qquad y^{n+1}, \qquad a^n_m
$$
$$
a_m, \qquad a_{mn}, \qquad x^n, \qquad y^{n+1}, \qquad a^n_m
$$
Fractions using \frac
and \tfrac
$$
\frac{x}{y}, \qquad \tfrac{x}{y}
$$
$$
\frac{x}{y}, \qquad \tfrac{x}{y}
$$
Automatically adjusting brackets
$$
f(x) = \left( 1 + \frac{e^x}{1+x} \right) \left[ 1 - \frac{e^x}{1-x} \right]
$$
$$
f(x) = \left( 1 + \frac{e^x}{1+x} \right) \left[ 1 - \frac{e^x}{1-x} \right]
$$
ODE
$$
\frac{d y}{d t} = f(t,y)
$$
$$
\frac{d y}{d t} = f(t,y)
$$
PDE
$$
\frac{\partial u}{\partial t} + \frac{\partial f}{\partial x} = 0
$$
$$
\frac{\partial u}{\partial t} + \frac{\partial f}{\partial x} = 0
$$
To reduce typing, we will define some aliases
$$
\newcommand{\re}{\mathbb{R}}
\newcommand{\mathd}{\textrm{d}}
\newcommand{\od}[2]{\frac{\mathd #1}{\mathd #2}}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
$$
$$
\newcommand{\re}{\mathbb{R}}
\newcommand{\mathd}{\textrm{d}}
\newcommand{\od}[2]{\frac{\mathd #1}{\mathd #2}}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
$$We write the ODE and PDE again
$$
\od{y}{t} = f(t,y), \qquad \pd{u}{t} + \pd{f}{x} = 0
$$
$$
\od{y}{t} = f(t,y), \qquad \pd{u}{t} + \pd{f}{x} = 0
$$
Heaviside function
$$
H(x) = \begin{cases}
0, & x < 0 \\
1, & x > 1
\end{cases}
$$
$$
H(x) = \begin{cases}
0, & x < 0 \\
1, & x > 0
\end{cases}
$$
Multi-line equation: \eqnarray
\begin{eqnarray}
y &=& (\sin(x) + \cos(x))^2 \\
&=& \sin^2(x) + \cos^2(x) + 2 \sin(x) \cos(x) \\
&=& 1 + \sin(2x)
\end{eqnarray}
\begin{eqnarray}
y &=& (\sin(x) + \cos(x))^2 \\
&=& \sin^2(x) + \cos^2(x) + 2 \sin(x) \cos(x) \\
&=& 1 + \sin(2x)
\end{eqnarray}
Multi-line equation: \align
\begin{align}
y &= (\sin(x) + \cos(x))^2 \\
&= \sin^2(x) + \cos^2(x) + 2 \sin(x) \cos(x) \\
&= 1 + \sin(2x)
\end{align}
\begin{align}
y &= (\sin(x) + \cos(x))^2 \\
&= \sin^2(x) + \cos^2(x) + 2 \sin(x) \cos(x) \\
&= 1 + \sin(2x)
\end{align}which allows multiple alignments
\begin{align}
y &= (\sin(x) + \cos(x))^2 & \\
&= \sin^2(x) + \cos^2(x) + 2 \sin(x) \cos(x) & \\
&= 1 + \sin(2x), & \textrm{since } \sin^2(x) + \cos^2(x) = 1
\end{align}
\begin{align}
y &= (\sin(x) + \cos(x))^2 & \\
&= \sin^2(x) + \cos^2(x) + 2 \sin(x) \cos(x) & \\
&= 1 + \sin(2x), & \textrm{since } \sin^2(x) + \cos^2(x) = 1
\end{align}
Use \gather
to stack many equations without alignment
\begin{gather}
y = x_1 + x_2 + x_3 + x_4 \\
z = x_1 + x_2
\end{gather}
\begin{gather}
y = x_1 + x_2 + x_3 + x_4 \\
z = x_1 + x_2
\end{gather}
Use \underbrace
to add extra information
\begin{align}
y &= (\sin(x) + \cos(x))^2 \\
&= \underbrace{\sin^2(x) + \cos^2(x)}_{=1} + 2 \sin(x) \cos(x) \\
&= 1 + \sin(2x)
\end{align}
\begin{align}
y &= (\sin(x) + \cos(x))^2 \\
&= \underbrace{\sin^2(x) + \cos^2(x)}_{=1} + 2 \sin(x) \cos(x) \\
&= 1 + \sin(2x)
\end{align}
Equation numbering is not well supported in markdown, but we can put numbers by hard-coding them
$$
u_t + u \cdot \nabla u + \nabla p = \epsilon \Delta u \tag{1}
$$
$$
u_t + u \cdot \nabla u + \nabla p = \epsilon \Delta u \tag{1}
$$
Linear Algebra:
> Given $b \in R^m$ and $A \in R^{m \times m}$, find $x \in R^m$ such that
>
> $$
A x = b
$$
Given $b \in R^m$ and $A \in R^{m \times m}$, find $x \in R^m$ such that
$$
A x = b $$
The set of reals and complex are usually denoted as: $\mathbb{R}$ and $\mathbb{C}$. So if we are very strict, we should write:
> Given $b \in \mathbb{R}^m$ and $A \in \mathbb{R}^{m \times m}$, find $x \in \mathbb{R}^m$ such that
>
> $$
A x = b
$$
Given $b \in \mathbb{R}^m$ and $A \in \mathbb{R}^{m \times m}$, find $x \in \mathbb{R}^m$ such that
$$
A x = b $$
Matrix
$$
A = \begin{bmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{bmatrix}
$$
$$
A = \begin{bmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{bmatrix}
$$
Condition number of matrix $A \in \re^{m \times m}$ with respect to some norm
$$
\kappa = \| A \| \| A^{-1} \|
$$
$$
\kappa = \| A \| \| A^{-1} \|
$$
Always
Possibly break the code down into some functions, so that you avoid repeating the same code in multiple places.
Put checks for array sizes, etc.
Be careful while dividing (denominator should not be zero), taking square roots (should be non-negative), etc.
Test the code on some inputs to see it is working correctly. Also test individual functions that they are correct.
If code crashes with some error, read the error message and fix the mistake. Cut and paste the error in a search engine to find solution.
Do not hard code values, define some variables and use them. This way, if you change the value, you only need to change it in one place.
Write comments in code to explain what you are doing.
Indent code for better readability. This is essential for Python and the notebook editor automatically does this.
Put spaces in formulae for better readability.
f = x+y+z*sin(a)*cos(b)
f = x + y + z * sin(a) * cos(b) <== Better
Use brackets in formulae where needed. E.g., $f = \frac{a}{b c}$
$$ \textrm{f = a / b * c} \qquad \textrm{wrong, this gives} \qquad f = \left(\frac{a}{b}\right) c $$
correct code is
$$ \textrm{f = a / (b * c)} $$
Another example
$$ \textrm{f = x + y + z * sin(a) * cos(b)} \qquad \textrm{gives} \qquad f = x + y + (z \sin(a) \cos(b)) $$
$$ \textrm{f = (x + y + z) * sin(a) * cos(b)} \qquad \textrm{gives} \qquad f = (x + y + z) \sin(a) \cos(b) $$
Put a decimal when assigning float values; note that Python infers type based on the value you assign.
Avoid writing long lines; continue long lines to new line or break into multiple statements of smaller size.
Print some values to see computations are correct, and to monitor progress of code.
In figures,
plt.grid(True)
.