{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "The Set_to_zero routine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook walks you through how to implement a simple function that sets all elements of a given matrix, \n", "stored in numpy matrix A, to zero." ] }, { "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": [ "\"Set" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "The routine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The specific laff function we will use is laff.zerov which, when given a row or column vector (stored as a 1 x n or n x 1 matrix) will zero that vector. The vectors to be zeroed will actually be parts of the matrix A that we overwrite with zeroes. \n", "\n", "The flame functions should be self-explanatory if you put the below next to the algorithm for setting a matrix to the zero, expressed using the FLAME notation.\n", "\n", "It is the merge_1x2 routine that does require an explanation. The problem is that we want to overwrite A with the result. That routine takes AT, AB, and copies them back into A.\n", "\n", "Follow along with the video, using the Spark webpage ." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import flame\n", "import laff as laff\n", "\n", "def Set_to_zero_unb_var1(A):\n", "\n", " AL, AR = flame.part_1x2(A, \\\n", " 0, 'LEFT')\n", "\n", " while AL.shape[1] < A.shape[1]:\n", "\n", " A0, a1, A2 = flame.repart_1x2_to_1x3(AL, AR, \\\n", " 1, 'RIGHT')\n", "\n", " #------------------------------------------------------------#\n", "\n", " laff.zerov( a1 )\n", "\n", " #------------------------------------------------------------#\n", "\n", " AL, AR = flame.cont_with_1x3_to_1x2(A0, a1, A2, \\\n", " 'LEFT')\n", "\n", " flame.merge_1x2(AL, AR, A)\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Testing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's quickly test the routine by creating a 5 x 5 matrix and then setting it to zero." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy import random\n", "from numpy import matrix\n", "\n", "A = matrix( random.rand( 5,4 ) )\n", "\n", "print( 'A before =' )\n", "print( A )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A before =\n", "[[ 0.7087534 0.68378286 0.92547683 0.87258882]\n", " [ 0.52904249 0.86452284 0.93831485 0.55558695]\n", " [ 0.61899443 0.64463657 0.30172828 0.01368121]\n", " [ 0.27673708 0.50489102 0.68849698 0.24154842]\n", " [ 0.16113079 0.08470225 0.73786262 0.82764949]]\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "Set_to_zero_unb_var1( A )\n", "\n", "print( 'A after =' )\n", "print( A )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A after =\n", "[[ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]]\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bingo, it seems to work!" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Try it yourself (homework 3.2.1.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, one could alternatively set a matrix to zero by rows." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the Spark webpage to generate the function Set_to_zero_unb_var2( A ) that overwrites A with zeroes one row at a time." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Programmed by: Name of author\n", "# Email of author\n", "\n", "import flame\n", "import laff as laff\n", "\n", "def Set_to_zero_unb_var2(A):\n", "\n", " AT, \\\n", " AB = flame.part_2x1(A, \\\n", " 0, 'TOP')\n", "\n", " while AT.shape[0] < A.shape[0]:\n", "\n", " A0, \\\n", " a1t, \\\n", " A2 = flame.repart_2x1_to_3x1(AT, \\\n", " AB, \\\n", " 1, 'BOTTOM')\n", "\n", " #------------------------------------------------------------#\n", "\n", " laff.zerov( a1t )\n", " \n", " #------------------------------------------------------------#\n", "\n", " AT, \\\n", " AB = flame.cont_with_3x1_to_2x1(A0, \\\n", " a1t, \\\n", " A2, \\\n", " 'TOP')\n", "\n", " flame.merge_2x1(AT, \\\n", " AB, A)\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test your routine with the following" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy import random\n", "from numpy import matrix\n", "\n", "A = matrix( random.rand( 5,4 ) )\n", "\n", "print( 'A before =' )\n", "print( A )\n", "\n", "Set_to_zero_unb_var2( A )\n", "print( 'A after =' )\n", "print( A )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A before =\n", "[[ 0.22716597 0.15086488 0.80769447 0.46671279]\n", " [ 0.48830478 0.00109018 0.95598695 0.56900545]\n", " [ 0.85862579 0.66929742 0.16037345 0.70875834]\n", " [ 0.98079404 0.64614272 0.10924422 0.55992507]\n", " [ 0.75264017 0.2145888 0.23320207 0.14315755]]\n", "A after =\n", "[[ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]]\n" ] } ], "prompt_number": 5 }, { "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": {} } ] }