{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework #3\n", "### Assignment\n", "1. Define a function euclidean_distance that computes the distance between two 1D vectors of size n according to the formula:\n", "\n", "$$\n", "d(a,b) = \\sqrt{\\sum_{i=1}^n{(a_i - b_i)^2}}\n", "$$\n", "\n", "The function should check that the vector have the same size and implement the simplest iterative version and the two methods proposed within this lecture, selectable by an additional parameter mode of the function.\n", "\n", "Test the function with pairs of randomly generated vectors of size [5, 10, 20, 50] and measure its performance with the %timeit magic.\n", "\n", "2. Solve the following linear equations system, in the form $Ax = b$, using numpy functions described in the present lecture:\n", "\n", "$$\n", "4x + 3y + 2z = 25 \\\\\n", "-2x + 2y + 3z = -10 \\\\\n", "3x -5y + 2z = -4\n", "$$\n", "\n", "### Possible solutions to questions 1 & 2 \n", "#### Question 1" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = [0.04756666 0.17755706 0.21323304 0.08255607 0.36625129]\n", "b = [0.66822995 0.39391188 0.54365618 0.53536603 0.05558075]\n", "9.24 µs ± 368 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n", "ED LO= 0.918\n", "15.6 µs ± 321 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n", "ED SS= 0.918\n", "9.51 µs ± 101 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n", "ED DP= 0.918\n", "a = [0.04221634 0.60532675 0.97611896 0.25605099 0.9669407 0.2492855\n", " 0.01462257 0.48600282 0.30880954 0.11884866]\n", "b = [0.61442178 0.07522763 0.71285844 0.18123605 0.90568225 0.35599843\n", " 0.67274454 0.11149599 0.19818347 0.14177104]\n", "14.9 µs ± 296 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n", "ED LO= 1.133\n", "16.3 µs ± 1.62 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n", "ED SS= 1.133\n", "9.6 µs ± 179 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n", "ED DP= 1.133\n", "a = [0.36454152 0.35431341 0.2452204 0.10963099 0.91409061 0.00741397\n", " 0.06116185 0.43287493 0.77105923 0.69210503 0.97740584 0.9063517\n", " 0.42872982 0.7640695 0.83426393 0.48779364 0.91748042 0.64518327\n", " 0.33178232 0.91955856]\n", "b = [0.70857996 0.34099661 0.35538063 0.32251283 0.06596971 0.1466998\n", " 0.60591954 0.38263155 0.2213241 0.11373697 0.45443336 0.08267574\n", " 0.350592 0.42075258 0.8478867 0.36166327 0.46768857 0.52536167\n", " 0.50157416 0.78699725]\n", "25.9 µs ± 1.12 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n", "ED LO= 1.790\n", "15.4 µs ± 253 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n", "ED SS= 1.790\n", "9.59 µs ± 171 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n", "ED DP= 1.790\n", "a = [0.66241041 0.8977363 0.76395084 0.09898128 0.41696298 0.33718484\n", " 0.2419236 0.95001454 0.92678579 0.00159685 0.08473459 0.16011744\n", " 0.30823732 0.4361975 0.87232125 0.5002936 0.29713966 0.94571833\n", " 0.75614151 0.04086167 0.52076853 0.76616898 0.18614206 0.49347664\n", " 0.26085435 0.1626869 0.24500608 0.61899173 0.32166108 0.69792644\n", " 0.28951983 0.83369022 0.94368824 0.69743006 0.06780959 0.82593332\n", " 0.18528358 0.38031009 0.32104502 0.91594209 0.2993625 0.12514716\n", " 0.23557262 0.50277038 0.2702443 0.05095381 0.40815823 0.56183583\n", " 0.31517506 0.82925131]\n", "b = [0.27587699 0.42375746 0.77437957 0.9413573 0.77644966 0.41041505\n", " 0.017733 0.319019 0.0186726 0.77862412 0.78194412 0.54206786\n", " 0.58276347 0.45608191 0.18558384 0.89378062 0.61439171 0.2278767\n", " 0.85073462 0.42662207 0.14000123 0.69409834 0.04850972 0.95771897\n", " 0.48034958 0.64479738 0.78451301 0.24552058 0.3019615 0.76305405\n", " 0.34267731 0.35379068 0.54755861 0.94291601 0.12346746 0.97399342\n", " 0.12388551 0.88549354 0.46390048 0.70671859 0.73449434 0.06224399\n", " 0.8435283 0.26863556 0.32315355 0.15195794 0.41800812 0.92919966\n", " 0.97428459 0.32225621]\n", "64.2 µs ± 1.69 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n", "ED LO= 2.926\n", "16.5 µs ± 273 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n", "ED SS= 2.926\n", "9.56 µs ± 147 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n", "ED DP= 2.926\n" ] } ], "source": [ "import numpy as np\n", "\n", "def euclidean_distance(a,b,mode):\n", " l = len(a)\n", " assert(l == len(b)),\"Arrays must have the same length\"\n", " if mode == 0:\n", " # iterative version\n", " vsum = 0\n", " for i in range(l):\n", " vsum += (a[i]-b[i])**2\n", " ed = np.sqrt(vsum)\n", " elif mode == 1:\n", " # sum of squares version\n", " ed = np.sqrt(np.sum(np.square(a-b)))\n", " elif mode == 2:\n", " # dot product\n", " ed = np.sqrt(np.dot(a-b,a-b))\n", " else:\n", " ed = None\n", " \n", " return ed\n", "\n", "for dimlist in [5,10,20,50]:\n", " a = np.random.random(dimlist)\n", " print('a = {}'.format(a))\n", " b = np.random.random(dimlist)\n", " print('b = {}'.format(b))\n", "\n", " %timeit euclidean_distance(a,b,0) # with the loop\n", " ED = euclidean_distance(a,b,0)\n", " print('ED LO= {:.3f}'.format(ED))\n", "\n", " %timeit euclidean_distance(a,b,1) # with the sum of squares\n", " ED = euclidean_distance(a,b,1)\n", " print('ED SS= {:.3f}'.format(ED))\n", "\n", " %timeit euclidean_distance(a,b,2) # using dot product\n", " ED = euclidean_distance(a,b,2)\n", " print('ED DP= {:.3f}'.format(ED))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Question 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 5. 3. -2.]\n" ] } ], "source": [ "A = np.array([[4, 3, 2], [-2, 2, 3], [3, -5, 2]])\n", "b = np.array([25, -10, -4])\n", "\n", "x = np.linalg.inv(A).dot(b)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.16" } }, "nbformat": 4, "nbformat_minor": 4 }