# Maths Overview

Trivially, notebooks provide us with a simple editing environment for combining markdown text, simple inline LaTeX and LateX blocks, and code cells prefixed with the `%%latex` block cell magic.

This also us to notebooks a medium for creating content blends narrative text with mathematical notation.

```{note}
Within a notebook user interface, native support for LaTeX inline in markdown cells is limited to that subset of LaTeX that can be parsed by the MathJax parser.

LaTeX parsing magics and code output transclusion can be used to provide access to a full featured LaTeX parser.
```

In addition, code cells allow us to perform mathematical computations and generate graphical outputs.

In a complete one piece generative document flow publishing system where we guarantee the correctness of calculations and formal arguments, as wel as the correctness of output graphics in relation to the body of the content, we ideally need to find a way to relate the (symobolic) mathematical content to the code that is executed.

Using a symbolic maths package such as `sympy`, we can create symbolic computational expressions that can be used to calculate (compute) expressions at a symbolic level as well as rendering those expressions in mathematical form using LaTeX (Mathjax). For rendering integrated one piece content in Jupyter book, the Python `myst_nb.glue()` provides a means for inline code outputs, but this requires a Python kernel. For bookdown workflows, outputs from all supported languages can be inlined [ *TO DO — CHECK* ].

If tight integration with the text is not required, or if markdown output can be generated from code, computation using a wide range of other languages can is enabled by installing the appropriate Jupyter kernel ([curated list of Jupyter kernels](https://github.com/jupyter/jupyter/wiki/Jupyter-kernels)). For example, several kernels are available that are particularly suited to a range of mathematics related activities such as statistical computing, symbolic maths and numerical computation. For example:

- [`R`](https://irkernel.github.io/) statistical computing and graphics;
- [`Stata`](https://github.com/TiesdeKok/ipystata) statistical computing;
- [`SageMath`](https://doc.sagemath.org/html/en/installation/launching.html#setting-up-sagemath-as-a-jupyter-kernel-in-an-existing-jupyter-notebook-or-jupyterlab-installation) computer algebra system;
- [`Maxima`](https://github.com/robert-dodier/maxima-jupyter) computer algebra system;
- [`Octave`](https://github.com/Calysto/octave_kernel) numerical computation;
- [`SciLab`](https://github.com/calysto/scilab_kernel) numerical computation;
- [*Matlab*](https://github.com/calysto/matlab_kernel) mathematical computing;
- [*Wolfram Language*](https://github.com/WolframResearch/WolframLanguageForJupyter) mathematical computing;
- [`Gnuplot`](https://github.com/has2k1/gnuplot_kernel) charts.


```{note}
We can also write markdown in a code cell by converting to the code cell to a *de facto* markdown cell using the `%%markdown` block magic.
```

## Rendering equations Using MathJax
Equations can be rendered as a block using MathJax in a markdown cell.

$$
\begin{align}
\sqrt{3x-1}+(1+x^2)
\end{align}
$$

See this third party [Typesetting Equations](https://nbviewer.jupyter.org/github/ipython/ipython/blob/4.0.x/examples/Notebook/Typesetting%20Equations.ipynb) demonstration notebook for further examples.

MathJax content can also be rendered inline. For example, we can include the expression  $\sqrt{3x-1}+(1+x^2)$ embedded *within* a line of text.

## Rendering equations from `sympy`

Guaranteeing the truth of a derived mathematical expression is often difficult if multiple steps of working are required and is further complicated particulalry if the expression is a complicated one.

Using a symbolic maths package such as `sympy` allows derived expressions to be generated automatically and then embedded into book output.

*The following example is taken from ["Technical writing: using math", Nicolás Guarín-Zapata](https://nicoguaro.github.io/posts/tech_writing_math/).*

Given the expression:


```python
from sympy import symbols, exp, sin

x = symbols("x")

f = exp(-x**2)*sin(3*x)
f
```

we can find its second derivative as:


```python
from sympy import diff

fxx = diff(f, x, 2)
fxx
```

Since the second derivative is calculated, and the equation is then rendered to a *LaTeX* form automatically, we know that the expression is correct (although it may not be in the form we require).

### Rendering Matrices

If we have a `numpy` array, we can render it as a LaTeX styled matrix using a Python package such as `numpyarray_to_latex`.

For example, here's a random 4 x 5 array with round brackets (the default, although other bracket styles are customisable):


```python
#https://github.com/benmaier/numpyarray_to_latex
#%pip install --upgrade numpyarray_to_latex
import numpy as np
from numpyarray_to_latex.jupyter import to_jup

array = np.random.randn(4,5)

to_jup(array)
```


$\displaystyle \left(
\begin{array}{}
    0.98 &  -0.76 &  -0.82 &    1.12 &    0.12\\
  -1.26 &  -1.71 &  -1.67 &    0.49 &    1.22\\
  -1.04 &    0.17 &    0.18 &  -0.59 &    1.21\\
    0.05 &    0.27 &    1.85 &  -1.15 &    0.17
\end{array}
\right)$


We can access the raw LaTeX if required:


```python
from numpyarray_to_latex import to_ltx

latex_txt = to_ltx(array)
print(latex_txt)
```

    \left(
    \begin{array}{}
      0.9752 & -0.7629 & -0.8192 &  1.1152 &  0.1160\\
     -1.2629 & -1.7068 & -1.6683 &  0.4853 &  1.2185\\
     -1.0446 &  0.1705 &  0.1847 & -0.5852 &  1.2059\\
      0.0474 &  0.2737 &  1.8477 & -1.1480 &  0.1711
    \end{array}
    \right)


Matrices can also be rendered via `sympy` (using square brackets), as can the results of matrix calculations. For example, let's cast the above array as a `sympy.Matrix` and add it to itself:


```python
from sympy import Matrix

Matrix(array) + Matrix(array)
```




$\displaystyle \left[\begin{matrix}1.95031844930636 & -1.52573498570827 & -1.63848094559338 & 2.23036030020092 & 0.232069228555355\\-2.52584160621578 & -3.41356387618327 & -3.33667226592755 & 0.970506979299136 & 2.43708864310567\\-2.08929947857012 & 0.340943496173498 & 0.369321669583589 & -1.17043961672008 & 2.41184728145124\\0.0947048905289055 & 0.547414340249613 & 3.69542839395078 & -2.29598669946454 & 0.342193659945731\end{matrix}\right]$



## Embedding LaTex / TikZ Graphical Outputs

We can use the [`ipython_magic_tikz`](https://github.com/innovationOUtside/ipython_magic_tikz) magic to provide access to a TikZ/LaTeX parser to allow us to generate diagrams from [TikZ](https://www.overleaf.com/learn/latex/TikZ_package)) scripts.


```python
#%pip install git+https://github.com/innovationOUtside/ipython_magic_tikz.git
%load_ext tikz_magic
```


```python
%%tikz
\usetikzlibrary{shapes.geometric, calc}
\def\numsides{7} % regular polygon sides
\node (a) 
[draw,  blue!0!black,rotate=90,minimum size=3cm,regular polygon, regular polygon sides=\numsides] at (0, 0) {}; 

\foreach \x in {1,2,...,\numsides}
  \fill (a.corner \x) circle[radius=.5pt];

\foreach \x in {1,2,...,\numsides}{
  \draw [red,dashed, shorten <=-0.5cm,shorten >=-0.5cm](a.center) -- (a.side \x);
  \draw [red,dashed, shorten <=-0.5cm,shorten >=-0.5cm](a.center) -- (a.corner \x);}
```

2cb96dc7e18a96cfde1690e07e4f0e6ce845a9d5
ipynb
Jupyter Notebook
src/maths/overview.ipynb
OpenComputingLab/SubjectMatterNotebooks
1a7eff65528862c6b0c38eee6416d1ed814d4499
MIT
2021-12-07T07:55:49.000Z
2022-01-12T03:02:10.000Z
src/maths/overview.ipynb
OpenComputingLab/SubjectMatterNotebooks
1a7eff65528862c6b0c38eee6416d1ed814d4499
MIT
src/maths/overview.ipynb
OpenComputingLab/SubjectMatterNotebooks
1a7eff65528862c6b0c38eee6416d1ed814d4499
MIT
Qwen/Qwen-72B
1. YES
2. YES