Wyświetl online:
Last active
November 11, 2021 11:45
-
-
Save Maarrk/8657bdba703a511de01fd13738239b30 to your computer and use it in GitHub Desktop.
rownania.ipynb
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": "markdown", | |
"source": [ | |
"# Dynamiczne równania w Jupyter\r\n", | |
"\r\n", | |
"Następna komórka ustawia środowisko tak żeby można było wygodnie wyświetlać równania" | |
], | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"source": [ | |
"from IPython.display import display, Markdown, Latex # Display cells with dynamic content\r\n", | |
"from string import Template # Replace text with $ instead of {}, much better for LaTeX equations\r\n", | |
"\r\n", | |
"def round_numbers(data: dict, places: int) -> dict:\r\n", | |
" \"\"\"Setup floating point variables for string substitution\r\n", | |
"\r\n", | |
" Parameters\r\n", | |
" ----------\r\n", | |
" data : dict\r\n", | |
" A dictionary of values with string keys\r\n", | |
" places : int\r\n", | |
" Number of decimal places in string representation\r\n", | |
"\r\n", | |
" Returns\r\n", | |
" -------\r\n", | |
" dict\r\n", | |
" A deep copy of data with floating points converted to str\r\n", | |
" \"\"\"\r\n", | |
" format_string = '{:.' + str(places) + 'f}'\r\n", | |
" result = dict()\r\n", | |
" for k, v in data.items():\r\n", | |
" if isinstance(v, float):\r\n", | |
" result[k] = format_string.format(v)\r\n", | |
" else:\r\n", | |
" result[k] = v\r\n", | |
" return result\r\n", | |
"\r\n", | |
"tex = lambda text, data, places = 3: display(Latex(Template(text).substitute(round_numbers(data, places))))\r\n", | |
"md = lambda text, data, places = 3: display(Markdown(Template(text).substitute(round_numbers(data, places))))\r\n" | |
], | |
"outputs": [], | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Potem wystarczy zebrać wyświetlane wartości w słownik, i odwoływać się do nich w równaniu przez znak `$`. Każde dwa obok siebie zostaną zmienione w pojedynczy - konieczne żeby wskazać środowisko matematyczne. Tekst jest poprzedzony znakiem `r`, dzięki czemu Python ignoruje wszystkie `\\` i umożliwia przetwarzanie ich przez LaTeX." | |
], | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"source": [ | |
"from math import sqrt\r\n", | |
"\r\n", | |
"num = dict() # Container for all displayed numbers\r\n", | |
"num['phi'] = (sqrt(5) + 1.0) / 2.0\r\n", | |
"tex(r'$$ \\phi = \\frac{\\sqrt{5} + 1}{2} = $phi $$', num, 5)" | |
], | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Latex object>" | |
], | |
"text/latex": [ | |
"$$ \\phi = \\frac{\\sqrt{5} + 1}{2} = 1.61803 $$" | |
] | |
}, | |
"metadata": {} | |
} | |
], | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"source": [ | |
"md(r\"\"\"\r\n", | |
"Można też pisać całe akapity w których równania są w tekście $$ \\phi = $phi $$, (jak na przykład to)\r\n", | |
"\r\n", | |
"Dane do obu podstawień są dostarczane dokładnie w ten sam sposób.\r\n", | |
"\"\"\", num)" | |
], | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Markdown object>" | |
], | |
"text/markdown": [ | |
"\n", | |
"Można też pisać całe akapity w których równania są w tekście $ \\phi = 1.618 $, (jak na przykład to)\n", | |
"\n", | |
"Dane do obu podstawień są dostarczane dokładnie w ten sam sposób.\n" | |
] | |
}, | |
"metadata": {} | |
} | |
], | |
"metadata": {} | |
} | |
], | |
"metadata": { | |
"orig_nbformat": 4, | |
"language_info": { | |
"name": "python", | |
"version": "3.9.1", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3.9.1 64-bit" | |
}, | |
"interpreter": { | |
"hash": "ac59ebe37160ed0dfa835113d9b8498d9f09ceb179beaac4002f036b9467c963" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment