{ "cells": [ { "cell_type": "markdown", "id": "942f3aba", "metadata": {}, "source": [ "# Arithmetic\n", "\n", "Arrays allow you to perform stack operations on data without having to use `for` loops. This is called *vectorisation* in NumPy. For all arithmetic operations between arrays of the same size, the operation is applied element by element:" ] }, { "cell_type": "code", "execution_count": 1, "id": "cc82112f", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:08:12.719027Z", "iopub.status.busy": "2026-05-17T15:08:12.718822Z", "iopub.status.idle": "2026-05-17T15:08:12.774409Z", "shell.execute_reply": "2026-05-17T15:08:12.773880Z", "shell.execute_reply.started": "2026-05-17T15:08:12.719005Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.29244924, 0.32072447, 0.30934757],\n", " [0.89174829, 0.84865903, 0.8219735 ],\n", " [0.73972395, 0.139968 , 0.7551268 ],\n", " [0.30778384, 0.16360734, 0.05536757],\n", " [0.26396912, 0.08043216, 0.24179992],\n", " [0.93238137, 0.02947168, 0.26255382],\n", " [0.90513945, 0.50250317, 0.2176639 ]])" ] }, "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": "1d3fdcf6", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:08:12.774985Z", "iopub.status.busy": "2026-05-17T15:08:12.774870Z", "iopub.status.idle": "2026-05-17T15:08:12.777663Z", "shell.execute_reply": "2026-05-17T15:08:12.777480Z", "shell.execute_reply.started": "2026-05-17T15:08:12.774976Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 3.41939682, 3.11794111, 3.23260989],\n", " [ 1.12139267, 1.17832953, 1.21658423],\n", " [ 1.35185566, 7.14449008, 1.3242809 ],\n", " [ 3.24903344, 6.11219537, 18.06111538],\n", " [ 3.78832187, 12.43283799, 4.13565066],\n", " [ 1.0725225 , 33.93087472, 3.80874296],\n", " [ 1.10480214, 1.9900372 , 4.59423914]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 / data" ] }, { "cell_type": "code", "execution_count": 3, "id": "3b409fac", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:08:12.777983Z", "iopub.status.busy": "2026-05-17T15:08:12.777924Z", "iopub.status.idle": "2026-05-17T15:08:12.780224Z", "shell.execute_reply": "2026-05-17T15:08:12.780029Z", "shell.execute_reply.started": "2026-05-17T15:08:12.777977Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[8.55265575e-02, 1.02864183e-01, 9.56959163e-02],\n", " [7.95215015e-01, 7.20222151e-01, 6.75640436e-01],\n", " [5.47191516e-01, 1.95910415e-02, 5.70216487e-01],\n", " [9.47308944e-02, 2.67673603e-02, 3.06556738e-03],\n", " [6.96796977e-02, 6.46933218e-03, 5.84672015e-02],\n", " [8.69335024e-01, 8.68580127e-04, 6.89345065e-02],\n", " [8.19277417e-01, 2.52509435e-01, 4.73775726e-02]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data**2" ] }, { "cell_type": "markdown", "id": "8bb8388b", "metadata": {}, "source": [ "Comparison of two arrays:" ] }, { "cell_type": "code", "execution_count": 4, "id": "248171de", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:08:12.780656Z", "iopub.status.busy": "2026-05-17T15:08:12.780563Z", "iopub.status.idle": "2026-05-17T15:08:12.782853Z", "shell.execute_reply": "2026-05-17T15:08:12.782643Z", "shell.execute_reply.started": "2026-05-17T15:08:12.780648Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[False, True, False],\n", " [ True, True, True],\n", " [ True, False, True],\n", " [ True, False, False],\n", " [False, False, False],\n", " [ True, False, False],\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 }