Compare commits
3 Commits
0384334ce0
...
6d496dd103
Author | SHA1 | Date | |
---|---|---|---|
6d496dd103 | |||
76a6564a3e | |||
847e7744ff |
223
notebooks/week-10.ipynb
Normal file
223
notebooks/week-10.ipynb
Normal file
@ -0,0 +1,223 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"'''IMPORTS'''\n",
|
||||||
|
"import os;\n",
|
||||||
|
"import sys; \n",
|
||||||
|
"\n",
|
||||||
|
"# NOTE: need this to force jupyter to reload imports:\n",
|
||||||
|
"for key in list(sys.modules.keys()):\n",
|
||||||
|
" if key.startswith('src.'):\n",
|
||||||
|
" del sys.modules[key];\n",
|
||||||
|
"\n",
|
||||||
|
"os.chdir(os.path.dirname(_dh[0]));\n",
|
||||||
|
"sys.path.insert(0, os.getcwd());\n",
|
||||||
|
"\n",
|
||||||
|
"from src.thirdparty.maths import *;\n",
|
||||||
|
"from src.thirdparty.misc import *;\n",
|
||||||
|
"from src.thirdparty.render import *;\n",
|
||||||
|
"from src.maths.diagrams import *;\n",
|
||||||
|
"\n",
|
||||||
|
"np.random.seed(8007253); # zur Wiederholbarkeit"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"'''BEISPIEL-SIGNAL'''\n",
|
||||||
|
"n = 100;\n",
|
||||||
|
"u = np.zeros((n,), dtype=complex);\n",
|
||||||
|
"u[8] = -0.7\n",
|
||||||
|
"u[58] = 1j * sqrt(2);\n",
|
||||||
|
"\n",
|
||||||
|
"display_signal(u, 'Beispiel eines Vektors als Signal dargestellt');"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"'''SEMINARAUFGABE 10.4 a)'''\n",
|
||||||
|
"n = 3;\n",
|
||||||
|
"t = linspace(0, n-1, n);\n",
|
||||||
|
"i, j = np.meshgrid(t, t);\n",
|
||||||
|
"theta = 2*pi * i * j / n;\n",
|
||||||
|
"\n",
|
||||||
|
"# definiere die Matrix F_n + die Vektoren u_nk\n",
|
||||||
|
"F_n = (1/sqrt(n)) * exp(-1j * theta);\n",
|
||||||
|
"u_n = [ (1/sqrt(n)) * exp(1j * theta[:, k]) for k in range(n) ];\n",
|
||||||
|
"\n",
|
||||||
|
"print(dedent(\n",
|
||||||
|
" f'''\n",
|
||||||
|
" Matrix:\n",
|
||||||
|
"\n",
|
||||||
|
" F_{n} = \\n{np.round(F_n, 3)}\n",
|
||||||
|
" '''\n",
|
||||||
|
"));\n",
|
||||||
|
"print('');\n",
|
||||||
|
"\n",
|
||||||
|
"# NOTE: Programmiersprachen können nicht exakt berechnen.\n",
|
||||||
|
"# Darum entstehen winzige Fehler, die sich durch Runden entfernen lassen.\n",
|
||||||
|
"print('Matrix-Vektor-Produkte:');\n",
|
||||||
|
"for k in range(n):\n",
|
||||||
|
" result = F_n @ u_n[k];\n",
|
||||||
|
" print(dedent(\n",
|
||||||
|
" f'''\n",
|
||||||
|
"\n",
|
||||||
|
" u_{{n {k}}} = {np.round(u_n[k], 3)}\n",
|
||||||
|
" F_n · u_{{n {k}}} = {np.round(result, 3)}\n",
|
||||||
|
" '''\n",
|
||||||
|
" ));"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"attachments": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Seminaraufgabe 10.4 b) ##\n",
|
||||||
|
"\n",
|
||||||
|
"Für $i\\in\\{0,1,\\ldots,n-1\\}$ ist die $i$-te von $\\mathcal{F}_{n}\\,u_{n,k}$\n",
|
||||||
|
"\n",
|
||||||
|
"$$\n",
|
||||||
|
" (\\mathcal{F}_{n}\\,u_{n,k})_{i}\n",
|
||||||
|
" = \\sum_{j=0}^{n-1}\n",
|
||||||
|
" (\\mathcal{F}_{n})_{ij}\\,(u_{n,k})_{j}\n",
|
||||||
|
" = \\sum_{j=0}^{n-1}\n",
|
||||||
|
" \\tfrac{1}{\\sqrt{n}}\n",
|
||||||
|
" \\exp(-\\imath\\tfrac{2\\pi\\,ij}{n})\n",
|
||||||
|
" \\cdot\n",
|
||||||
|
" \\tfrac{1}{\\sqrt{n}}\n",
|
||||||
|
" \\exp(\\imath\\tfrac{2\\pi\\,kj}{n})\n",
|
||||||
|
" = \\frac{1}{n}\\sum_{j=0}^{n-1}\n",
|
||||||
|
" \\underbrace{\n",
|
||||||
|
" \\exp(\\imath\\tfrac{2\\pi\\,(k-i)j}{n})\n",
|
||||||
|
" }_{= r^{j}}\n",
|
||||||
|
"$$\n",
|
||||||
|
"\n",
|
||||||
|
"wobei $r \\colonequals \\exp(\\imath\\tfrac{2\\pi\\,(k-i)}{n})$.\n",
|
||||||
|
"Wir haben, dass\n",
|
||||||
|
" $r = 1$\n",
|
||||||
|
" $\\Leftrightarrow$\n",
|
||||||
|
" $\\tfrac{k-i}{n} \\in \\Z$\n",
|
||||||
|
" $\\Leftrightarrow$\n",
|
||||||
|
" $i = k$,\n",
|
||||||
|
"da $0\\leq i,k \\leq n-1$.\n",
|
||||||
|
"<br>\n",
|
||||||
|
"Es gilt außerdem $r^{n} = \\exp(\\imath 2\\pi\\,(k-i)) = 1$.\n",
|
||||||
|
"<br>\n",
|
||||||
|
"Aus der o.s. Berechnung erhält man also\n",
|
||||||
|
"\n",
|
||||||
|
"$$\n",
|
||||||
|
" (\\mathcal{F}_{n}\\,u_{n,k})_{i}\n",
|
||||||
|
" = \\tfrac{1}{n}\\displaystyle\\sum_{j=0}^{n-1}r^{j}\n",
|
||||||
|
" = \\tfrac{1}{n}\n",
|
||||||
|
" \\begin{cases}\n",
|
||||||
|
" \\displaystyle\\sum_{j=0}^{n-1}1 &: &i = k\\\\\n",
|
||||||
|
" \\frac{1 - r^{n}}{1 - r} &: &\\text{sonst}\\\\\n",
|
||||||
|
" \\end{cases}\n",
|
||||||
|
" = \\tfrac{1}{n}\n",
|
||||||
|
" \\begin{cases}\n",
|
||||||
|
" n &: &i = k\\\\\n",
|
||||||
|
" \\frac{1 - 1}{1 - r} &: &\\text{sonst}\\\\\n",
|
||||||
|
" \\end{cases}\n",
|
||||||
|
" = \\delta_{ik}.\n",
|
||||||
|
"$$\n",
|
||||||
|
"\n",
|
||||||
|
"Darum $\n",
|
||||||
|
" \\mathcal{F}_{n}\\,u_{n,k}\n",
|
||||||
|
" = \\left(\\begin{matrix}\n",
|
||||||
|
" 0\\\\\n",
|
||||||
|
" 0\\\\\n",
|
||||||
|
" \\vdots\\\\\n",
|
||||||
|
" 1\\\\\n",
|
||||||
|
" \\vdots\\\\\n",
|
||||||
|
" 0\\\\\n",
|
||||||
|
" \\end{matrix}\\right)\n",
|
||||||
|
" \\begin{matrix}\n",
|
||||||
|
" \\\\\n",
|
||||||
|
" \\\\\n",
|
||||||
|
" \\\\\n",
|
||||||
|
" \\leftarrow k\\\\\n",
|
||||||
|
" \\\\\n",
|
||||||
|
" \\\\\n",
|
||||||
|
" \\end{matrix}\n",
|
||||||
|
" = \\mathbf{e}_{k}\n",
|
||||||
|
"$.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"'''SEMINARAUFGABE 10.4 d)'''\n",
|
||||||
|
"n = 4;\n",
|
||||||
|
"t = linspace(0, n-1, n);\n",
|
||||||
|
"i, j = np.meshgrid(t, t);\n",
|
||||||
|
"theta = 2*pi * i * j / n;\n",
|
||||||
|
"\n",
|
||||||
|
"# definiere die Matrix F_n + die Vektoren u_nk\n",
|
||||||
|
"F_n = (1/sqrt(n)) * exp(-1j * theta);\n",
|
||||||
|
"u_n = [ (1/sqrt(n)) * exp(1j * theta[:, k]) for k in range(n) ];\n",
|
||||||
|
"\n",
|
||||||
|
"# Eingabe des Vektors:\n",
|
||||||
|
"F_mal_u = np.asarray([100, -3, 0, -3]); # = F_n @ u\n",
|
||||||
|
"c = [10,10,40,50];\n",
|
||||||
|
"# u = c[0]u_n[0] + c[1]u_n[1] + c[2]u_n[2] + c[3]u_n[3]\n",
|
||||||
|
"u_guess = sum([ c[k] * u_n[k] for k in range(n) ]);\n",
|
||||||
|
"\n",
|
||||||
|
"# Anzeige der Lösung:\n",
|
||||||
|
"display_signal(u_guess, 'Geschätztes Signal');\n",
|
||||||
|
"\n",
|
||||||
|
"# Verifikation von Lösung:\n",
|
||||||
|
"print(dedent(\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",
|
||||||
|
"));\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.10.8"
|
||||||
|
},
|
||||||
|
"vscode": {
|
||||||
|
"interpreter": {
|
||||||
|
"hash": "98590ff4fe04c8543246b2a01debd3de3c5ca9b666f43f1fa87d5110c692004c"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
pip>=22.3
|
pip>=22.3.1
|
||||||
wheel>=0.37.1
|
wheel>=0.37.1
|
||||||
|
|
||||||
# jupyter
|
# jupyter
|
||||||
@ -16,7 +16,7 @@ codetiming>=1.3.0
|
|||||||
|
|
||||||
# testing + dev
|
# testing + dev
|
||||||
coverage[toml]>=6.4
|
coverage[toml]>=6.4
|
||||||
pytest>=7.1.1
|
pytest>=7.2.0
|
||||||
pytest-asyncio>=0.18.3
|
pytest-asyncio>=0.18.3
|
||||||
pytest-cov>=3.0.0
|
pytest-cov>=3.0.0
|
||||||
pytest-lazy-fixture>=0.6.3
|
pytest-lazy-fixture>=0.6.3
|
||||||
@ -33,7 +33,6 @@ lorem>=0.1.1
|
|||||||
safetywrap>=1.5.0
|
safetywrap>=1.5.0
|
||||||
typing>=3.7.4.3
|
typing>=3.7.4.3
|
||||||
nptyping>=2.1.1
|
nptyping>=2.1.1
|
||||||
typing-extensions>=3.10.0.2
|
|
||||||
|
|
||||||
# config
|
# config
|
||||||
python-dotenv>=0.20.0
|
python-dotenv>=0.20.0
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
from src.maths.diagrams.sets import *;
|
from src.maths.diagrams.sets import *;
|
||||||
|
from src.maths.diagrams.signal import *;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# EXPORTS
|
# EXPORTS
|
||||||
@ -14,4 +15,5 @@ from src.maths.diagrams.sets import *;
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
'Function',
|
'Function',
|
||||||
'Functions',
|
'Functions',
|
||||||
|
'display_signal',
|
||||||
];
|
];
|
||||||
|
48
src/maths/diagrams/signal.py
Normal file
48
src/maths/diagrams/signal.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# IMPORTS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
from __future__ import annotations;
|
||||||
|
|
||||||
|
from src.thirdparty.code import *;
|
||||||
|
from src.thirdparty.types import *;
|
||||||
|
from src.thirdparty.maths import *;
|
||||||
|
from src.thirdparty.plots import *;
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# EXPORTS
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'display_signal',
|
||||||
|
];
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
# CONSTANTS / VARIABLES
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
def display_signal(
|
||||||
|
u: np.ndarray,
|
||||||
|
title: str,
|
||||||
|
max_labels = 10,
|
||||||
|
):
|
||||||
|
n = len(u);
|
||||||
|
if n <= max_labels:
|
||||||
|
x_axis = list(range(n));
|
||||||
|
else:
|
||||||
|
skip = int(n/max_labels);
|
||||||
|
x_axis = [ k*skip for k in range(max_labels) ];
|
||||||
|
|
||||||
|
fig, axs = mplot.subplots(1, 1, constrained_layout=True);
|
||||||
|
mplot.title(title);
|
||||||
|
mplot.xlabel('»Zeit« [=Indizes]');
|
||||||
|
mplot.ylabel('Werte [=Einträge]');
|
||||||
|
axs.stem(linspace(0,n-1,n), np.real(u), label='reeller Teil', linefmt='red');
|
||||||
|
axs.stem(linspace(0,n-1,n) + 0.05, np.imag(u), label='imaginärer Teil', linefmt='lime');
|
||||||
|
mplot.legend();
|
||||||
|
axs.set_xticks(x_axis, labels=[ str(index) for index in x_axis]);
|
||||||
|
mplot.show();
|
||||||
|
return;
|
16
src/thirdparty/maths.py
vendored
16
src/thirdparty/maths.py
vendored
@ -6,10 +6,17 @@
|
|||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
from fractions import Fraction;
|
from fractions import Fraction;
|
||||||
|
from numpy import cos;
|
||||||
|
from numpy import exp;
|
||||||
|
from numpy import linalg;
|
||||||
|
from numpy import linspace;
|
||||||
|
from numpy import pi;
|
||||||
|
from numpy import sin;
|
||||||
|
from numpy import sqrt;
|
||||||
|
from typing import TypeVar;
|
||||||
import math;
|
import math;
|
||||||
import numpy as np;
|
import numpy as np;
|
||||||
import random;
|
import random;
|
||||||
from typing import TypeVar;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# MODIFICATIONS
|
# MODIFICATIONS
|
||||||
@ -39,8 +46,15 @@ def sample(
|
|||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'Fraction',
|
'Fraction',
|
||||||
|
'cos',
|
||||||
|
'exp',
|
||||||
|
'linalg',
|
||||||
|
'linspace',
|
||||||
'math',
|
'math',
|
||||||
'np',
|
'np',
|
||||||
|
'pi',
|
||||||
'random',
|
'random',
|
||||||
'sample',
|
'sample',
|
||||||
|
'sin',
|
||||||
|
'sqrt',
|
||||||
];
|
];
|
||||||
|
4
src/thirdparty/render.py
vendored
4
src/thirdparty/render.py
vendored
@ -19,11 +19,11 @@ from qiskit.visualization import array_to_latex;
|
|||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
'Latex',
|
||||||
'array_to_latex',
|
'array_to_latex',
|
||||||
|
'display',
|
||||||
'display_latex',
|
'display_latex',
|
||||||
'display_markdown',
|
'display_markdown',
|
||||||
'display_png',
|
'display_png',
|
||||||
'display',
|
|
||||||
'Latex',
|
|
||||||
'widgets',
|
'widgets',
|
||||||
];
|
];
|
||||||
|
4
src/thirdparty/types.py
vendored
4
src/thirdparty/types.py
vendored
@ -38,8 +38,8 @@ from typing import Generic;
|
|||||||
from typing import Optional;
|
from typing import Optional;
|
||||||
from typing import Type;
|
from typing import Type;
|
||||||
from typing import TypeVar;
|
from typing import TypeVar;
|
||||||
from typing_extensions import Concatenate;
|
from typing import Concatenate;
|
||||||
from typing_extensions import ParamSpec;
|
from typing import ParamSpec;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# EXPORTS
|
# EXPORTS
|
||||||
|
Loading…
x
Reference in New Issue
Block a user