{ "cells": [ { "cell_type": "markdown", "id": "conditional-vermont", "metadata": {}, "source": [ "#### `==` vs `is`" ] }, { "cell_type": "code", "execution_count": null, "id": "serial-chamber", "metadata": {}, "outputs": [], "source": [ "a = 1.0\n", "b = 1.0\n", "print(a == b)\n", "print(a is b)" ] }, { "cell_type": "code", "execution_count": null, "id": "nominated-completion", "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3]\n", "b = [1, 2, 3]\n", "print(a == b)\n", "print(a is b)" ] }, { "cell_type": "code", "execution_count": null, "id": "vietnamese-needle", "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3]\n", "b = a\n", "print(a == b)\n", "print(a is b)\n", "b[0] = 42\n", "print(a)" ] }, { "cell_type": "markdown", "id": "random-dispute", "metadata": {}, "source": [ "#### numpy copy vs view" ] }, { "cell_type": "code", "execution_count": null, "id": "higher-contamination", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "id": "continental-panel", "metadata": {}, "outputs": [], "source": [ "a = np.arange(10)\n", "b = a[5] #copy numeric type\n", "b = 42\n", "a" ] }, { "cell_type": "code", "execution_count": null, "id": "bronze-heather", "metadata": {}, "outputs": [], "source": [ "a = np.arange(10)\n", "b = a #alias\n", "b[5] = 42\n", "a" ] }, { "cell_type": "code", "execution_count": null, "id": "rational-thursday", "metadata": {}, "outputs": [], "source": [ "a = np.arange(10)\n", "b = a[5:10] #view\n", "b[0] = 42\n", "a" ] }, { "cell_type": "code", "execution_count": null, "id": "arranged-hello", "metadata": {}, "outputs": [], "source": [ "a = np.arange(10)\n", "b = np.array(a[5:10]) #copy array view\n", "b[0] = 42\n", "a" ] }, { "cell_type": "code", "execution_count": null, "id": "finite-copyright", "metadata": {}, "outputs": [], "source": [ "a = np.arange(10)\n", "b = a[[5, 6, 7, 8, 9]] #copy by advanced indexing\n", "b[0] = 42\n", "a" ] }, { "cell_type": "markdown", "id": "endangered-throw", "metadata": {}, "source": [ "### Give an equivalent python function for the following mathematical expressions\n", "Vector and matrix variables are represented by numpy arrays.\n", "Run the assert cells to check your solution." ] }, { "cell_type": "markdown", "id": "dependent-crime", "metadata": {}, "source": [ "$f(\\mathbf{x}, \\mathbf{y}) = \\sum \\limits_n x_n y_n$\n", "$\\qquad \\mathbf{x}, \\mathbf{y} \\in \\mathbb{R}^N$\n", "$\\qquad 0 \\leq n \\lt N$" ] }, { "cell_type": "code", "execution_count": null, "id": "forbidden-ireland", "metadata": {}, "outputs": [], "source": [ "# solution using a loop\n", "def f(x, y):\n", " s = 0\n", " for n in range(x.shape[0]):\n", " s += x[n] * y[n]\n", " return s" ] }, { "cell_type": "markdown", "id": "sixth-works", "metadata": {}, "source": [ "Which is equivalent to the dot product:" ] }, { "cell_type": "code", "execution_count": null, "id": "needed-haiti", "metadata": {}, "outputs": [], "source": [ "# solution using the numpy dot product (more efficient and concise)\n", "def f(x, y):\n", " return x @ y" ] }, { "cell_type": "code", "execution_count": null, "id": "referenced-siemens", "metadata": {}, "outputs": [], "source": [ "assert f(np.array([1, 2]), np.array([3, 4])) == 11" ] }, { "cell_type": "markdown", "id": "rising-quantum", "metadata": {}, "source": [ "$g(\\mathbf{X}, \\mathbf{y})_m = \\sum \\limits_n X_{m, n} y_n$\n", "$\\qquad \\mathbf{X} \\in \\mathbb{R}^{MxN}$\n", "$\\quad \\mathbf{y} \\in \\mathbb{R}^{N}$\n", "$\\quad \\mathbf{g}(\\mathbf{X}, \\mathbf{y}) \\in \\mathbb{R}^{M}$\n", "$\\qquad 0 \\leq m \\lt M \\quad 0 \\leq n \\lt N$" ] }, { "cell_type": "code", "execution_count": null, "id": "racial-retail", "metadata": {}, "outputs": [], "source": [ "# solution using for loops\n", "def g(X, y):\n", " s = np.zeros(X.shape[0])\n", " for m in range(X.shape[0]):\n", " for n in range(X.shape[1]):\n", " s[m] += X[m, n] * y[n]\n", " return s" ] }, { "cell_type": "markdown", "id": "increasing-brief", "metadata": {}, "source": [ "Which is again equivalent to the dot product:" ] }, { "cell_type": "code", "execution_count": null, "id": "reliable-confusion", "metadata": {}, "outputs": [], "source": [ "# solution using the numpy dot product (more efficient and concise)\n", "def g(X, y):\n", " return X @ y" ] }, { "cell_type": "code", "execution_count": null, "id": "impressive-policy", "metadata": {}, "outputs": [], "source": [ "X = np.array([[0, 2, 4],\n", " [1, 3, 5]])\n", "assert (g(X, np.array([1, 2, 3])) == np.array([16, 22])).all()" ] }, { "cell_type": "markdown", "id": "thrown-dimension", "metadata": {}, "source": [ "$\\Phi(\\mathbf x, N)_{m, n} = x_m^n$\n", "$\\qquad \\mathbf x \\in \\mathbb R^M$\n", "$\\quad \\mathbf\\Phi(\\mathbf x, N) \\in \\mathbb R^{MxN}$\n", "$\\qquad 0 \\leq m \\lt M$\n", "$\\qquad 0 \\leq n \\lt N$" ] }, { "cell_type": "code", "execution_count": null, "id": "qualified-polish", "metadata": {}, "outputs": [], "source": [ "# solution using for loops\n", "def phi(x, N):\n", " phi = np.zeros((x.shape[0], N))\n", " for m in range(phi.shape[0]):\n", " for n in range(phi.shape[1]):\n", " phi[m, n] = x[m]**n\n", " return phi" ] }, { "cell_type": "code", "execution_count": null, "id": "monthly-lingerie", "metadata": {}, "outputs": [], "source": [ "# solution using numpy broadcasting (more efficient and concise) \n", "def phi(x, N):\n", " return np.power(x[:, np.newaxis], np.arange(N))" ] }, { "cell_type": "code", "execution_count": null, "id": "religious-quarter", "metadata": {}, "outputs": [], "source": [ "assert (phi(np.array([1, 2, 3]), 3) == np.array([[1, 1, 1],\n", " [1, 2, 4],\n", " [1, 3, 9]])).all()" ] } ], "metadata": { "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.7.9" } }, "nbformat": 4, "nbformat_minor": 5 }