{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Multiplying upper triangular matrices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook helps you implement the operation $ C := U R $ where $ C, U, R \\in \\mathbb{R}^{n \\times n} $, and $ U $ and $ R $ are upper triangular. $ U $ and $ R $ are stored in the upper triangular part of numpy matrices U and R . The upper triangular part of matrix C is to be overwritten with the resulting upper triangular matrix. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we create some matrices." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "\n", "n = 5\n", "\n", "C = np.matrix( np.random.random( (n, n) ) )\n", "print( 'C = ' )\n", "print( C )\n", "\n", "Cold = np.matrix( np.zeros( (n,n ) ) )\n", "Cold = np.matrix( np.copy( C ) ) # an alternative way of doing a \"hard\" copy, in this case of a matrix\n", " \n", "U = np.matrix( np.random.random( (n, n) ) )\n", "print( 'U = ' )\n", "print( U )\n", "\n", "R = np.matrix( np.random.random( (n, n) ) )\n", "print( 'R = ' )\n", "print( R )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "

The algorithm

\n", "\n", "(The homework was to propose the updates to $ C $.)\n", "\n", "\"Upper\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

The routine Trtrmm_uu_unb_var1( U, R, C )

\n", "\n", "This routine computes $ C := U R + C $. The \"\\_uu\\_\" means that $ U $ and $ R $ are upper triangular matrices (which means $ C $ is too). However, the lower triangular parts of numpy matrices U , R , and C are not to be \"touched\".\n", " \n", "The specific laff function you will want to use is some subset of\n", "\n", "\n", " Hint: If you multiply with $ U_{00} $, you will want to use np.triu( U00 ) to make sure you don't compute with the nonzeroes below the diagonal.\n", "\n", "Use the Spark webpage to generate a code skeleton. (Make sure you adjust the name of the routine.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert code here\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "C = np.matrix( np.copy( Cold ) ) # restore C \n", "\n", "Trtrmm_uu_unb_var1( U, R, C )\n", "\n", "# compute it using numpy *. This is a little complex, since we want to make sure we\n", "# don't change the lower triangular part of C\n", "# Cref = np.tril( Cold, -1 ) + np.triu( U ) * np.triu( R )\n", "Cref = np.triu( U ) * np.triu( R ) + np.tril( Cold, -1 )\n", "print( 'C - Cref' )\n", "print( C - Cref )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In theory, you should get a matrix of (approximately) all zeroes." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Watch the algorithm at work!" ] }, { "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": {} } ] }