182 lines
4.5 KiB
Plaintext
182 lines
4.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"import matplotlib.pyplot as plt\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Übung\n",
|
|
"\n",
|
|
"Gegeben ist das folgende Array:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"n = np.array([\n",
|
|
" [0. , 0. , 0.5, 1. , 1. , 0. , 1. , 0. , 1. ],\n",
|
|
" [0. , 1. , 1.5, 1. , 0. , 1. , 1. , 0. , 0. ]\n",
|
|
"])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Die erste Zeile sind dabei x-Werte, die zweite Zeile y-Werte von Punkten. Plotten Sie die Punkte als Linienzug mit `plt.plot(...)`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Linienzug plotten\n",
|
|
"plt.figure()\n",
|
|
"plt.plot(n[0], n[1])\n",
|
|
"plt.show()\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Als nächstes sollen die Punkte rotiert werden. Eine Matrix, die eine Rotation um einen Winkel $\\theta$ durchführt, lautet wie folgt:\n",
|
|
"\n",
|
|
"\\begin{equation}\n",
|
|
"m_1 =\n",
|
|
"\\begin{bmatrix}\n",
|
|
"\\cos \\theta & -\\sin \\theta \\\\ \n",
|
|
"\\sin \\theta & \\cos \\theta \\\\\n",
|
|
"\\end{bmatrix}\n",
|
|
"\\end{equation}\n",
|
|
"\n",
|
|
"Implementieren Sie eine Funktion, die für einen gegeben Winkel diese Matrix als `ndarray` erzeugt:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# angle soll in Grad angegeben werden\n",
|
|
"def rotation_matrix(angle):\n",
|
|
" theta = np.deg2rad(angle)\n",
|
|
" c = np.cos(theta)\n",
|
|
" s = np.sin(theta)\n",
|
|
" return np.array([[c, -s], [s, c]])\n",
|
|
"\n",
|
|
"# mögliche Tests\n",
|
|
"assert np.allclose(rotation_matrix(90).dot(np.array([1,0])), np.array([0,1]))\n",
|
|
"assert np.allclose(rotation_matrix(-90).dot(np.array([1,0])), np.array([0,-1]))\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Zum Rotieren aller Punkte muss jeder Punkt mit der Matrix `m` multipliziert werden. Rotieren Sie alle Punkte um 60 Grad und plotten Sie das Ergebnis erneut (Bonus: Versuchen Sie ohne `for`-Schleife auszukommen):"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# n_rot soll die rotierten Punkte enthalten, n_rot.shape == n.shape\n",
|
|
"n_rot = rotation_matrix(60).dot(n)\n",
|
|
"\n",
|
|
"plt.figure()\n",
|
|
"plt.plot(n_rot[0], n_rot[1])\n",
|
|
"plt.show()\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Als nächstes sollen die Punkte bezüglich des Ursprungs skaliert werden werden. Eine Matrix, die eine Skalierung in x-Richtung mit Faktor $s_x$ und in y-Richtung mit Faktor $s_y$ durchführt, lautet:\n",
|
|
"\n",
|
|
"\\begin{equation}\n",
|
|
"m_2 =\n",
|
|
"\\begin{bmatrix}\n",
|
|
"s_x & 0 \\\\ \n",
|
|
"0 & s_y \\\\\n",
|
|
"\\end{bmatrix}\n",
|
|
"\\end{equation}\n",
|
|
"\n",
|
|
"Implementieren Sie eine Funktion, die für einen gegeben Winkel diese Matrix als `ndarray` erzeugt:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def scale_matrix(sx, sy):\n",
|
|
" return np.array([[sx, 0], [0, sy]])\n",
|
|
"\n",
|
|
"assert np.allclose(scale_matrix(2, 3).dot(np.array([1,1])), np.array([2,3]))\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Skalieren Sie die gegebenen Punkte um beliebige Faktoren und plotten Sie die ursprünglichen und skalierten Punkte im selben Diagramm:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"scaled = scale_matrix(2, 0.5).dot(n)\n",
|
|
"plt.figure()\n",
|
|
"plt.plot(n[0], n[1], label=\"original\")\n",
|
|
"plt.plot(scaled[0], scaled[1], label=\"scaled\")\n",
|
|
"plt.legend()\n",
|
|
"plt.show()\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"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.11.0"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|