master > master: nb - plotformatierung

This commit is contained in:
RD 2023-01-05 15:58:06 +01:00
parent b94b763cf1
commit 050dde99f6
2 changed files with 62 additions and 12 deletions

View File

@ -49,7 +49,7 @@
"u[8] = -0.7\n", "u[8] = -0.7\n",
"u[58] = 1j * sqrt(2);\n", "u[58] = 1j * sqrt(2);\n",
"\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", "# Anzeige der Lösung:\n",
"display_signal(u_guess, 'Geschätztes Signal');\n", "display_signal(u_guess, 'Geschätztes Signal');\n",
"\n", "\n",
"# Verifikation von Lösung:\n", "# Verifizierung der Lösung:\n",
"print(dedent(\n", "display_latex(\n",
" f'''\n", " f'''\n",
" %error := ‖u_guess u‖ / ‖u‖\n", " $$\n",
" = ‖F_n · (u_guess u)‖ / ‖F_n · u‖\n", " \\\\begin{{array}}{{rcl}}\n",
" ... wieso?? [Stichwort: unitär]\n", " \\\\%\\\\text{{error}}\n",
" = ‖F_n · u_guess F_n · u‖ / ‖F_n · u‖\n", " &\\colonequals\n",
" = {linalg.norm(F_n @ u_guess - F_mal_u)/linalg.norm(F_mal_u):.2%}\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" ");\n"
] ]
} }
], ],

View File

@ -6,14 +6,47 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from IPython.display import Latex; 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;
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; import ipywidgets as widgets;
# from array_to_latex import to_ltx as array_to_latex; # <- has issues # from array_to_latex import to_ltx as array_to_latex; # <- has issues
from qiskit.visualization import array_to_latex; 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 # EXPORTS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~