From 050dde99f65a99f278ae48f069fe57b058a6dd80 Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Thu, 5 Jan 2023 15:58:06 +0100 Subject: [PATCH] master > master: nb - plotformatierung --- notebooks/week-10.ipynb | 35 ++++++++++++++++++++++++++--------- src/thirdparty/render.py | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/notebooks/week-10.ipynb b/notebooks/week-10.ipynb index 604ff1a..8401284 100644 --- a/notebooks/week-10.ipynb +++ b/notebooks/week-10.ipynb @@ -49,7 +49,7 @@ "u[8] = -0.7\n", "u[58] = 1j * sqrt(2);\n", "\n", - "display_signal(u, 'Beispiel eines Vektors als Signal dargestellt');" + "display_signal(u, 'Beispiel eines Vektors in $\\mathbb{C}^{100}$ als Signal über $\\{0,1,2,\\ldots,99\\}$ dargestellt');" ] }, { @@ -211,16 +211,33 @@ "# Anzeige der Lösung:\n", "display_signal(u_guess, 'Geschätztes Signal');\n", "\n", - "# Verifikation von Lösung:\n", - "print(dedent(\n", + "# Verifizierung der Lösung:\n", + "display_latex(\n", " f'''\n", - " %error := ‖u_guess – u‖ / ‖u‖\n", - " = ‖F_n · (u_guess – u)‖ / ‖F_n · u‖\n", - " ... wieso?? [Stichwort: unitär]\n", - " = ‖F_n · u_guess – F_n · u‖ / ‖F_n · u‖\n", - " = {linalg.norm(F_n @ u_guess - F_mal_u)/linalg.norm(F_mal_u):.2%}\n", + " $$\n", + " \\\\begin{{array}}{{rcl}}\n", + " \\\\%\\\\text{{error}}\n", + " &\\colonequals\n", + " &\\\\dfrac{{\\\\|u_{{\\\\text{{guess}}}} - u\\\\|}}{{\\\\|u\\\\|}}\\\\\\\\\n", + " &=\n", + " &\\\\dfrac{{\n", + " \\\\|\\\\mathcal{{F}}_{{n}}\\\\,(u_{{\\\\text{{guess}}}} - u)\\\\|\n", + " }}{{\n", + " \\\\|\\\\mathcal{{F}}_{{n}}\\\\,u\\\\|\n", + " }}\n", + " \\\\quad\\\\text{{\\\\ldots wieso?? [Stichwort: unitär!]}}\\\\\\\\\n", + " &= &\\\\dfrac{{\n", + " \\\\|\\\\mathcal{{F}}_{{n}}\\\\,u_{{\\\\text{{guess}}}} - \\\\mathcal{{F}}_{{n}}\\\\,u\\\\|\n", + " }}{{\n", + " \\\\|\\\\mathcal{{F}}_{{n}}\\\\,u\\\\|\n", + " }}\\\\\\\\\n", + " &= &{linalg.norm(F_n @ u_guess - F_mal_u)/linalg.norm(F_mal_u):.2%}\n", + " \\\\%\n", + " \\\\\\\\\n", + " \\\\end{{array}}\n", + " $$\n", " '''\n", - "));\n" + ");\n" ] } ], diff --git a/src/thirdparty/render.py b/src/thirdparty/render.py index 477c6dc..0a1e510 100644 --- a/src/thirdparty/render.py +++ b/src/thirdparty/render.py @@ -6,14 +6,47 @@ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ from IPython.display import Latex; -from IPython.display import display_latex; -from IPython.display import display_png; -from IPython.display import display_markdown; from IPython.display import display; +from IPython.display import display_png; +from IPython.display import display as display_text_ipython; +from IPython.display import display_latex as display_latex_ipython; +from IPython.display import display_markdown as display_md_ipython; import ipywidgets as widgets; # from array_to_latex import to_ltx as array_to_latex; # <- has issues from qiskit.visualization import array_to_latex; +from functools import wraps; +from typing import Callable; +from typing import TypeVar; + +from src.thirdparty.misc import dedent; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# MODIFICATIONS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +T1 = TypeVar('T1'); +T2 = TypeVar('T2'); + +def display_with_dedent(pre_process: Callable[[str], T1] = lambda x: x): + ''' + Returns a decorator that modifies string -> string methods + ''' + def dec(method: Callable[[T1], T2]) -> Callable[[str], T2]: + ''' + Performs method but dedents first. + ''' + @wraps(method) + def wrapped_method(text: str) -> T2: + return method(pre_process(dedent(text))); + return wrapped_method; + + return dec; + +display_latex = display_with_dedent(Latex)(display_latex_ipython); +display_text = display_with_dedent()(display_text_ipython); +display_markdown = display_with_dedent()(display_md_ipython); + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # EXPORTS # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~