diff --git a/Simple syntax for typeset sympy mathematical operations.ipynb b/Simple syntax for typeset sympy mathematical operations.ipynb index 9623b86..8232016 100644 --- a/Simple syntax for typeset sympy mathematical operations.ipynb +++ b/Simple syntax for typeset sympy mathematical operations.ipynb @@ -2,13 +2,23 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "from IPython.display import HTML as html\n", "from IPython.display import display\n", "import sympy as sp\n", + "import inspect\n", + "\n", + "def retrieve_name(var):\n", + " callers_local_vars = inspect.currentframe().f_back.f_back.f_locals.items()\n", + " string_name = ''\n", + " for var_name, var_val in callers_local_vars:\n", + " if (var_val is var) and not (var_name.startswith('_')):\n", + " string_name = var_name\n", + " #return ([var_name for var_name, var_val in callers_local_vars if var_val is var],string_name)\n", + " return string_name\n", "\n", "def dmo(*exprs,**kwarg): #Display math operation\n", " '''\n", @@ -24,7 +34,10 @@ " another code cell.\n", " \n", " In an IPython/Jupyter notebook will display a typeset version of the operation and return\n", - " the expression to the namespace for further operations.\n", + " the expression to the namespace for further operations. The function tries to identify\n", + " the actual string (name) passed for the expression and output the typeset expression \n", + " `name=result of the operation`. If it cannot identify the actual string passed it just\n", + " outputs the typeset result of the operation.\n", " \n", " J. Gutow May 2020.\n", " '''\n", @@ -32,7 +45,11 @@ " expr=None\n", " if(len(exprs) >= 1):\n", " expr = exprs[0] #ignore others.\n", - " display(html(r'$'+sp.latex(expr)+'$'))\n", + " namestr=retrieve_name(expr)\n", + " if (namestr==''):\n", + " display(html(r'$'+sp.latex(expr)+'$'))\n", + " else:\n", + " display(html(r'$'+namestr+'='+sp.latex(expr)+'$'))\n", " else:\n", " key = list(kwarg)[0] #ignore all but first.\n", " expr = kwarg[key]\n", @@ -46,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -69,7 +86,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -78,7 +95,7 @@ "b*psi/c" ] }, - "execution_count": 3, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -89,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -112,7 +129,7 @@ "[3, 4]])" ] }, - "execution_count": 17, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -123,7 +140,29 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "$M=\\left[\\begin{matrix}1 & 2\\\\3 & 4\\end{matrix}\\right]$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "dmo(M)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -145,7 +184,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -180,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -202,7 +241,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -224,7 +263,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -246,7 +285,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 22, "metadata": {}, "outputs": [ { @@ -281,7 +320,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -303,7 +342,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -325,7 +364,29 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "$psi=e^{- i b}$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "dmo(psi)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, "metadata": {}, "outputs": [ { @@ -347,27 +408,29 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 28, "metadata": {}, "outputs": [ { "data": { + "text/html": [ + "$psi1=-0.7027 - 0.7115 i$" + ], "text/plain": [ - "-0.7027 - 0.7115*I" + "" ] }, - "execution_count": 14, "metadata": {}, - "output_type": "execute_result" + "output_type": "display_data" } ], "source": [ - "psi1" + "dmo(psi1)" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -376,7 +439,7 @@ "-0.7027 - 0.7115*I" ] }, - "execution_count": 15, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -441,6 +504,125 @@ "display(Latex(result))" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "pretty_print() got an unexpected keyword argument 'ppd'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0msympy\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mvar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ppa ppb ppc'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mpretty_print\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mppd\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mppa\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mppb\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mppc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: pretty_print() got an unexpected keyword argument 'ppd'" + ] + } + ], + "source": [ + "# abuse of pretty_print. It does not handle what dmo does.\n", + "from sympy import *\n", + "var('ppa ppb ppc')\n", + "pretty_print(ppd=ppa*ppb/ppc)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "$ppd=\\frac{ppa ppb}{ppc}$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "dmo(ppd=ppa*ppb/ppc)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'e^{- 2.75 i}'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "latex(exp(-I*2.75))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "$e^{- \\frac{1.375 i}{ppb}}$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display(html('$'+latex(exp(-I*2.75/ppb/2))+'$'))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "$0$" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display(html('$'+latex(diff(ppa,ppb))+'$'))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "latex?" + ] + }, { "cell_type": "code", "execution_count": null,