{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# A Simple Registry for Managing and Auto-numbering _sympy_ Equations\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"bla bl\n",
"\n",
"\n",
" \n",
"\n",
"\n",
" **Fig 1. JupyterLab Autocompletion Dialog for Registered Equations**\n",
""
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sympy as sp\n",
"\n",
"\n",
"class EquationRegistry():\n",
"\n",
" def __init__(self):\n",
" # bypass self.__setattr__\n",
" self.__dict__['_indexmap'] ={}\n",
"\n",
" def __setattr__(self, name, value):\n",
" index = self._indexmap.get(name,0)\n",
" if index == 0: # a new field is requested\n",
" self._indexmap[name] = len(self._indexmap) + 1\n",
" self.__dict__[name] = value\n",
"\n",
" def __call__(self, name):\n",
" index = self._indexmap[name]\n",
" self._indexmap[name] = index # mark as published\n",
" return IPd.Math(r'\\begin{align}\\fbox{\\(' \\\n",
" + sp.latex(self.__dict__[name]) \\\n",
" + r'\\)} \\tag*{(' \\\n",
" + str(index) \\\n",
" + r')}\\end{align}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"from sympy import Sum, Eq, IndexedBase, diff, Function, factor_terms, expand\n",
"from sympy.abc import *\n",
"import IPython.display as IPd\n",
"\n",
"EQ = EquationRegistry() # create the equation registry for this notebook"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\begin{align}\\fbox{\\({\\epsilon}_{i} = x - {p}_{i}\\)} \\tag*{(1)}\\end{align}$"
],
"text/plain": [
""
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"epsilon = IndexedBase('\\epsilon')\n",
"p = IndexedBase('p')\n",
"EQ.error = Eq(epsilon[i], x-p[i])\n",
"EQ('error')"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\begin{align}\\fbox{\\(E{\\left(x \\right)} = \\sum_{i=0}^{n - 1} \\left(x - {p}_{i}\\right)^{2}\\)} \\tag*{(2)}\\end{align}$"
],
"text/plain": [
""
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"E=Function('E')\n",
"EQ.energy=Eq(E(x),Sum(EQ.error.rhs**2,(i,0,n-1)))\n",
"EQ('energy')"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\begin{align}\\fbox{\\(\\frac{d}{d x} E{\\left(x \\right)} = \\sum_{i=0}^{n - 1} \\left(2 x - 2 {p}_{i}\\right)\\)} \\tag*{(3)}\\end{align}$"
],
"text/plain": [
""
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"EQ.dE_dx = Eq(diff(EQ.energy.lhs,x),EQ.energy.rhs.diff(x))\n",
"EQ('dE_dx')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first derivative of the energy function $E(x)$ can be simplified like so:"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\begin{align}\\fbox{\\(\\frac{d}{d x} E{\\left(x \\right)} = 2 n x - 2 \\sum_{i=0}^{n - 1} {p}_{i}\\)} \\tag*{(4)}\\end{align}$"
],
"text/plain": [
""
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"EQ.dE_dx_simple = Eq(EQ.dE_dx.lhs,factor_terms(expand(EQ.dE_dx.rhs)).doit())\n",
"EQ('dE_dx_simple') "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Appendix\n",
"\n",
"Packages used in this gist:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/markdown": [
"**Python Packages used in This Notebook**"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"| Component | Version | Description |\n",
"| --------------------------------- | -------------------------- | -------------------- |\n",
"| [Python](https://www.python.org/) | 3.8.6 | Programming Language |\n",
"| [jnbBuffs](https://github.com/WetHat/jupyter-notebooks) | 0.1.2 | Utilities for authoring JupyterLab Python notebooks. |\n",
"| [jupyterlab](http://jupyter.org) | 3.0.12 | The JupyterLab server extension. |\n",
"| [sympy](https://sympy.org) | 1.7.1 | Computer algebra system (CAS) in Python |"
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from jnbBuffs.manifest import notebook_manifest\n",
"import IPython.display as IPd\n",
"\n",
"display(IPd.Markdown('**Python Packages used in This Notebook**'))\n",
"notebook_manifest('jupyterlab', 'sympy', 'jnbBuffs')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (JupyterLab)",
"language": "python",
"name": "jupyterlab"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}