{ "cells": [ { "cell_type": "markdown", "id": "46879ddc", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "id": "88f4dd69", "metadata": {}, "source": [ "# Nice MathJax for Matrices\n", "\n", "Purpose of this notebook is to make plain ascii form of matrices look more visually appealing. All that done by just a few lines of code inside Jupyter notebooks with help of [MathJax](https://www.mathjax.org/) and `IPython.display` methods. In particular methods `Math` and `Markdown`.\n", "\n", "Great source of information about `MathJax` syntax is at [Basic Tutorial](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference)." ] }, { "cell_type": "markdown", "id": "ad2f8507", "metadata": {}, "source": [ "## The eye candy function" ] }, { "cell_type": "markdown", "id": "31026c0b", "metadata": {}, "source": [ "### Some code hygiene\n", "\n", "Let's make the code more reproducible by capturing module versions and environment using [watermark](https://github.com/rasbt/watermark)" ] }, { "cell_type": "code", "execution_count": 1, "id": "9756d04d", "metadata": {}, "outputs": [], "source": [ "!pip -q install watermark\n", "# assuming that you have already installed all the dependencies and libraries\n", "# uncomment below if needed\n", "#!pip -q install tensorflow torch numpy" ] }, { "cell_type": "code", "execution_count": 2, "id": "61108a82", "metadata": {}, "outputs": [], "source": [ "from IPython.display import Math\n", "from IPython.display import Markdown\n", "import tensorflow as tf\n", "import torch\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 3, "id": "c54fc993", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Last updated: 2021-08-02T13:07:23.444236+02:00\n", "\n", "Compiler : Clang 12.0.5 (clang-1205.0.22.9)\n", "OS : Darwin\n", "Release : 20.6.0\n", "Machine : x86_64\n", "Processor : i386\n", "CPU cores : 8\n", "Architecture: 64bit\n", "\n", "torch : 1.9.0\n", "numpy : 1.19.5\n", "tensorflow: 2.5.0\n", "\n", "Watermark: 2.2.0\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -iv -i -z -u -m -w" ] }, { "cell_type": "markdown", "id": "3fbfcf42", "metadata": {}, "source": [ "### The pair of functions\n", "\n", "The functions below are the heart of this notebook reference, together with the imports above they are the piece that should be used wherever needed. Most probably only the `sm_sd` function only as it is more portable." ] }, { "cell_type": "code", "execution_count": 4, "id": "48602248", "metadata": {}, "outputs": [], "source": [ "## The Math version of a matrix formating\n", "# which might or might not be available, relies on __IPython.display.Math__ working properly\n", "# (not supported at Google's Colab)\n", "def sm_math(mat,mname=\"A\"):\n", " latex = \"\"\n", " latex += \"\\\\renewcommand{\\\\vec}[1]{\\\\boldsymbol{#1}}\\n\"\n", " latex += \"\\\\renewcommand{\\\\matrix}[1]{\\\\boldsymbol{\\\\mathrm{#1}}}\\n\"\n", " latex += \"\\\\newcommand{\\\\tensor}[1]{\\\\boldsymbol{\\mathrm{#1}}}\\n\"\n", " if mname is not None:\n", " latex += \"\\\\matrix{\"+mname+\"} = \"\n", " latex += \"\\\\begin{bmatrix}\"\n", " tmp_rows = list()\n", " for rr in range(len(mat)):\n", " tmp_rows.append(\" & \".join([(f'{item:.2f}').rstrip('0').rstrip('.') for item in mat[rr]]))\n", " latex += \"\\\\\\\\\".join(tmp_rows)\n", " latex += \"\\\\end{bmatrix}\"\n", " return display(Math(latex))\n", "\n", "## More simplistic version of function above\n", "# which works on Google's Colab as well\n", "def sm_md(inmat):\n", " md = \"\"\n", " md += \"\\\\begin{bmatrix}\"\n", " tmp_rows = list()\n", " mat = inmat\n", " # Pythonify the TensorFlow\n", " if isinstance(inmat,tf.Variable):\n", " mat = inmat.numpy().tolist()\n", " # Pythonify the Numpy and PyTorch\n", " if isinstance(inmat,(torch.Tensor,np.ndarray)):\n", " mat = inmat.tolist()\n", " for rr in range(len(mat)):\n", " tmp_rows.append(\" & \".join([(f'{item:.2f}').rstrip('0').rstrip('.') for item in mat[rr]]))\n", " md += \"\\\\\\\\\".join(tmp_rows)\n", " md += \"\\\\end{bmatrix}\"\n", " return display(Markdown(md))" ] }, { "cell_type": "markdown", "id": "a51fa997", "metadata": {}, "source": [ "## Debug and test\n", "\n", "so let's test them, this one works in local `jupyter-notebook`" ] }, { "cell_type": "code", "execution_count": 5, "id": "733e69fe", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\renewcommand{\\vec}[1]{\\boldsymbol{#1}}\n", "\\renewcommand{\\matrix}[1]{\\boldsymbol{\\mathrm{#1}}}\n", "\\newcommand{\\tensor}[1]{\\boldsymbol{\\mathrm{#1}}}\n", "\\begin{bmatrix}2 & 3 & 4\\\\5 & 6 & 7\\\\20 & 7.98 & 6\\end{bmatrix}$" ], "text/plain": [ "