{ "cells": [ { "cell_type": "markdown", "id": "fd4dff47", "metadata": {}, "source": [ "# Universelle Funktionen (`ufunc`)\n", "\n", "Eine universelle Funktion, oder `ufunc`, ist eine Funktion, die elementweise Operationen auf Daten in `ndarrays` durchführt. Man kann sie sich als schnelle vektorisierte Wrapper für einfache Funktionen vorstellen, die einen oder mehrere skalare Werte annehmen und ein oder mehrere skalare Ergebnisse erzeugen.\n", "\n", "Viele `ufuncs` sind einfache elementweise Transformationen, wie [sqrt](https://numpy.org/doc/stable/reference/generated/numpy.sqrt.html) oder [exp](https://numpy.org/doc/stable/reference/generated/numpy.exp.html):" ] }, { "cell_type": "code", "execution_count": 1, "id": "f559f67e", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.107739Z", "iopub.status.busy": "2026-05-21T16:51:21.107473Z", "iopub.status.idle": "2026-05-21T16:51:21.139867Z", "shell.execute_reply": "2026-05-21T16:51:21.139511Z", "shell.execute_reply.started": "2026-05-21T16:51:21.107719Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "\n", "data = np.arange(10)" ] }, { "cell_type": "code", "execution_count": 2, "id": "26e18ead", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.140405Z", "iopub.status.busy": "2026-05-21T16:51:21.140285Z", "iopub.status.idle": "2026-05-21T16:51:21.143616Z", "shell.execute_reply": "2026-05-21T16:51:21.143292Z", "shell.execute_reply.started": "2026-05-21T16:51:21.140396Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 3, "id": "0a3b82aa", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.143985Z", "iopub.status.busy": "2026-05-21T16:51:21.143912Z", "iopub.status.idle": "2026-05-21T16:51:21.146328Z", "shell.execute_reply": "2026-05-21T16:51:21.145991Z", "shell.execute_reply.started": "2026-05-21T16:51:21.143978Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0. , 1. , 1.41421356, 1.73205081, 2. ,\n", " 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sqrt(data)" ] }, { "cell_type": "code", "execution_count": 4, "id": "8da698e7", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.146850Z", "iopub.status.busy": "2026-05-21T16:51:21.146758Z", "iopub.status.idle": "2026-05-21T16:51:21.149169Z", "shell.execute_reply": "2026-05-21T16:51:21.148945Z", "shell.execute_reply.started": "2026-05-21T16:51:21.146842Z" } }, "outputs": [ { "data": { "text/plain": [ "array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01,\n", " 5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03,\n", " 2.98095799e+03, 8.10308393e+03])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.exp(data)" ] }, { "cell_type": "markdown", "id": "1c16e2f2", "metadata": {}, "source": [ "Diese werden als einstellige ufuncs bezeichnet. Andere, wie [add](https://numpy.org/doc/stable/reference/generated/numpy.add.html) oder [maximum](https://numpy.org/doc/stable/reference/generated/numpy.maximum.html), nehmen zwei Arrays (also binäre ufuncs) und geben ein einziges Array als Ergebnis zurück:" ] }, { "cell_type": "code", "execution_count": 5, "id": "ee94de95", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.149586Z", "iopub.status.busy": "2026-05-21T16:51:21.149510Z", "iopub.status.idle": "2026-05-21T16:51:21.158466Z", "shell.execute_reply": "2026-05-21T16:51:21.158152Z", "shell.execute_reply.started": "2026-05-21T16:51:21.149578Z" } }, "outputs": [ { "data": { "text/plain": [ "(array([-0.44268441, 0.91852841, -3.0452678 , -0.19302094, 1.04784189,\n", " 0.78246093, -1.24320392, -0.85376618]),\n", " array([ 0.00448541, -1.31346391, 0.29722027, 2.53364962, 0.06792141,\n", " -1.52562906, -0.25359309, 0.45988813]))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "rng = np.random.default_rng()\n", "x = rng.normal(size=8)\n", "y = rng.normal(size=8)\n", "x, y" ] }, { "cell_type": "code", "execution_count": 6, "id": "583063e6", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.160094Z", "iopub.status.busy": "2026-05-21T16:51:21.159940Z", "iopub.status.idle": "2026-05-21T16:51:21.162342Z", "shell.execute_reply": "2026-05-21T16:51:21.162093Z", "shell.execute_reply.started": "2026-05-21T16:51:21.160086Z" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0.00448541, 0.91852841, 0.29722027, 2.53364962, 1.04784189,\n", " 0.78246093, -0.25359309, 0.45988813])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.maximum(x, y)" ] }, { "cell_type": "markdown", "id": "ce9fc716", "metadata": {}, "source": [ "Hier berechnete `numpy.maximum` das elementweise Maximum der Elemente in `x` und `y`." ] }, { "cell_type": "markdown", "id": "901af41f", "metadata": {}, "source": [ "Manche `ufunc`, wie z.B. [modf](https://numpy.org/doc/stable/reference/generated/numpy.modf.html) , eine vektorisierte Version des eingebauten Python [divmod](https://docs.python.org/3/library/functions.html#divmod), geben mehrere Arrays zurückgeben: die Bruch- und Integralteile eines Gleitkomma-Arrays:" ] }, { "cell_type": "code", "execution_count": 7, "id": "a4b665ed", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.162787Z", "iopub.status.busy": "2026-05-21T16:51:21.162723Z", "iopub.status.idle": "2026-05-21T16:51:21.164539Z", "shell.execute_reply": "2026-05-21T16:51:21.164315Z", "shell.execute_reply.started": "2026-05-21T16:51:21.162780Z" } }, "outputs": [], "source": [ "data = x * 5" ] }, { "cell_type": "code", "execution_count": 8, "id": "34c8f475", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.164922Z", "iopub.status.busy": "2026-05-21T16:51:21.164807Z", "iopub.status.idle": "2026-05-21T16:51:21.167187Z", "shell.execute_reply": "2026-05-21T16:51:21.166771Z", "shell.execute_reply.started": "2026-05-21T16:51:21.164911Z" } }, "outputs": [ { "data": { "text/plain": [ "array([ -2.21342203, 4.59264206, -15.22633899, -0.96510468,\n", " 5.23920946, 3.91230463, -6.2160196 , -4.26883088])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 9, "id": "1daabbb7", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.167574Z", "iopub.status.busy": "2026-05-21T16:51:21.167490Z", "iopub.status.idle": "2026-05-21T16:51:21.169119Z", "shell.execute_reply": "2026-05-21T16:51:21.168873Z", "shell.execute_reply.started": "2026-05-21T16:51:21.167567Z" } }, "outputs": [], "source": [ "remainder, whole_part = np.modf(x)" ] }, { "cell_type": "code", "execution_count": 10, "id": "9d827342", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.169691Z", "iopub.status.busy": "2026-05-21T16:51:21.169536Z", "iopub.status.idle": "2026-05-21T16:51:21.171791Z", "shell.execute_reply": "2026-05-21T16:51:21.171575Z", "shell.execute_reply.started": "2026-05-21T16:51:21.169681Z" } }, "outputs": [ { "data": { "text/plain": [ "array([-0.44268441, 0.91852841, -0.0452678 , -0.19302094, 0.04784189,\n", " 0.78246093, -0.24320392, -0.85376618])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "remainder" ] }, { "cell_type": "code", "execution_count": 11, "id": "9846dd84", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.172358Z", "iopub.status.busy": "2026-05-21T16:51:21.172225Z", "iopub.status.idle": "2026-05-21T16:51:21.174707Z", "shell.execute_reply": "2026-05-21T16:51:21.174426Z", "shell.execute_reply.started": "2026-05-21T16:51:21.172350Z" } }, "outputs": [ { "data": { "text/plain": [ "array([-0., 0., -3., -0., 1., 0., -1., -0.])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "whole_part" ] }, { "cell_type": "markdown", "id": "2248cfbd", "metadata": {}, "source": [ "Ufuncs akzeptieren ein optionales `out`-Argument, das es euch erlaubt, eure Ergebnisse an ein bestehendes Array zu übertragen, anstatt ein neues zu erstellen:" ] }, { "cell_type": "code", "execution_count": 12, "id": "28726c4d", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.175112Z", "iopub.status.busy": "2026-05-21T16:51:21.175034Z", "iopub.status.idle": "2026-05-21T16:51:21.176819Z", "shell.execute_reply": "2026-05-21T16:51:21.176485Z", "shell.execute_reply.started": "2026-05-21T16:51:21.175106Z" } }, "outputs": [], "source": [ "out = np.zeros_like(data)" ] }, { "cell_type": "code", "execution_count": 13, "id": "a725be5a", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.177222Z", "iopub.status.busy": "2026-05-21T16:51:21.177124Z", "iopub.status.idle": "2026-05-21T16:51:21.179356Z", "shell.execute_reply": "2026-05-21T16:51:21.179141Z", "shell.execute_reply.started": "2026-05-21T16:51:21.177179Z" } }, "outputs": [ { "data": { "text/plain": [ "array([ -1.21342203, 5.59264206, -14.22633899, 0.03489532,\n", " 6.23920946, 4.91230463, -5.2160196 , -3.26883088])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.add(data, 1)" ] }, { "cell_type": "code", "execution_count": 14, "id": "1046df1e", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.180065Z", "iopub.status.busy": "2026-05-21T16:51:21.179744Z", "iopub.status.idle": "2026-05-21T16:51:21.182359Z", "shell.execute_reply": "2026-05-21T16:51:21.182063Z", "shell.execute_reply.started": "2026-05-21T16:51:21.180041Z" } }, "outputs": [ { "data": { "text/plain": [ "array([ -1.21342203, 5.59264206, -14.22633899, 0.03489532,\n", " 6.23920946, 4.91230463, -5.2160196 , -3.26883088])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.add(data, 1, out=out)" ] }, { "cell_type": "code", "execution_count": 15, "id": "9a00a932", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T16:51:21.182992Z", "iopub.status.busy": "2026-05-21T16:51:21.182873Z", "iopub.status.idle": "2026-05-21T16:51:21.185072Z", "shell.execute_reply": "2026-05-21T16:51:21.184796Z", "shell.execute_reply.started": "2026-05-21T16:51:21.182985Z" } }, "outputs": [ { "data": { "text/plain": [ "array([ -1.21342203, 5.59264206, -14.22633899, 0.03489532,\n", " 6.23920946, 4.91230463, -5.2160196 , -3.26883088])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "out" ] }, { "cell_type": "markdown", "id": "e6d35a55", "metadata": {}, "source": [ "Einige einstellige ufuncs:\n", "\n", "Funktion | Beschreibung\n", ":------- | :-----------\n", "`abs`, `fabs` | berechnet den absoluten Wert elementweise für Ganzzahl-, Gleitkomma- oder komplexe Werte\n", "`sqrt` | berechnet die Quadratwurzel aus jedem Element (entspricht `data ** 0,5`)\n", "`square` | berechnet das Quadrat eines jeden Elements (entspricht `data ** 2`)\n", "`exp` | berechnet die Exponentialfunktion aller Elemente in einem Eingabearray\n", "`log`, `log10`, `log2`, `log1p` | Natürlicher Logarithmus (Basis e), log Basis 10, log Basis 2 bzw. log(1 + x)\n", "`sign` | berechnet das Vorzeichen jedes Elements: `1` (positiv), `0` (Null), oder `-1` (negativ)\n", "`ceil` | berechnet die Obergrenze jedes Elements (d.h. die kleinste ganze Zahl, die größer oder gleich dieser Zahl ist)\n", "`floor` | berechnet die Untergrenze jedes Elements (d.h. die größte ganze Zahl, die kleiner oder gleich jedem Element ist)\n", "`rint` | rundet Elemente auf die nächste Ganzzahl, wobei der `dtype` erhalten bleibt\n", "`modf` | gibt den gebrochenen und ganzzahligen Teile des Arrays als separate Arrays zurück\n", "`isnan` | gibt ein boolesches Array zurück, das angibt, ob jeder Wert `NaN` (Not a Number) ist\n", "`isfinite`, `isinf` | gibt ein boolesches Array zurück, das angibt, ob jedes Element endlich (non-`inf`, not-`NaN`) bzw. unendlich ist\n", "`cos`, `cosh`, `sin`, `sinh`, `tan`, `tanh` | reguläre und hyperbolische trigonometrische Funktionen\n", "`arccos`, `arccosh`, `arcsin`, `arcsinh`, `arctan`, `arctanh` | Inverse trigonometrische Funktionen\n", "`logical_not` | berechnet den Wahrheitswert von `not x` elementweise (entspricht `~data`)\n", "\n", "Einige binäre universelle Funktionen:\n", "\n", "Funktion | Beschreibung\n", ":------- | :-----------\n", "`add` | hinzufügen entsprechender Elemente in Arrays\n", "`subtract` | subtrahiert Elemente im zweiten Array vom ersten Array\n", "`multiply` | Array-Elemente multiplizieren\n", "`divide`, `floor_divide` | Dividieren oder Abschneiden des Rests\n", "`power` | erhöht Elemente im ersten Array auf die im zweiten Array angegebenen Potenzen\n", "`maximum`, `fmax` | elementweises Maximum; `fmax` ignoriert `NaN`\n", "`minimum`, `fmin` | elementweises Minimum; `fmin` ignoriert `NaN`\n", "`mod` | Elementweiser Modulus (Rest der Division)\n", "`copysign` | kopiert das Vorzeichen der Werte im zweiten Argument auf die Werte im ersten Argument\n", "`greater`, `greater_equal`, `less`, `less_equal`, `equal`, `not_equal` | Elementweise Vergleiche durchführen, die ein boolesches Array ergeben (entspricht den Infix-Operatoren `>`, `>=`, `<`, `<=`, `==`, `!=`)\n", "`logical_and` |berechnet den elementweisen Wahrheitswert der logischen Operation AND (`&`).\n", "`logical_or` | berechnet den elementweisen Wahrheitswert der logischen Operation OR (`|`).\n", "`logical_xor` | berechnet den elementweisen Wahrheitswert der logischen Operation XOR (`^`).\n", "\n", "
\n", "\n", "**Hinweis:**\n", "\n", "Eine vollständige Übersicht über binäre universelle Funktionen findet ihr in [Universal functions (ufunc)](https://numpy.org/doc/stable/reference/ufuncs.html).\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "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.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }