{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d663a137",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    },
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Modularisierung\" data-toc-modified-id=\"Modularisierung-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Modularisierung</a></span><ul class=\"toc-item\"><li><span><a href=\"#Einbinden-von-Modulen-und-Paketen\" data-toc-modified-id=\"Einbinden-von-Modulen-und-Paketen-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Einbinden von Modulen und Paketen</a></span></li><li><span><a href=\"#Aliase\" data-toc-modified-id=\"Aliase-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>Aliase</a></span></li></ul></li><li><span><a href=\"#Einführung-in-NumPy\" data-toc-modified-id=\"Einführung-in-NumPy-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Einführung in NumPy</a></span><ul class=\"toc-item\"><li><span><a href=\"#Vektoren-und-Matrizen-in-NumPy-erzeugen-(Teil-1)\" data-toc-modified-id=\"Vektoren-und-Matrizen-in-NumPy-erzeugen-(Teil-1)-2.1\"><span class=\"toc-item-num\">2.1&nbsp;&nbsp;</span>Vektoren und Matrizen in NumPy erzeugen (Teil 1)</a></span></li><li><span><a href=\"#Attribute-(eines-NumPy-Arrays)\" data-toc-modified-id=\"Attribute-(eines-NumPy-Arrays)-2.2\"><span class=\"toc-item-num\">2.2&nbsp;&nbsp;</span>Attribute (eines NumPy Arrays)</a></span></li><li><span><a href=\"#Mathematische-Operationen-für-NumPy-Arrays\" data-toc-modified-id=\"Mathematische-Operationen-für-NumPy-Arrays-2.3\"><span class=\"toc-item-num\">2.3&nbsp;&nbsp;</span>Mathematische Operationen für NumPy Arrays</a></span><ul class=\"toc-item\"><li><span><a href=\"#Addition,-Subtraktion,-Multiplikation,-Division,-modulo,-potenzieren\" data-toc-modified-id=\"Addition,-Subtraktion,-Multiplikation,-Division,-modulo,-potenzieren-2.3.1\"><span class=\"toc-item-num\">2.3.1&nbsp;&nbsp;</span>Addition, Subtraktion, Multiplikation, Division, modulo, potenzieren</a></span></li><li><span><a href=\"#Der-@-Operator\" data-toc-modified-id=\"Der-@-Operator-2.3.2\"><span class=\"toc-item-num\">2.3.2&nbsp;&nbsp;</span>Der @ Operator</a></span></li></ul></li><li><span><a href=\"#Ändern-der-Form-eines-ndarrays-reshape()\" data-toc-modified-id=\"Ändern-der-Form-eines-ndarrays-reshape()-2.4\"><span class=\"toc-item-num\">2.4&nbsp;&nbsp;</span>Ändern der Form eines ndarrays <code>reshape()<code></code></code></a></span></li><li><span><a href=\"#Zusammensetzen-mit-concatenate,-vstack-und-hstack\" data-toc-modified-id=\"Zusammensetzen-mit-concatenate,-vstack-und-hstack-2.5\"><span class=\"toc-item-num\">2.5&nbsp;&nbsp;</span>Zusammensetzen mit <code>concatenate</code>, <code>vstack</code> und <code>hstack</code></a></span></li><li><span><a href=\"#Auf-Einträge-zugreifen--und--Einträge-ändern\" data-toc-modified-id=\"Auf-Einträge-zugreifen--und--Einträge-ändern-2.6\"><span class=\"toc-item-num\">2.6&nbsp;&nbsp;</span>Auf Einträge zugreifen  und  Einträge ändern</a></span></li><li><span><a href=\"#Kronecker-Produkt\" data-toc-modified-id=\"Kronecker-Produkt-2.7\"><span class=\"toc-item-num\">2.7&nbsp;&nbsp;</span>Kronecker Produkt</a></span></li><li><span><a href=\"#Definition-spezieller-Matrizen-ones,-zeros,-eye,-arange\" data-toc-modified-id=\"Definition-spezieller-Matrizen-ones,-zeros,-eye,-arange-2.8\"><span class=\"toc-item-num\">2.8&nbsp;&nbsp;</span>Definition spezieller Matrizen <code>ones</code>, <code>zeros</code>, <code>eye</code>, <code>arange</code></a></span></li><li><span><a href=\"#Diagional--und-Dreiecksmatrizen\" data-toc-modified-id=\"Diagional--und-Dreiecksmatrizen-2.9\"><span class=\"toc-item-num\">2.9&nbsp;&nbsp;</span>Diagional- und Dreiecksmatrizen</a></span></li><li><span><a href=\"#max-und-min-für-Arrays\" data-toc-modified-id=\"max-und-min-für-Arrays-2.10\"><span class=\"toc-item-num\">2.10&nbsp;&nbsp;</span><code>max</code> und <code>min</code> für Arrays</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3e63b3e",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Modularisierung"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d34fdc28",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Module (modules) sind Python-Dateien mit der Endung .py, die Funktionen und Variablen (Datencontainer) für bestimmte Aufgaben bereitstellen. Mehrere Module werden zu Paketen (packages) zusammengefasst, um den Funktionsumfang von Python zu erweitern. Oft werde auch Pakete und Unterpakete einfach Modul genannt."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7805025f",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Erstelle Datei compla2026.py im selben Ordner, in dem auch lektion4.ipynb liegt."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "533c03c1",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Einbinden von Modulen und Paketen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bf87b504",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import compla2026"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f401da5b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'PROJEKT_NAME' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mPROJEKT_NAME\u001b[49m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'PROJEKT_NAME' is not defined"
     ]
    }
   ],
   "source": [
    "PROJEKT_NAME"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fc8f4a7e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Comp LA 2026'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compla2026.PROJEKT_NAME"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce062bb2",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "compla2026.begruessung('Julia')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ac43de7",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "from compla2026 import begruessung"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "117e806d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "begruessung('Max')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c4a38fb",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Aliase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ec4f770",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import compla2026 as cla"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80f93ca5",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "cla.begruessung('Eve')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0965e9f5",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "from compla2026 import begruessung as hello"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7086e79",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "hello('Tom')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c266e128",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "hallo = begruessung # das geht nur, weil wir vorher \"from compla26 import begruessung\" ausgefuehrt haben."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92e3b38e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "hallo('Li')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eea5bc5f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Einführung in NumPy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "114ae018",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Listen sind als Vektoren nicht so gut zu gebrauchen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f98d4068",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "[1, 1.] + [2, 3.]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28700c10-5b60-4305-9a00-2ce07bb33f07",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "NumPy ist ein Paket das Vektoren, Matrizen, Tensoren, mathematische Funktionen, einen Zufallszahlengenerator, grundlegende Verfahren der linearen Algebra und vieles mehr zur Verfügung stellt."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "558844e0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Hier ist ein winziger Ausschnitt aus der Ordnerstruktur von numpy\n",
    "```text\n",
    "numpy/                      # Hauptordner = Paket\n",
    "│\n",
    "├── __init__.py             # Sagt Python: \"Ich bin ein Paket!\"\n",
    "│\n",
    "├── linalg/                 # Unterordner = Sub-Paket!\n",
    "│   ├── __init__.py         # Sagt Python: \"Ich bin ein Sub-Paket!\"\n",
    "│   └── _linalg.py          # Einzelne Datei = Das eigentliche Modul\n",
    "│\n",
    "└── random/                 # Unterordner = Sub-Paket!\n",
    "    ├── __init__.py         # Sagt Python: \"Ich bin ein Sub-Paket!\"\n",
    "    ├── generator.py        # Einzelne Datei = Modul\n",
    "    └── mtrand.pyx          # Einzelne Datei = Modul\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8167a013-9cbe-44a9-bcae-6c95e4a14c6a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0b2bb24",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#dir(np)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0ad3fe5-9a52-4756-8566-882e6fca3c37",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "v = np.array((1, 2))\n",
    "v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4166bf4a-6409-45c3-8a58-43696d1cd1fb",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e8ab5f4-66d1-413e-8cd0-9a0ad1085bff",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.sin(np.pi)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "489c4d40-46d5-486e-9ede-4de8f9671d1b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Vektoren und Matrizen in NumPy erzeugen (Teil 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab624fed-7d94-4fb9-a2d5-dc6336fd129a",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u = np.array([1, 2, 3])      # 1D Array (Vektor)\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d541bf43-fa27-4979-b281-2f6251b2e25d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "v = np.array([4, 5])\n",
    "v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d2346a8-1caa-4f68-ae1f-48c5ea89f601",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A = np.array([[3, 2, 1], [3, 5, 4], [8, 6, 7]])  # 2D Array (Matrix)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3347ff7a-f285-4df2-ac6f-9e6a6e04d5cc",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "type(u), type(v), type(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a6a1718-db35-49a2-a901-91a7aaa1692d",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40911981",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f1a10e0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ceb77c2a-801f-4d67-b1cf-d98458e7d847",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "w = np.array(5)    # eine skalare Größe (0D array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a66ad2a1-683c-493b-b771-f33c7d57b736",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96d35261-f651-4339-a8ca-44c9f75a781c",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "3*u  # jedes Element wird mit 3 multipliziert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c896982-e63c-42c9-a4d3-6487da8fea20",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "e = np.array([1, 1, 1])\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fad6d34-2163-4b6b-b98f-74f93f6bba59",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "3*u - e # Skalarmultiplikation und Addidtion/Substraktion von Vektoren/Arrays"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43b2851b-6983-4f4e-a591-d8bae30a012c",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Attribute (eines NumPy Arrays)\n",
    "\n",
    "Mit \n",
    "```python\n",
    "   obj.name\n",
    "```\n",
    "greift man auf das Attribut mit <code>name</code> des Objekts <code>obj</code> zu.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20652a9f",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a = 1.23 - 98.7j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c759ba75",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a.real"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a915140e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a.imag"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be529017",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "<code>ndim</code> Anzahl der Dimensionen eines NumPy arrays anzeigen: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9624d812-fdd3-4557-8012-eef944a28b15",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "w.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dfef663-1332-46e6-ad35-5eaaa7f1b1e1",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A.ndim"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2a74d7a-f964-4c32-a713-587421eba392",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Es gibt <code>ndim</code> auch als numpy Funktion:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc0f5d35-45e7-4068-a91d-7a032ccacd11",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.ndim(u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3693d73-6aa2-47dc-a066-e533ed5ab30e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.ndim(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cd0930d-7aed-4bb4-8113-393fe91df967",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Die Elemente des <code>shape</code> Tupels geben die Größe der entsprechenden Array Dimensionen an.\n",
    "\n",
    "Ebenso wie <code>ndim</code> gibt es auch <code>shape</code> sowohl als Attribut als auch als Funktion.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81471716-3892-4bb6-89b4-fd85c0a7f5dd",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35fb83b9-e1dd-47be-8be6-c329b3169766",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19c4ef61-1437-42e2-8a71-35aba815a4fd",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "w.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7f7f090-fa01-4b28-9515-76fadf611c22",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.shape(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1389cad3-8411-43d8-9ee9-089ec0263f25",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.shape(u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec95ecec",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "len(u)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6515d1e9-9da1-4efb-a075-83efab744158",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "<code>size</code> gibt die Anzahl der Elemente des Arrays an:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6dc7981e-73c5-4686-84f7-bb428a706e26",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3204f96c-7f12-4a05-9564-5ecfe561dbf4",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.size(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70f2cf14-8397-4c95-810f-43a5b00ada45",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e694ce92-0d8d-45fb-90c0-ddfc06b5e0e9",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.size(u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d20209ac",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "len(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70f19a31-dd24-403e-b7b2-495949c7a3f3",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Mathematische Operationen für NumPy Arrays\n",
    "### Addition, Subtraktion, Multiplikation, Division, modulo, potenzieren\n",
    "\n",
    "*, / , % und &ast;&ast; wirken eintragsweise, wie  + und - "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa1e2374-fe7d-4b2d-8b73-907d7b8f6089",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5081d7eb-ee46-4718-9f34-a87667f1c93d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a = np.array(range(4,7))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5713895e-664e-452f-aaff-426811accabf",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u * a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "452b7adc-602d-4661-b43d-faa88f0ee17c",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u**u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd641192-115c-44fa-bb2f-be7650d7d924",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6164991-18d0-4d5f-ba42-8ac38aaa7b98",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afed3922",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbe2261b-f970-4f0b-90ac-d7aa3d38b0ac",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A/A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9742fdf",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Transponierte Matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d96aea0d-e5e7-4ef2-b479-0945ae028584",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A.T "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aad2b13e-f5b7-4333-8a5c-d1f11f4985c7",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Der @ Operator \n",
    "\n",
    "<code> @ </code> definiert auf np.arrays eine allgemeine Matrixmultplikation.\n",
    " \n",
    "Skalarprodukt von Vektoren"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e0ba527-f226-40ce-b4ec-2db76c3de598",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u @ u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e80a9ac-10ad-49d8-acab-13886405a942",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.dot(u, u)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdb36953",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Matrix-Vektor Produkt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c3ce102-39ce-43aa-b370-7f36e77f57d0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A @ u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a75f6832-2567-400f-b547-3aee1c1b8f7c",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u @ A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fed7ea1",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Matrix-Matrix Produkt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a38c079-4ad5-4e52-8669-bb05db6c7e98",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A @ A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f5aae74-8a35-4198-bab0-ae01ae98ff81",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.dot(A, A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6022148a-92a4-45d8-a91d-0ce6186b4da3",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Ändern der Form eines ndarrays <code>reshape()<code>\n",
    "\n",
    "Objekte haben neben Attributen auch eigene Funktionen, sogenannte Methoden. Mit\n",
    "```python\n",
    "        obj.name()\n",
    "```\n",
    "wird die Methode <code>name</code> des Objekts <code>obj</code> aufgerufen. Wichtig sind hier die Klammern.\n",
    "\n",
    "Mit <code>.reshape()</code> oder <code>np.newaxis</code> kann man aus einem _flachen_ Vektor einen Zeilen- oder Spaltenvektor machen.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06521355-aa92-4594-af1e-9d83f65c6605",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u_zeile = u.reshape((1, -1)) # Zeilenvektor\n",
    "u_zeile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e7b7d88",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u_zeile = u.reshape(1,-1)\n",
    "u_zeile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96f80218",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "u_zeile.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "601d6fb8",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0674dd8d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u_zeile.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a90566f",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1af0140-e26e-4871-8728-3dab5d30c7f6",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u_zeile_ = u[:, np.newaxis]\n",
    "u_zeile"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d549253c-45bd-4b39-844f-8e14cdb4a393",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Zusammensetzen mit <code>concatenate</code>, <code>vstack</code> und <code>hstack</code>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57953400-2097-4c1f-a400-1442c48d91cb",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# Arrays zusammenkleben A soll um eine Zeile mit e erweitert werden\n",
    "np.concatenate((A, e), axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8273c082",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.concatenate((A, e.reshape(1, -1)), axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6552fa7d-4097-4dfe-a6b3-f1935cf694a9",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.vstack((A, e)) # Arrays vertikal zusammenkleben mit np.vstack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17e5a58a-d2b6-45bd-99c2-a748cdedaa59",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "np.concatenate((A, e.reshape(-1, 1)), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8bddd39-1fdf-4fd1-b131-4f835b7dda7a",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.hstack((A, e.reshape(-1, 1))) # Arrays horizontal zusammenkleben mit np.hstack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8a7ea0b-935a-42f0-980f-78a368a8a020",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.hstack((A, A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "242bbd0e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.concatenate((A, A), axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed2446b9-99ed-4e8e-b202-ed5c872df966",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Auf Einträge zugreifen  und  Einträge ändern "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3126c3be-de28-446a-9e0c-ecdb08bd843f",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "C = np.concatenate((A, 10*A), axis=0)\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93a8bcbe-fa13-4e5d-8ef3-fba40afb754e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "C[0, 0] #Element links oben in C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e56e193-cf76-41c2-8205-df68c248a3c8",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "C[-1, -2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "023c0cfc-4b9d-4a61-9a7b-b789cad6697d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "C[-1, -1] = 101 # Element rechts unten\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ee5e980-b401-4799-b28f-75ed41f4dc03",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "C[2, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7223e07f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75cbcbfd-df28-4839-89e1-828138edce33",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "C[1::2, [1]] # Spaltenvektor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec204d7c-5c2e-4353-9f7f-21763f61048a",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "C[1::2, 1]  # flacher Vektor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8e42d82-07b3-48b6-95df-3aa6e04f80e6",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "C[2:4, 1:3]  # 2d Array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "036f24cf-5c7a-4d58-a09d-1cb3f61fd66d",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "C[2, :] = -1\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5309e47-5952-4387-84d8-fdf3b8349920",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "u[0] = 100\n",
    "u_zeile  # u_zeile ist nur eine Ansicht (view) von u und zeigt auf dieselben Einträge im Speicher"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4513f760-a6f0-44fd-bf13-629301055653",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u_spalte = u[:, np.newaxis]\n",
    "u_spalte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b0ba57a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "u_spalte @ u_zeile  # äußeres Produkt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c925c8b-0526-4329-b406-23adaaa5d8cd",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    " u_zeile @ u_spalte  # Skalarprodukt (inneres Produkt) Achtung, das ist ein 1 x 1 array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3177d41b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u_zeile.flatten() @ u_spalte.flatten() # Skalarprodukt (inneres Produkt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c6f81ed-8bcb-4175-96ee-bb16ea143a25",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u_spalte @ A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eb53f06-93c4-4325-b0fd-d9ec410bc79e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "u_zeile @ A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cff71997-640b-4e41-ab25-89e3f4ac267b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Kronecker Produkt\n",
    "Ein Beispiel statt einer Definition\n",
    "$$\\scriptsize \\begin{pmatrix} 1 & 2 \\\\ 3 & 4 \\\\ 5 & 6 \\end{pmatrix}\\otimes \\begin{pmatrix} 7 & 8 \\\\ 9 & 0 \\end{pmatrix}\n",
    "=\\begin{pmatrix} \n",
    "1 \\cdot \\begin{pmatrix} 7 & 8 \\\\ 9 & 0 \\end{pmatrix} & 2 \\cdot \\begin{pmatrix} 7 & 8 \\\\ 9 & 0 \\end{pmatrix} \\\\\\\\\n",
    "3 \\cdot \\begin{pmatrix} 7 & 8 \\\\ 9 & 0 \\end{pmatrix} & 4 \\cdot \\begin{pmatrix} 7 & 8 \\\\ 9 & 0 \\end{pmatrix} \\\\\\\\\n",
    "5 \\cdot \\begin{pmatrix} 7 & 8 \\\\ 9 & 0 \\end{pmatrix} & 6 \\cdot \\begin{pmatrix} 7 & 8 \\\\ 9 & 0 \\end{pmatrix}\n",
    "\\end{pmatrix}\n",
    "=\\begin{pmatrix} 7 & 8 & \\!\\!\\! & 14 & 16 \\\\ 9 & 0 & \\!\\!\\! & 18 & 0 \\\\[0.6em] 21 & 24 & \\!\\!\\! & 28 & 32 \\\\ 27 & 0 & \\!\\!\\! & 36 & 0 \\\\[0.6em] 35 & 40 & \\!\\!\\! & 42 & 48 \\\\ 45 & 0 & \\!\\!\\! & 54 & 0 \\end{pmatrix} $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92ac9cc2-4824-4763-ab5e-1b9f91ee1cdf",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.kron([1, 10, 100], [5, 6, 7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48a836c2",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "D2 = np.array([[1, 0], [0, 2]])\n",
    "E2 = np.array([[1, 3], [5, 2]])\n",
    "D2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3218ae79",
   "metadata": {},
   "outputs": [],
   "source": [
    "E2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b187bbe-49a3-435f-be00-36f7eda976e0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.kron(D2, E2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebfd8ade",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.kron(E2, D2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a537a46c-d84c-490a-a25a-6faaaf1ba0a6",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Definition spezieller Matrizen <code>ones</code>, <code>zeros</code>, <code>eye</code>, <code>arange</code>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b0c0943-0d74-4aa7-a86b-e5666fe1577b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "E = np.ones((3, 3))   # Matrix aus Einsen\n",
    "Z = np.zeros((3, 3))  # Nullmatrix\n",
    "I = np.eye(3)         # Identität\n",
    "A = np.arange(1,17)   # flacher \"Vektor\" [1,..16]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f76b47ab",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "E "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72724eaa",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "Z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e8fca3b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37b6328f-ecc7-4dbb-bf34-3b16fa169122",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "237cbe1d-8dbb-4986-8f3c-c5572137de18",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "np.eye(4, 6, k=1)  # erste obere Nebendiagonale"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbafa301-98cd-4528-ac88-ead8867dff05",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.eye(4, 6, k=-1)  # erste untere Nebendiagonale"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a032d962-3177-4360-bb32-fb1e17385482",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.eye(4, 6)   # Default Wert ist k=0, Hauptdiagonale"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6d50709",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Diagional- und Dreiecksmatrizen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8cf520f1-e2c3-48ef-922c-bcccf04b5d15",
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A = A.reshape(4, 4)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02badc85-bbef-49d0-bdc7-1868d5ae3ff7",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.diagonal(A) # Hauptdiagonale von A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ddcb873-7746-4a79-9c3c-e1cc68fa96ff",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.tril(A) # Untere Dreiecksmatrix (lower triangular)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a463acd-8b58-4dd7-9eec-d43bf6d67e11",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.triu(A) # Obere Dreiecksmatrix (upper triangular)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b8920c3",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f4dc1e4-42db-4f6a-9eeb-86559c01afd0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "NU = np.diagonal(A, offset=1)  # erste obere Nebendiagonale\n",
    "NU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02b72744-7ec1-413b-95a5-0945c226f0cf",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "NL = np.diagonal(A, offset=-1) # erste untere Nebendiagonale\n",
    "NL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb6c1e54-646b-4def-9733-65aba329fc84",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "B = np.diagflat(v.flatten()) # np.diagflat erzeugt Diagonalmatrix aus flachen Vektor\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fb9df95-7edb-4138-a5a1-499e3c30bc96",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# Benutze np.diag zur Definition von Matrizen\n",
    "x = np.array([1, 2, 3])\n",
    "C = np.diagflat(x, k=-1) + np.diagflat(2*x, k=1) #k=-1 erste untere Nebendiagonale, k=1 erste obere Nebendiagonle\n",
    "C"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "935a5d29-463f-408b-9691-6c62243a1ad5",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "##  <code>max</code> und <code>min</code> für Arrays\n",
    "\n",
    "<code>np.max(A)</code> oder <code>A.max()</code> liefert das maximale Matrixelement:\n",
    "(<code>min</code> geht analog)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1e97dd5-acbe-47fa-9008-b75538c2c46f",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6709bb2-6dc0-46da-8b45-0844e5fef45b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.max(A), A.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60bbe8f9-0620-404f-8d2d-baff99de932d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A.max(1) # Man kann auch das Maximum entlang der ersten Achse nehmen (zeilenweises Maximum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76a8e6aa-f39f-49c8-ba3e-b5699df07043",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A.max(0) # Man kann auch das Maximum entlang der nullten Achse (spaltenweises Maximum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f09ce3b8",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.max(A, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e89836a-e7da-41a1-aa88-ac1a1a6504a3",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.max(A, axis=1)  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c26c6891-a663-4727-995f-9dd7cb8dff32",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "<code>.flatten()</code> A in ein 1-dim. Array umwandeln"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5df19bbc-a97e-4df6-9af1-85d4851fbea8",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A.flatten()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4eba48ad-1d09-42e2-b47e-0ff594989925",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "__np.argmin(A)__ bzw. __np.argmax(A)__ liefert den Index, bezüglich \"flattened array\", des kleinsten / größten Matrixelements.\n",
    "\n",
    "Falls das kleinste bzw. größte Element mehrfach vorkommt, erhält man den Index des ersten Elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abc87cc2-4513-474f-bcba-1dfe4d72a513",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A.argmax(), A.flatten()[np.argmax(A)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f8cc8c4-c5c0-4c06-b24f-0c5e22619388",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "B = np.array([3*i-4 for i in range(16)]).reshape(4, 4)\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5d6b80d-f8a9-4921-8c3f-d736dbe91dd6",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.maximum(A, B) # elementweises maximum"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "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.12.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "203.85px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
