Skip to content

Instantly share code, notes, and snippets.

@jkarakas
Last active June 14, 2018 07:47
Show Gist options
  • Save jkarakas/2693fb53aade3204414c7cd48a97d220 to your computer and use it in GitHub Desktop.
Save jkarakas/2693fb53aade3204414c7cd48a97d220 to your computer and use it in GitHub Desktop.
Turn regular numeric expressions in python into latex
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "## Numeric Expression in Python to Latex\nInstall and import Sympy and use it's methods that turn regular numeric expressions into latex \nInspired from: https://stackoverflow.com/questions/3867028/converting-a-python-numeric-expression-to-latex"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-06-14T07:46:10.743463Z",
"end_time": "2018-06-14T07:46:11.016712Z"
},
"trusted": true
},
"cell_type": "code",
"source": "from sympy import latex, sympify\nfrom IPython.display import display, Math",
"execution_count": 1,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### As a Function"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-06-14T07:46:11.018329Z",
"end_time": "2018-06-14T07:46:11.021100Z"
},
"trusted": true
},
"cell_type": "code",
"source": "def latify(equation: str) -> str:\n '''Convert the equation to the latex equivalent'''\n return latex(sympify(equation))",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-06-14T07:46:11.022679Z",
"end_time": "2018-06-14T07:46:11.040068Z"
},
"trusted": true
},
"cell_type": "code",
"source": "latify('1+2**(x+y)')",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "'2^{x + y} + 1'"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### As a Class"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-06-14T07:46:40.489328Z",
"end_time": "2018-06-14T07:46:40.519751Z"
},
"trusted": true
},
"cell_type": "code",
"source": "class Equation:\n def __init__(self, name: str, variables_dic : dict = {}):\n self.name = name\n self.variables = variables_dic\n\n def latify(self: str) -> str:\n '''Convert the equation to the latex equivalent'''\n return latex(sympify(self.name))\n\n def calculate(self) -> str:\n ''' Replace the variables with the inputed numbers,\n evaluate and print'''\n string = self.name\n for var, val in self.variables.items():\n val = str(val)\n string = string.replace(var, val)\n print(var + ' = ' + val)\n display(Math(self.latify()))\n print(' = ', eval(string) )\n ",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-06-14T07:46:40.919813Z",
"end_time": "2018-06-14T07:46:40.925958Z"
},
"trusted": true
},
"cell_type": "code",
"source": "e = Equation('1+2**(x+y+z)', variables_dic={'x': 5, 'y': 6, 'z': 7} )",
"execution_count": 9,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-06-14T07:46:41.438391Z",
"end_time": "2018-06-14T07:46:41.450425Z"
},
"trusted": true
},
"cell_type": "code",
"source": "e.latify()",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "'2^{x + y + z} + 1'"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2018-06-14T07:46:41.820295Z",
"end_time": "2018-06-14T07:46:41.834660Z"
},
"trusted": true
},
"cell_type": "code",
"source": "e.calculate()",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "x = 5\ny = 6\nz = 7\n",
"name": "stdout"
},
{
"output_type": "display_data",
"data": {
"text/plain": "<IPython.core.display.Math object>",
"text/latex": "$$2^{x + y + z} + 1$$"
},
"metadata": {}
},
{
"output_type": "stream",
"text": " = 262145\n",
"name": "stdout"
}
]
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/2693fb53aade3204414c7cd48a97d220"
},
"gist": {
"id": "2693fb53aade3204414c7cd48a97d220",
"data": {
"description": "Turn regular numeric expressions in python into latex",
"public": true
}
},
"hide_input": false,
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.4",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"nav_menu": {},
"number_sections": false,
"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
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment