{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial A11" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1__ Write a function that takes three vectors $\\mathbf{a}$, $\\mathbf{b}$, $\\mathbf{c}$ in 2D (NumPy arrays) and checks whether they form a triangle, i.e. if $a + b = \\pm c$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2__ Consider the vector $\\mathbf{v} = \\begin{pmatrix}0& 0& 1\\end{pmatrix}^\\mathrm{T}$ and the rotation matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\begin{equation*}\n", "\\mathbf{R} = \\begin{pmatrix} 1 & 0 & 0 \\\\\n", "0 & 0 & -1\\\\\n", "0 & 1 & 0\n", "\\end{pmatrix}\n", "\\end{equation*}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Rotate $\\mathbf{v}$ by $\\mathbf{R}$ to obtain $\\mathbf{v}^\\prime$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Compute the scalar product of $\\mathbf{v}$ and $\\mathbf{v}^\\prime$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - By which angle did we rotate along which axis?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__3__ Consider the second Laplace spherical harmonic\n", "\n", "$$Y_1^{-1}(\\theta, \\phi) = \\frac{1}{2}\\sqrt{\\frac{3}{2\\pi}}\\sin\\theta\\mathrm{e}^{-i\\phi}$$\n", "\n", " - Compute $Y$ on a grid of 10 points per dimension ($0 \\leq \\theta \\leq \\pi$, $0 \\leq \\phi < 2\\pi$)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__4__ Given 1000 randomly places points in 2D, compute all the pairwise distances according to the formula below (see example below on how to generate random numbers).\n", "\n", "$d_{ij} = \\sqrt{(x_i-x_j)^2+(y_i-y_j)^2}$ with $i, j \\in (1,2,...,1000)$\n", "\n", "Use three different Methods for this and compare the times (see example below on how to stop the time in Python):\n", "\n", " - __a)__ A naive double for loop \n", " - __Advanced b)__ A vectorized version using meshgrids\n", " - __Advanced c)__ Outer subtraction\n", " - __Advanced d)__ The `cdist` function from the `scipy` module (`scipy.spatial.distance.cdist`)\n", "\n", "How many distances are larger than 0.5?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "run_control": { "marked": true } }, "outputs": [], "source": [ "# How to generate random numbers in NumPy\n", "import numpy as np\n", "\n", "x = np.random.random(1000)\n", "# Gives you 1000 numbers in the half-open interval [0., 1.)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Doing ...\n", "... stuff ...\n", "Total execution time: 2.0033 seconds\n" ] } ], "source": [ "# Example on how to measure execution times\n", "\n", "import time\n", "# Import the time module from the standard library\n", "\n", "t0 = time.time()\n", "# Store the current system time\n", "\n", "print(\"Doing ...\")\n", "time.sleep(1)\n", "# do something (or do nothing)\n", "\n", "print(\"... stuff ...\")\n", "time.sleep(1)\n", "\n", "t = time.time() - t0\n", "# Get the current system time again and compute the ellapsed time\n", "\n", "print(f\"Total execution time: {t:.4f} seconds\")" ] } ], "metadata": { "anaconda-cloud": {}, "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.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }