{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Programming without indices (dot product)" ] }, { "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", "

In this notebook, we show how the FLAME notation (the notation in which vectors and/or matrices are partitioned into regions) can be leveraged to implement linear algebra operations without using indices (which are the root of all evil in programming...).

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's start by importing numpy and creating vectors $ x = \\left( \\begin{array}{r} 1 \\\\ 2 \\\\ 3 \\end{array} \\right) $ and $ y = \\left( \\begin{array}{r} -1 \\\\ 0 \\\\ -2 \\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 two two-dimensional matrices of only one column each. \n", "# In the future we will also think of (column) \n", "# vectors as matrices with only one column.\n", "x = np.matrix( '1.;2.;3.' )\n", "print( 'x = ' )\n", "print( x )\n", "\n", "y = np.matrix( '-1.;0.;-2.' )\n", "print( 'y = ' )\n", "print( y )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Dot as a simple routine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "Here is a simple routine for computing $ {\\rm dot}( x, y ) = x^T y $:\n", "

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def dot( x, y ):\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", " alpha = 0.0\n", " \n", " for i in range( m ):\n", " alpha = x[ i,0 ] * y[ i,0 ] + alpha\n", " \n", " return alpha" ], "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, execute" ] }, { "cell_type": "code", "collapsed": false, "input": [ "alpha = 0.\n", "\n", "alpha = dot( x, y )\n", "\n", "print( 'alpha' )\n", "print( alpha )\n", "\n", "print( 'Difference between alpha and np.transpose(x) * y:' )\n", "alpha_reference = np.transpose(x) * y\n", "\n", "print( alpha - alpha_reference[0,0] )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "An implementation with the FLAMEPy Application Programming Interface (API)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now show how to implement this same routine using the FLAMEPy API.\n", "\n", "Start by visiting the Spark webpage. Follow along with the video and paste the resulting code below. Then follow along with the video and add the appropriate commands. (You may even want to bookmark this page).\n", "\n", "Here is the algorithm as presented in Unit 1.6.2. \n", "\"some_text\"\n", "\n", "In the video for Unit 1.6.3, we discuss how to translate this into Python code using the FLAMEPy API. Follow these instructions, insert the resulting code below." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Insert Code here\n", "\n", "\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import laff\n", "\n", "alpha = np.matrix( '-2.0' ) # the way we are going to program, scalars, vectors, and matrices are \n", " # all just matrices. So, alpha here is a 1 x 1 matrix, which we \n", " # initialize to some random number, in this case -2.0.\n", "\n", "dot_unb( x, y, alpha ) # Takes x, y, and alpha as an input, and then updates alpha with the \n", " # result of dot( x, y ). Notice that the contents of variable alpha\n", " # are updated. This only works if alpha is passed in as an array \n", " # (a matrix in our case)\n", "\n", "print( 'alpha' )\n", "print( alpha )\n", "\n", "print( 'compare alpha to np.transpose(x) * y:' )\n", "alpha_reference = np.transpose(x) * y\n", "\n", "print( alpha - alpha_reference[0,0] )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output should be:\n", "\n", "alpha\n", "[[-7.]]\n", "compare alpha to np.transpose(x) * y:\n", "[[0.]]\n", "" ] } ], "metadata": {} } ] }