Skip to content

Commit

Permalink
Better determination of expression name
Browse files Browse the repository at this point in the history
Search up in the locals() tree to find the string name used to call the expression.
  • Loading branch information
gutow authored May 4, 2020
1 parent 288a755 commit 255d25a
Showing 1 changed file with 206 additions and 24 deletions.
230 changes: 206 additions & 24 deletions Simple syntax for typeset sympy mathematical operations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -24,15 +34,22 @@
" 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",
" code = kwarg.pop('code', None)\n",
" 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",
Expand All @@ -46,7 +63,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand All @@ -69,7 +86,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 14,
"metadata": {},
"outputs": [
{
Expand All @@ -78,7 +95,7 @@
"b*psi/c"
]
},
"execution_count": 3,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -89,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand All @@ -112,7 +129,7 @@
"[3, 4]])"
]
},
"execution_count": 17,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -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": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"dmo(M)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand All @@ -145,7 +184,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 18,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -180,7 +219,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand All @@ -202,7 +241,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 20,
"metadata": {},
"outputs": [
{
Expand All @@ -224,7 +263,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 21,
"metadata": {},
"outputs": [
{
Expand All @@ -246,7 +285,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 22,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -281,7 +320,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 23,
"metadata": {},
"outputs": [
{
Expand All @@ -303,7 +342,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 24,
"metadata": {},
"outputs": [
{
Expand All @@ -325,7 +364,29 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"$psi=e^{- i b}$"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"dmo(psi)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
Expand All @@ -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"
"<IPython.core.display.HTML object>"
]
},
"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": [
{
Expand All @@ -376,7 +439,7 @@
"-0.7027 - 0.7115*I"
]
},
"execution_count": 15,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -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<ipython-input-1-96cf5b2ff64f>\u001b[0m in \u001b[0;36m<module>\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": [
"<IPython.core.display.HTML object>"
]
},
"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": [
"<IPython.core.display.HTML object>"
]
},
"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": [
"<IPython.core.display.HTML object>"
]
},
"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,
Expand Down

0 comments on commit 255d25a

Please sign in to comment.