{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Transposing a matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook walks you through how to implement simple functions that transpose matrix $ A $, storing the result in matrix $ B $." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Getting started" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use some functions that are part of our laff library (of which this function will become a part) as well as some routines from the FLAME API (Application Programming Interface) that allows us to write code that closely resembles how we typeset algorithms using the FLAME notation. These functions are imported with the \"import laff as laff\" and \"import flame\" statements." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Algorithm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Transposing" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "The routine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write your
\n", " Transpose_unb_var1( A, B )
\n", "routine, using the Spark webpage and the laff.copy routine." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert code here\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Testing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's quickly test the routine by creating a 5 x 4 matrix $ A $ and a 4 x 5 matrix $ B $ and then transposing $ A $, overwriting $ B $ with the result." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy import random\n", "from numpy import matrix\n", "\n", "A = matrix( random.rand( 5,4 ) )\n", "B = matrix( random.rand( 4,5 ) )\n", "\n", "print( 'A ' )\n", "print( A )\n", "\n", "\n", "print( 'B before =' )\n", "print( B )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "Transpose_unb_var1( A, B )\n", "\n", "print( 'A ' )\n", "print( A )\n", "\n", "print( 'B after =' )\n", "print( B )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bingo, it seems to work!" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Try it yourself (Homework 3.2.5.3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, an alternative routine that accesses the matrix by rows." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the Spark webpage to generate the routine
\n", " Transpose_unb_var2( A, B ).
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert code here\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test your routine with the following" ] }, { "cell_type": "code", "collapsed": false, "input": [ "A = matrix( random.rand( 5,4 ) )\n", "B = matrix( random.rand( 4,5 ) )\n", "\n", "print( 'A ' )\n", "print( A )\n", "\n", "\n", "print( 'B before =' )\n", "print( B )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "Transpose_unb_var2( A, B )\n", "\n", "print( 'A ' )\n", "print( A )\n", "\n", "print( 'B after =' )\n", "print( B )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Watch your code in action!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy and paste the code into PictureFLAME , a webpage where you can watch your routine in action. Just cut and paste into the box. \n", "\n", "Disclaimer: we implemented a VERY simple interpreter. If you do something wrong, we cannot guarantee the results. But if you do it right, you are in for a treat.\n", "\n", "If you want to reset the problem, just click in the box into which you pasted the code and hit \"next\" again." ] } ], "metadata": {} } ] }