Last active
May 2, 2019 23:14
-
-
Save isuruf/e97ddbd6a74e6043ca4d469629ab21ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sympy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from sympy import StrPrinter, S, Symbol, FunctionClass, Add, Mul, Pow, Integral\n", | |
"from sympy.printing.printer import Printer\n", | |
"import sympy\n", | |
"\n", | |
"class GuppyPrinter(Printer):\n", | |
" def _print_Function(self, expr):\n", | |
" return [expr.func.__name__, [self._print(i) for i in expr.args]]\n", | |
" \n", | |
" def _print_Add(self, expr):\n", | |
" return [\"+\", [self._print(i) for i in expr.args]]\n", | |
" \n", | |
" def _print_Mul(self, expr):\n", | |
" return [\"*\", [self._print(i) for i in expr.args]]\n", | |
" \n", | |
" def _print_Pow(self, expr):\n", | |
" return [\"exponential\", [self._print(i) for i in expr.args]]\n", | |
"\n", | |
" def _print_Symbol(self, expr):\n", | |
" return [\"var\", [expr.name]]\n", | |
" \n", | |
" def _print_Integer(self, expr):\n", | |
" return [\"val\", [str(expr)]]\n", | |
" \n", | |
" def _print_Rational(self, expr):\n", | |
" return [\"fraction\", [self._print(S(i)) for i in (expr.p, expr.q)]]\n", | |
" \n", | |
" def _print_Integral(self, expr):\n", | |
" if len(expr.args[1]) == 1:\n", | |
" return [\"integral\", [self._print(i) for i in (expr.args[0], expr.args[1][0])]]\n", | |
" else:\n", | |
" args = expr.args[1][1], expr.args[1][2], expr.args[0], expr.args[1][0]\n", | |
" return [\"defintegral\", [self._print(i) for i in args]]\n", | |
" \n", | |
" _print_Float = _print_Integer\n", | |
"\n", | |
"def guppy_to_sympy(expr):\n", | |
" assert isinstance(expr, list)\n", | |
" if expr[0] == \"val\":\n", | |
" return S(expr[1][0])\n", | |
" elif expr[0] == \"var\":\n", | |
" return Symbol(expr[1][0])\n", | |
" elif expr[0] == \"+\":\n", | |
" args = [guppy_to_sympy(arg) for arg in expr[1]]\n", | |
" return Add(*args)\n", | |
" elif expr[0] == \"*\":\n", | |
" args = [guppy_to_sympy(arg) for arg in expr[1]]\n", | |
" return Mul(*args)\n", | |
" elif expr[0] == \"exponential\":\n", | |
" args = [guppy_to_sympy(arg) for arg in expr[1]]\n", | |
" return Pow(*args)\n", | |
" elif expr[0] == \"-\":\n", | |
" args = [guppy_to_sympy(arg) for arg in expr[1]]\n", | |
" assert len(args) == 2\n", | |
" return args[0] - args[1]\n", | |
" elif expr[0] == \"fraction\":\n", | |
" args = [guppy_to_sympy(arg) for arg in expr[1]]\n", | |
" assert len(args) == 2\n", | |
" return args[0] / args[1]\n", | |
" elif expr[0] == \"neg\":\n", | |
" args = [guppy_to_sympy(arg) for arg in expr[1]]\n", | |
" assert len(args) == 1\n", | |
" return -args[0]\n", | |
" elif expr[0] == \"integral\":\n", | |
" args = [guppy_to_sympy(arg) for arg in expr[1]]\n", | |
" return Integral(args[0], args[1])\n", | |
" elif expr[0] == \"defintegral\":\n", | |
" args = [guppy_to_sympy(arg) for arg in expr[1]]\n", | |
" return Integral(args[2], (args[3], args[0], args[1]))\n", | |
" elif hasattr(sympy, expr[0]) and isinstance(getattr(sympy, expr[0]), FunctionClass):\n", | |
" args = [guppy_to_sympy(arg) for arg in expr[1]]\n", | |
" return getattr(sympy, expr[0])(*args)\n", | |
"\n", | |
"def load_ipython_formula_editor():\n", | |
" from IPython.core.magic import register_line_magic\n", | |
" from IPython.display import display, HTML, publish_display_data\n", | |
"\n", | |
" html_data = \"\"\"\n", | |
"<link rel=\"stylesheet\" type=\"text/css\" href=\"https://guppy.js.org/build/guppy-default.min.css\">\n", | |
"<div id=\"guppy{i}\"></div>\n", | |
"<script>\n", | |
"\n", | |
"$.getScript( \"https://guppy.js.org/build/guppy.js\", function( data, textStatus, jqxhr ) {{\n", | |
" var g1 = new Guppy(\"guppy{i}\");\n", | |
" g1.import_syntax_tree({value})\n", | |
" \n", | |
" function sendToIPython(e) {{\n", | |
" var kernel = IPython.notebook.kernel;\n", | |
" try {{\n", | |
" var execcmd = '{resultVar} = guppy_to_sympy('+ g1.engine.get_content(\"ast\") + ')';\n", | |
" console.log(execcmd);\n", | |
" kernel.execute(execcmd);\n", | |
" }} catch(e) {{\n", | |
"\n", | |
" }}\n", | |
" }}\n", | |
"\n", | |
" g1.event(\"change\", sendToIPython);\n", | |
" g1.render(true);\n", | |
" sendToIPython(1);\n", | |
"}});\n", | |
"</script>\n", | |
"\"\"\"\n", | |
" \n", | |
" printer = GuppyPrinter()\n", | |
"\n", | |
" @register_line_magic\n", | |
" def formula(expr):\n", | |
" import sympy\n", | |
" if expr == \"\":\n", | |
" resultVar = \"resultVar\"\n", | |
" value = S(0)\n", | |
" elif expr.isidentifier():\n", | |
" resultVar = expr\n", | |
" if expr in globals():\n", | |
" value = S(globals()[expr])\n", | |
" else:\n", | |
" value = S(0)\n", | |
" else:\n", | |
" value = S(expr)\n", | |
" resultVar = \"resultVar\"\n", | |
" guppy_value = printer.doprint(value)\n", | |
" out_html = html_data.format(i=formula.counter, resultVar=resultVar, value=guppy_value)\n", | |
" formula.counter += 1\n", | |
" display(HTML(out_html))\n", | |
"\n", | |
" formula.counter = 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"load_ipython_formula_editor()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"\n", | |
"<link rel=\"stylesheet\" type=\"text/css\" href=\"https://guppy.js.org/build/guppy-default.min.css\">\n", | |
"<div id=\"guppy0\"></div>\n", | |
"<script>\n", | |
"\n", | |
"$.getScript( \"https://guppy.js.org/build/guppy.js\", function( data, textStatus, jqxhr ) {\n", | |
" var g1 = new Guppy(\"guppy0\");\n", | |
" g1.import_syntax_tree(['defintegral', [['val', ['0']], ['val', ['1']], ['*', [['val', ['3']], ['exponential', [['var', ['x']], ['val', ['2']]]]]], ['var', ['x']]]])\n", | |
" \n", | |
" function sendToIPython(e) {\n", | |
" var kernel = IPython.notebook.kernel;\n", | |
" try {\n", | |
" var execcmd = 'resultVar = guppy_to_sympy('+ g1.engine.get_content(\"ast\") + ')';\n", | |
" console.log(execcmd);\n", | |
" kernel.execute(execcmd);\n", | |
" } catch(e) {\n", | |
"\n", | |
" }\n", | |
" }\n", | |
"\n", | |
" g1.event(\"change\", sendToIPython);\n", | |
" g1.render(true);\n", | |
" sendToIPython(1);\n", | |
"});\n", | |
"</script>\n" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"# Edit a explicit formula\n", | |
"%formula Integral(3*x**2, (x, 0, 1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle \\sin{\\left(y \\right)} + \\int\\limits_{0}^{1} 3 x^{2}\\, dx$" | |
], | |
"text/plain": [ | |
"sin(y) + Integral(3*x**2, (x, 0, 1))" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Value is stored in resultVar\n", | |
"resultVar" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from sympy import symbols, cos\n", | |
"x, y = symbols(\"x, y\")\n", | |
"w = cos(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"\n", | |
"<link rel=\"stylesheet\" type=\"text/css\" href=\"https://guppy.js.org/build/guppy-default.min.css\">\n", | |
"<div id=\"guppy1\"></div>\n", | |
"<script>\n", | |
"\n", | |
"$.getScript( \"https://guppy.js.org/build/guppy.js\", function( data, textStatus, jqxhr ) {\n", | |
" var g1 = new Guppy(\"guppy1\");\n", | |
" g1.import_syntax_tree(['cos', [['var', ['x']]]])\n", | |
" \n", | |
" function sendToIPython(e) {\n", | |
" var kernel = IPython.notebook.kernel;\n", | |
" try {\n", | |
" var execcmd = 'w = guppy_to_sympy('+ g1.engine.get_content(\"ast\") + ')';\n", | |
" console.log(execcmd);\n", | |
" kernel.execute(execcmd);\n", | |
" } catch(e) {\n", | |
"\n", | |
" }\n", | |
" }\n", | |
"\n", | |
" g1.event(\"change\", sendToIPython);\n", | |
" g1.render(true);\n", | |
" sendToIPython(1);\n", | |
"});\n", | |
"</script>\n" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"# Edit the value of variable \"w\"\n", | |
"%formula w" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$\\displaystyle \\cos{\\left(x \\right)} + 3$" | |
], | |
"text/plain": [ | |
"cos(x) + 3" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Value is stored in variable \"w\"\n", | |
"w" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"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.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment