{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "The Set_to_diagonal_matrix routine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook walks you through how to implement simple functions that set the diagonal elements of a square matrix to the components of a given vector." ] }, { "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
Set_to_diagonal_matrix_unb_var1( d, A ) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This routine sets the diagonal elements of $ A $ to the components of vector $ d$, and sets the off-diagonal elements to zero.\n", "\n", "The specific laff functions we will use are \n", "\n", "\n", "Use the Spark webpage ." ] }, { "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_diagonal_matrix_unb_var1(d, A):\n", "\n", " dT, \\\n", " dB = flame.part_2x1(d, \\\n", " 0, 'TOP')\n", "\n", " ATL, ATR, \\\n", " ABL, ABR = flame.part_2x2(A, \\\n", " 0, 0, 'TL')\n", "\n", " while dT.shape[0] < d.shape[0]:\n", "\n", " d0, \\\n", " delta1, \\\n", " d2 = flame.repart_2x1_to_3x1(dT, \\\n", " dB, \\\n", " 1, 'BOTTOM')\n", "\n", " A00, a01, A02, \\\n", " a10t, alpha11, a12t, \\\n", " A20, a21, A22 = flame.repart_2x2_to_3x3(ATL, ATR, \\\n", " ABL, ABR, \\\n", " 1, 1, 'BR')\n", "\n", " #------------------------------------------------------------#\n", "\n", " laff.zerov( a01 )\n", " laff.copy( delta1, alpha11 )\n", " laff.zerov( a21 )\n", "\n", " #------------------------------------------------------------#\n", "\n", " dT, \\\n", " dB = flame.cont_with_3x1_to_2x1(d0, \\\n", " delta1, \\\n", " d2, \\\n", " 'TOP')\n", "\n", " ATL, ATR, \\\n", " ABL, ABR = flame.cont_with_3x3_to_2x2(A00, a01, A02, \\\n", " a10t, alpha11, a12t, \\\n", " A20, a21, A22, \\\n", " 'TL')\n", "\n", " flame.merge_2x2(ATL, ATR, \\\n", " ABL, ABR, A)\n", "\n", "\n", "\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 5 matrix and vector of size 5 and then setting the matrix to the diagonal matrix with the components of the vector on its diagonal" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy import random\n", "from numpy import matrix\n", "\n", "A = matrix( random.rand( 5,5 ) )\n", "d = matrix( random.rand( 5,1 ) )\n", "\n", "print( 'A before =' )\n", "print( A )\n", "\n", "print( 'd before =' )\n", "print( d )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "Set_to_diagonal_matrix_unb_var1( d, A )\n", "\n", "print( 'A after =' )\n", "print( A )" ], "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.3.3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, one could alternatively sets the matrix to the diagonal matrix by rows." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the Spark webpage to generate the function Set_to_diagonal_matrix_unb_var2( d, A ) that overwrites A 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_diagonal_matrix_unb_var2(d, A):\n", "\n", " dT, \\\n", " dB = flame.part_2x1(d, \\\n", " 0, 'TOP')\n", "\n", " ATL, ATR, \\\n", " ABL, ABR = flame.part_2x2(A, \\\n", " 0, 0, 'TL')\n", "\n", " while dT.shape[0] < d.shape[0]:\n", "\n", " d0, \\\n", " delta1, \\\n", " d2 = flame.repart_2x1_to_3x1(dT, \\\n", " dB, \\\n", " 1, 'BOTTOM')\n", "\n", " A00, a01, A02, \\\n", " a10t, alpha11, a12t, \\\n", " A20, a21, A22 = flame.repart_2x2_to_3x3(ATL, ATR, \\\n", " ABL, ABR, \\\n", " 1, 1, 'BR')\n", "\n", " #------------------------------------------------------------#\n", "\n", " laff.zerov( a10t )\n", " laff.copy( delta1, alpha11 )\n", " laff.zerov( a12t )\n", "\n", " #------------------------------------------------------------#\n", "\n", " dT, \\\n", " dB = flame.cont_with_3x1_to_2x1(d0, \\\n", " delta1, \\\n", " d2, \\\n", " 'TOP')\n", "\n", " ATL, ATR, \\\n", " ABL, ABR = flame.cont_with_3x3_to_2x2(A00, a01, A02, \\\n", " a10t, alpha11, a12t, \\\n", " A20, a21, A22, \\\n", " 'TL')\n", "\n", " flame.merge_2x2(ATL, ATR, \\\n", " ABL, ABR, A)\n", "\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "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,5 ) )\n", "d = matrix( random.rand( 5,1 ) )\n", "\n", "print( 'A before =' )\n", "print( A )\n", "\n", "print( 'd before =' )\n", "print( d )\n", "\n", "Set_to_diagonal_matrix_unb_var2( d, A )\n", "\n", "print( 'A after =' )\n", "print( A )" ], "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." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }