{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Implementing a routine to compute vector length" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Preliminaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Again, copy this notebook so that you don't corrupt the original! Then you can \"play\" with the copy of the notebook all you want!

\n", "\n", "

\n", "\n", "NOTE: A common problem that students have with IPython notebooks is not understanding that when the code in the gray boxes (cells) is executed, it assigns variables that persist the whole time that the notebook is open. Further, some cells rely on variables assigned by earlier cells. If you execute these cells out of order, or if you execute the same cell twice, then you may end up changing the value of the variables. To correct this, click on \"Cell\" at the top and execute \"run all above\" or \"run all\". You can also reset all cells by clicking \"Cell -> All Output -> Clear\"\n", "\n", "

\n", "\n", "

In this notebook, we show how to write a routine to compute vector length.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's start by importing numpy and creating a vector $ x = \\left( \\begin{array}{r} 1 \\\\ 2 \\\\ 3 \\end{array} \\right) $.\n", "\n", "Execute the code in the box by clicking in the box and then on \"Cell -> Run\". Alternative, click on the box and push \"Shift\" and \"Return\" (or \"Enter\") together." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np # This imports a package called \"numpy\" that will make working with matrices \n", " # simpler\n", "\n", "# create a two-dimensional matrix with only one column. \n", "x = np.matrix( '1.;2.;3.' )\n", "print( 'x = ' )\n", "print( x )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, recall that the length of a vector equals $ \\| x \\|_2 = \\sqrt{ x^T x } $. So, we can use the dot product routine that we wrote before to compute the length:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import laff\n", "import math\n", "\n", "length_x = math.sqrt( laff.dot( x, x ) )\n", "\n", "print( 'length_x:' )\n", "print( length_x )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Length as a simple routine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "As before, it is preferable to create a routine that computes the length.\n", "

\n", "\n", "

\n", "Complete the following routine to implement this:\n", "

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def length( x ):\n", " #Your code here\n", " " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Be sure the run the above box, or this notebook won't know about the routine!!!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, if you execute" ] }, { "cell_type": "code", "collapsed": false, "input": [ "length_x = length( x )\n", "\n", "print( 'length_x:' )\n", "print( length_x )\n", "\n", "print( 'difference between length_x and math.sqrt( laff.dot( x, x ) ):' )\n", "print( length_x - math.sqrt( laff.dot( x, x ) ) )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result should be:\n", "\n", "\n", "length_x:\n", "3.7416573867739413\n", "difference between length_x and math.sqrt( laff.dot( x, x ) ):\n", "0.0\n", "" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "A complete length function as part of the LAFF library" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we proceed with progressively more advanced operations and routines, we are going to need a general dot routine where $ x $ and $ y $ can be row and/or column vectors. \n", "\n", "This routine is part of the 'laff' library. If you do\n", "\n", "\n", "import laff\n", "\n", "\n", "then laff.norm2( x ) will perform the desired computation of the length of $ x $, when x is a column and/or a row vectors. If you really want to see what this routine looks like, then ask for it on the discussion forum and we'll point you to where it can be found." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import laff\n", "\n", "length_x = laff.norm2( x )\n", "\n", "print( 'length_x:' )\n", "print( length_x )\n", "\n", "print( 'difference between length_x and math.sqrt( laff.dot( x, x ) ):' )\n", "print( length_x - math.sqrt( laff.dot( x, x ) ) )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Need a challenge?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In \"1.7.3 Overflow and Underflow\", we discuss how computing the length with the dot product can inadvertently cause overflow or underflow. Write a routine that avoids that" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def length( x ):\n", " ### You fill in the rest!" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }