Skip to content

Instantly share code, notes, and snippets.

@lapis-zero09
Last active March 27, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lapis-zero09/aa25a6e91c363265057134dc7de04a8d to your computer and use it in GitHub Desktop.
Save lapis-zero09/aa25a6e91c363265057134dc7de04a8d to your computer and use it in GitHub Desktop.
cookbook/python_formula2tex.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-03-27T13:44:01.038269Z",
"end_time": "2018-03-27T13:44:01.063304Z"
},
"trusted": true
},
"cell_type": "code",
"source": "def python_formula2tex(formula, ret=None):\n import sympy\n import inspect\n\n formula = inspect.getsourcelines(f)[0][0].split(\": \")[1].replace('np.', '')\n tex_expr = sympy.latex(sympy.simplify(formula))\n\n from IPython.display import display, Markdown\n display(Markdown(f'$ {tex_expr} $'))\n\n if ret:\n return tex_expr",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-03-27T13:44:01.661132Z",
"end_time": "2018-03-27T13:44:01.756748Z"
},
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-03-27T13:44:02.167671Z",
"end_time": "2018-03-27T13:44:03.217861Z"
},
"trusted": true
},
"cell_type": "code",
"source": "f = lambda x, y: np.sin(3 * np.pi * x)**2 + (x-1)**2 * (1+np.sin(3 * np.pi * y)**2) + (y-1)**2 * (1+np.sin(2*np.pi*y)**2)\na = python_formula2tex(f, ret=True)",
"execution_count": 3,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "<IPython.core.display.Markdown object>",
"text/markdown": "$ \\left(x - 1\\right)^{2} \\left(\\sin^{2}{\\left (3 \\pi y \\right )} + 1\\right) + \\left(y - 1\\right)^{2} \\left(\\sin^{2}{\\left (2 \\pi y \\right )} + 1\\right) + \\sin^{2}{\\left (3 \\pi x \\right )} $"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"latex_envs": {
"eqNumInitial": 1,
"eqLabelWithNumbers": true,
"current_citInitial": 1,
"cite_by": "apalike",
"bibliofile": "biblio.bib",
"LaTeX_envs_menu_present": true,
"labels_anchors": false,
"latex_user_defs": false,
"user_envs_cfg": false,
"report_style_numbering": false,
"autoclose": false,
"autocomplete": true,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
}
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "9e9d7e428a044ceab82d86c1ff6c2845",
"data": {
"description": "cookbook/python_formula2tex.ipynb",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/9e9d7e428a044ceab82d86c1ff6c2845"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment