{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d24a20c1",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    },
    "toc": true
   },
   "source": [
    "<h1>Einführung in NumpPy Teil 2<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Broadcasting\" data-toc-modified-id=\"Broadcasting-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Broadcasting</a></span></li><li><span><a href=\"#Beispiel-zur-Verwendung-von-Index-Arrays.\" data-toc-modified-id=\"Beispiel-zur-Verwendung-von-Index-Arrays.-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Beispiel zur Verwendung von Index-Arrays.</a></span></li><li><span><a href=\"#Verwende-boolsche-Ausdrücke-bei-der-Definition-von-Matrizen\" data-toc-modified-id=\"Verwende-boolsche-Ausdrücke-bei-der-Definition-von-Matrizen-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>Verwende boolsche Ausdrücke bei der Definition von Matrizen</a></span></li><li><span><a href=\"#Achtung\" data-toc-modified-id=\"Achtung-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>Achtung</a></span></li><li><span><a href=\"#Universal-functions-(ufunc)\" data-toc-modified-id=\"Universal-functions-(ufunc)-5\"><span class=\"toc-item-num\">5&nbsp;&nbsp;</span>Universal functions (ufunc)</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81f8bedb",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Einführung in NumPy\n",
    "## Broadcasting\n",
    "+, -, * , %, / &ast;&ast; wirken eintragsweise, __wobei die _singleton_ Dimension expandiert wird__.\n",
    "\n",
    "_singleton_: fehlende Dimenson oder Dimension der Länge 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60d9b6bf",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "685f668a",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "A = np.array([[1, 2], [3, 4]])\n",
    "v = np.array([-5, 7])\n",
    "display(A, v) # das etwas übersichtlicher"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c4fdf2b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04144cf1",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# Das erste Element von v wird zu jeder zu jedem Element der ersten Spalte addiert\n",
    "A + v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7754769b",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#' Das erste Element von v wird ...\n",
    "A * v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79e8efcc",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "A**v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ad1883e",
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb512ec2",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "# zu jedem Eintrag wird 1 addiert\n",
    "A + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "013e2f2e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# jeder Eintrag wird durch 3 dividiert\n",
    "A / 3 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31756dd0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "numpy macht hier einen sogenannten 'upcast', versucht also ganze Zahlen in Gleitkommazahlen (float) umzuwandeln und gibt als Ergebnis ein Array mit floats zurück."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30cd9c13",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# 3 wird durch jeden Eintrag dividiert\n",
    "3 / A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "229b28a4",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "__Broadcastingregel__\n",
    "(aus https://numpy.org/doc/stable/user/basics.broadcasting.html)\n",
    "\n",
    "When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing (i.e. rightmost) dimensions and works its way left. Two dimensions are compatible when\n",
    "\n",
    "1. they are equal, or\n",
    "\n",
    "2. one of them is 1\n",
    "\n",
    "If these conditions are not met, a ValueError: operands could not be broadcast together exception is thrown, indicating that the arrays have incompatible shapes. The size of the resulting array is the size that is not 1 along each axis of the inputs.\n",
    "\n",
    "Arrays do not need to have the same number of dimensions. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "040b2b7c",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "DreiArray = np.array(np.arange(3 * 4 * 5)).reshape(3, 4, 5)\n",
    "DreiArray.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "531a2a10",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "ZweiArray = np.array(np.arange(3 * 4)).reshape(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fde5083",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "DreiArray + ZweiArray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c100e5b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "DreiArray_neu = np.array(np.arange(3 * 4 * 5)).reshape(5, 3, 4)\n",
    "DreiArray_neu.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91d26194",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "EinsArray = np.array(np.arange(5)).reshape(5, 1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a71988a2",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "#DreiArray_neu + ZweiArray + EinsArray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d39bb8f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "EinsArray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cc4ceae",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.squeeze(EinsArray)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31ba68b9",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Beispiel zur Verwendung von Index-Arrays.\n",
    "Erzeuge einen Vektor, der die Gegendiagonale einer Matrix enthält"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a91f4bc",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "A = np.arange(16).reshape(4, 4)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1e16459",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "i = np.arange(4)\n",
    "j = i[::-1] # view von i in umgekehrter Reihenfolge\n",
    "j, i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ecadf6c",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "b = A[i, j]\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2799eff8",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Verwende boolsche Ausdrücke bei der Definition von Matrizen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea74f43d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# Bsp: Setze alle negativen Matrixeinträge auf Null\n",
    "A \n",
    "B = A - 2*A.T \n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5361bdf",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "B[B < 0] = 0\n",
    "B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2febb54",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    " elementweises logisches \"oder\" und \"und\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a88371e8",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "B = A - A.T\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ae5ed28",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# Welche Elemente von B sind kleiner als -5 ODER größer als 6?\n",
    "index_array = np.logical_or(B < -5, B > 6)\n",
    "index_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e606ab45",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "# Welche Elemente von B sind kleiner als 4 UND größer als 0?\n",
    "display(B, np.logical_and(B < 4, B > 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc340578",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "B[index_array] = 100 # index_array = np.logical_or(B < -5, B > 6)\n",
    "B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d23826a5",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Achtung\n",
    "\n",
    "NumPy macht kein Ducktyping.\n",
    "\n",
    "... duck typing is an application of the duck test—\"If it walks like a duck and it quacks like a duck, then it must be a duck\" https://en.wikipedia.org/wiki/Duck_typing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2f1af41",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a = np.array([1, -2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c13b9fd7",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e8e507a",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a[0] = 1. + 1j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa3c50f0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "ac = np.array([1, -2.25], dtype='complex')\n",
    "ac"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0ff26a1",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "ac[0] = 10. + 10j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6a80cd1",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "a + ac"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b656fe8b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "np.sqrt(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50b56d78",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.sqrt(a.astype('complex'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a02e996",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Die NumPy-Funktionen in emath (power, exp, log, und inverse trigonometrische Funktionen) passen den Ausgabedatentyp an, wenn der Ausgabedatentyp vom Eingabedatentyp abweicht."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af020523",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "np.emath.sqrt(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22fb94f5",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "?np.emath"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24aea557",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Universal functions (ufunc)\n",
    "\n",
    "Die universellen Funktionen aus NumPy (sin, cos, tan, arcsin, arccos, sinh, cosh, arcsinh, ... exp, log, log2, log10, sqrt, ...) wirken elementweise, wenn sie auf NumPy Arrays angewandt werden. https://numpy.org/doc/stable/reference/ufuncs.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28f70f88",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "x = np.linspace(0, 5 * np.pi, 10)  # 10 äquidistante Zahlen zwischen 0 und 5 pi\n",
    "display(x, np.sin(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5efaba2c",
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "?np.expm1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3fdfbead",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "x = np.linspace(0, 5* np.pi, 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55f4a809",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "%%timeit\n",
    "y = np.sin(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "414fe93a",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "%%timeit\n",
    "y = np.empty_like(x)\n",
    "for k, xx in enumerate(x):\n",
    "    y[k] = np.sin(xx)"
   ]
  }
 ],
 "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": false,
   "skip_h1_title": true,
   "title_cell": "Einführung in NumpPy Teil 2",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "576px"
   },
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
