{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Implementing a routine that scales a vector" ] }, { "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, you are asked to write the loop that scales a vector.

" ] }, { "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) $ and a scalar $ \\alpha $. \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 )\n", "\n", "alpha = 2.5\n", "print( 'alpha = ' )\n", "print( alpha )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With Python, you can simply multiply $ \\alpha x $ to scale the vector, $ x $. We will first make a copy of $ x $ so we can easily reset $ x $ to its original contents in the future. For this, we will use the laff.copy routine." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import laff\n", "\n", "xold = np.matrix( '0.;0.;0.' )\n", "\n", "laff.copy( x, xold )\n", "\n", "print( 'xold = ' )\n", "print( xold )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's scale $ x $:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "laff.copy( xold, x )\n", "\n", "print( 'x before scaling:')\n", "print( x )\n", "\n", "x = alpha * x\n", "\n", "print( 'x after scaling: ' )\n", "print( x )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Scaling with a loop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we want you to write a loop that scales the three entries in $ x $ by $ \\alpha $. \n", "Recall that the loop for copying three entries of a vector $ x $ into a vector $ y $ was given by \n", "\n", "\n", " for i in range( 3 ):\n", " y[ i, 0 ] = x[ i, 0 ] \n", "\n", "\n", "Don't forget the indentation before y[ i, 0 ] since that indicates what statements are part of the \"loop body\" (in other words, what statements are executed as part of the \"for loop\")." ] }, { "cell_type": "code", "collapsed": false, "input": [ "laff.copy( xold, x )\n", "\n", "print( 'x before scaling:')\n", "print( x )\n", "\n", "# insert your loop here\n", " \n", "print( 'x after scaling:' )\n", "print( x )\n", "\n", "print( 'difference between result and alpha * xold:' )\n", "print( x - alpha * xold )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Scaling as a simple routine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Writing the \"for loop\" every time you want to scale a vector is cumbersome. Obviously, you can do \" x = alpha * x \", but the point of this exercise is for you to write your own routine. For this reason, you are going to write a routine, scal( alpha, x ), that scales the contents of vector x by the value of scalar alpha. \n", "\n", "Recall, here is what it means to scale a vector $ x $ of size $ m $ by $ \\alpha $:\n", "$$\n", "\\left( \\begin{array}{c}\n", "\\chi_0 \\\\\n", "\\chi_1 \\\\\n", "\\vdots \\\\\n", "\\chi_{m-1}\n", "\\end{array}\n", "\\right)\n", ":=\n", "\\left( \\begin{array}{c}\n", "\\alpha \\chi_0 \\\\\n", "\\alpha \\chi_1 \\\\\n", "\\vdots \\\\\n", "\\alpha \\chi_{m-1}\n", "\\end{array}\n", "\\right)\n", "$$\n", "\n", "As an algorithm this can be written as\n", "

\n", " for $ i = 0, \\ldots , m-1 $
\n", " $ ~~~ \\chi_i := \\alpha \\chi_i $
\n", " endfor \n", "

\n", "\n", "

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

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def scal( alpha, x ):\n", "\n", " # Check how many elements there are in vector x. For this, \n", " # np.shape( x ) return the row and column size of x, where x is a matrix.\n", " \n", " m, n = np.shape( x )\n", " \n", " # insert your loop here!!!" ], "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": [ "laff.copy( xold, x )\n", "\n", "print( 'x before scaling:' )\n", "print( x )\n", "\n", "scal( alpha, x )\n", "\n", "print( 'x after scaling:' )\n", "print( x )\n", "\n", "print( 'difference between result and alpha * xold:' )\n", "print( x - alpha * xold )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This should result in:\n", "\n", "\n", "x before scaling:\n", "[[ 1.]\n", " [ 2.]\n", " [ 3.]]\n", "x after scaling:\n", "[[ 2.5]\n", " [ 5. ]\n", " [ 7.5]]\n", "difference between result and alpha * xold:\n", "[[ 0.]\n", " [ 0.]\n", " [ 0.]]\n", "" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "A complete scal function as part of the LAFF library" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we proceed to develop routines that contain progressively more advanced operations, we are really going to need a scal routine that can scal rows from a matrix or columns of a matrix. \n", "\n", "This routine is part of the 'laff' library. If you do\n", "\n", "\n", "import laff\n", "\n", "\n", "then laff.scal( alpha, x ) will perform the desired scaling, when x is a (column) vector or a row vector. 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", "laff.copy( xold, x )\n", "\n", "print( 'x before scaling:' )\n", "print( x )\n", "\n", "laff.scal( alpha, x )\n", "\n", "print( 'x after scaling:' )\n", "print( x )\n", "\n", "print( 'difference between result and alpha * xold:' )\n", "print( x - alpha * xold )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Need a challenge?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In \"1.5.2 Implementing a copy routine\", we gave a complete routine that implements a copy from a row/column vector to a row/column vector, checked whether the parameters were legal, and had comments in it. If you feel up to the challenge, below write a routine scal that similarly works for row and column vectors, checks the parameters, and has appropriate comments. Be sure to test your implementation. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "def scal( alpha, x ):\n", " ### You fill in the rest!" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }