{"nbformat_minor": 0, "nbformat": 3, "metadata": {"name": "", "signature": "sha256:125eb623f26c761e7d2d627e9d43a1ffb6cd11968fb065b3ef3231a63c5d6dad"}, "worksheets": [{"metadata": {}, "cells": [{"cell_type": "markdown", "source": ["# Vector Norms"], "metadata": {}}, {"cell_type": "markdown", "source": ["$p$-norms can be computed in two different ways in numpy:"], "metadata": {}}, {"cell_type": "code", "outputs": [], "collapsed": false, "metadata": {}, "input": ["import numpy as np\n", "import numpy.linalg as la"], "prompt_number": 18, "language": "python"}, {"cell_type": "code", "outputs": [], "collapsed": false, "metadata": {}, "input": ["x = np.array([1.,2,3])"], "prompt_number": 19, "language": "python"}, {"cell_type": "code", "outputs": [{"prompt_number": 20, "text": ["3.7416573867739413"], "output_type": "pyout", "metadata": {}}], "collapsed": false, "metadata": {}, "input": ["np.sum(x**2)**(1/2)"], "prompt_number": 20, "language": "python"}, {"cell_type": "code", "outputs": [{"prompt_number": 21, "text": ["3.7416573867739413"], "output_type": "pyout", "metadata": {}}], "collapsed": false, "metadata": {}, "input": ["la.norm(x, 2)"], "prompt_number": 21, "language": "python"}, {"cell_type": "markdown", "source": ["Both of the values above represent the 2-norm: $\\|x\\|_2$."], "metadata": {}}, {"cell_type": "markdown", "source": ["--------------\n", "Different values of $p$ work similarly:"], "metadata": {}}, {"cell_type": "code", "outputs": [{"prompt_number": 22, "text": ["3.0773848853940629"], "output_type": "pyout", "metadata": {}}], "collapsed": false, "metadata": {}, "input": ["np.sum(np.abs(x)**5)**(1/5)"], "prompt_number": 22, "language": "python"}, {"cell_type": "code", "outputs": [{"prompt_number": 23, "text": ["3.0773848853940629"], "output_type": "pyout", "metadata": {}}], "collapsed": false, "metadata": {}, "input": ["la.norm(x, 5)"], "prompt_number": 23, "language": "python"}, {"cell_type": "markdown", "source": ["---------------------\n", "\n", "The $\\infty$ norm represents a special case, because it's actually (in some sense) the *limit* of $p$-norms as $p\\to\\infty$.\n", "\n", "Recall that: $\\|x\\|_\\infty = \\max(|x_1|, |x_2|, |x_3|)$.\n", "\n", "Where does that come from? Let's try with $p=100$:"], "metadata": {}}, {"cell_type": "code", "outputs": [{"prompt_number": 24, "text": ["array([ 1.00000000e+00, 1.26765060e+30, 5.15377521e+47])"], "output_type": "pyout", "metadata": {}}], "collapsed": false, "metadata": {}, "input": ["x**100"], "prompt_number": 24, "language": "python"}, {"cell_type": "code", "outputs": [{"prompt_number": 25, "text": ["5.1537752073201132e+47"], "output_type": "pyout", "metadata": {}}], "collapsed": false, "metadata": {}, "input": ["np.sum(x**100)"], "prompt_number": 25, "language": "python"}, {"cell_type": "markdown", "source": ["Compare to last value in vector: the addition has essentially taken the maximum:"], "metadata": {}}, {"cell_type": "code", "outputs": [{"prompt_number": 26, "text": ["3.0"], "output_type": "pyout", "metadata": {}}], "collapsed": false, "metadata": {}, "input": ["np.sum(x**100)**(1/100)"], "prompt_number": 26, "language": "python"}, {"cell_type": "markdown", "source": ["Numpy can compute that, too:"], "metadata": {}}, {"cell_type": "code", "outputs": [{"prompt_number": 27, "text": ["3.0"], "output_type": "pyout", "metadata": {}}], "collapsed": false, "metadata": {}, "input": ["la.norm(x, np.inf)"], "prompt_number": 27, "language": "python"}]}]}