{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numpy for arrays! " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(0, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate\n", "$$\n", "m_i = \\frac{1}{2} (a_i + a_{i+1})\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## loops " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = np.zeros(len(a)-1)\n", "# write with loop " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## arrays " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Can we avoid the loop?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* `a[i]` covers the elements `[0, 1, 2, 3, 4]`\n", "* `a[i+1]` covers the elements `[1, 2, 3, 4, 5]`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Think about shifting the arrays:\n", "```\n", "[0 1 2 3 4]\n", " [0 1 2 3 4]\n", "-------------\n", " 1 3 5 7\n", " * 0.5\n", "=====================\n", " 0.5 1.5 2.5 3.5 \n", "``` " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Can we get these numbers by slicing?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", " [1 2 3 4] = a[1:]\n", " [0 1 2 3] = a[:-1]\n", "-----------\n", " 1 3 5 7 = a[1:] + a[:-1]\n", "``` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Think about these equivalences between loop and array approach:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* `a[i]` is like `a[:-1]`\n", "* `a[i+1]` is like `a[1:]`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use the sliced arrays " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# write with sliced arrays (NO loops!)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Using sliced arrays is\n", "* more readable (more elegant)\n", "* faster \n", "\n", "**Prefered way to work with arrays**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Speed-up" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How fast is it?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "for i in range(len(a)-1):\n", " m[i] = 0.5*(a[i] + a[i+1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "m = 0.5*(a[:-1] + a[1:])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise\n", "Use *numpy arrays* to calculate for a vector $\\mathbf{b}$ of length $N$\n", "\n", "$$\n", "y_i = \\frac{1}{2}(b_{i-1} + b_{i+1})\n", "$$\n", "\n", "and set $y_0 = y_{N-1} = 100$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = np.arange(11)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# add your solution here" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }