{ "cells": [ { "cell_type": "markdown", "id": "48faa768", "metadata": {}, "source": [ "# Arithmetik\n", "\n", "Arrays ermöglichen euch, Stapeloperationen auf Daten durchzuführen ohne `for`-Schleifen verwenden zu müssen. Dies wird bei NumPy *Vektorisierung* genannt. Bei allen arithmetischen Operationen zwischen Arrays gleicher Größe wird die Operation elementweise angewendet:" ] }, { "cell_type": "code", "execution_count": 1, "id": "af888012", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:30:06.535439Z", "iopub.status.busy": "2026-05-21T17:30:06.535171Z", "iopub.status.idle": "2026-05-21T17:30:06.582101Z", "shell.execute_reply": "2026-05-21T17:30:06.581778Z", "shell.execute_reply.started": "2026-05-21T17:30:06.535414Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.3377999 , 0.37343236, 0.61613397],\n", " [0.71191857, 0.35041942, 0.64726029],\n", " [0.06018761, 0.83997689, 0.31628701],\n", " [0.14621145, 0.26867346, 0.07336511],\n", " [0.22718287, 0.77538148, 0.40654442],\n", " [0.44235763, 0.34000371, 0.53182733],\n", " [0.98246577, 0.08442906, 0.33648811]])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "rng = np.random.default_rng()\n", "data = rng.random((7, 3))\n", "data" ] }, { "cell_type": "code", "execution_count": 2, "id": "a68ec8c7", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:30:06.582816Z", "iopub.status.busy": "2026-05-21T17:30:06.582652Z", "iopub.status.idle": "2026-05-21T17:30:06.585385Z", "shell.execute_reply": "2026-05-21T17:30:06.585077Z", "shell.execute_reply.started": "2026-05-21T17:30:06.582805Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 2.96033241, 2.6778611 , 1.62302363],\n", " [ 1.40465503, 2.8537231 , 1.5449735 ],\n", " [16.6147153 , 1.19050894, 3.1616853 ],\n", " [ 6.8394097 , 3.7219903 , 13.63045757],\n", " [ 4.40174025, 1.28968776, 2.45975579],\n", " [ 2.26061434, 2.94114442, 1.88030952],\n", " [ 1.01784717, 11.84426242, 2.97187318]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 / data" ] }, { "cell_type": "code", "execution_count": 3, "id": "4f3ab385", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:30:06.586699Z", "iopub.status.busy": "2026-05-21T17:30:06.586424Z", "iopub.status.idle": "2026-05-21T17:30:06.590717Z", "shell.execute_reply": "2026-05-21T17:30:06.590381Z", "shell.execute_reply.started": "2026-05-21T17:30:06.586688Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.11410877, 0.13945173, 0.37962107],\n", " [0.50682805, 0.12279377, 0.41894589],\n", " [0.00362255, 0.70556118, 0.10003747],\n", " [0.02137779, 0.07218543, 0.00538244],\n", " [0.05161206, 0.60121644, 0.16527837],\n", " [0.19568027, 0.11560252, 0.28284031],\n", " [0.96523899, 0.00712827, 0.11322425]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data**2" ] }, { "cell_type": "markdown", "id": "8e4492ab", "metadata": {}, "source": [ "Vergleich zweier Arrays:" ] }, { "cell_type": "code", "execution_count": 4, "id": "5520fe3b", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:30:06.591247Z", "iopub.status.busy": "2026-05-21T17:30:06.591161Z", "iopub.status.idle": "2026-05-21T17:30:06.593677Z", "shell.execute_reply": "2026-05-21T17:30:06.593378Z", "shell.execute_reply.started": "2026-05-21T17:30:06.591240Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ True, False, True],\n", " [ True, False, True],\n", " [False, True, False],\n", " [False, False, False],\n", " [ True, True, False],\n", " [ True, False, True],\n", " [ True, False, False]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data2 = rng.random((7, 3))\n", "data > data2" ] } ], "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 }