{ "metadata": { "language": "Julia", "name": "Lecture04.ipynb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": "Gauss Jordan, LU" }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "A Bit More about Blocks -- Consider 1x1 by 1x2" }, { "cell_type": "code", "collapsed": false, "input": "2*[3 4]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 195, "text": "1x2 Array{Int64,2}:\n 6 8" } ], "prompt_number": 195 }, { "cell_type": "code", "collapsed": false, "input": "[2*3 2*4]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 196, "text": "1x2 Array{Int64,2}:\n 6 8" } ], "prompt_number": 196 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Blocks work the same way" }, { "cell_type": "code", "collapsed": false, "input": "A=rand(1:3,2,4); B=rand(1:3,4,3); C=rand(1:3,4,2);\nprintln( A*[B C])\nprintln( [A*B A*C])", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "15\t16\t13\t15\t16\n15\t16\t13\t17\t14\n\n" }, { "output_type": "stream", "stream": "stdout", "text": "15\t16\t13\t15\t16\n15\t16\t13\t17\t14\n\n" } ], "prompt_number": 197 }, { "cell_type": "raw", "metadata": {}, "source": "Question: What dimensions of A and B and C allow this to work\n \nThere are a number of ways to check that with the right dimensions A[B C]=[AB AC]\n1. The highest level way is the block view\n Block view = if blocks conform then block matmul always works like elementwise matmul\n2. Also worthwhile to consider the column view\n The columns of A[B C] are all the matrix-vector products of A*the columns of [B C]\n Why is this the same as [AB AC], the columns of AB concatanated with the columns of AC?\n3. You might try other views of matmul too\n You might think some are easier on the brain than others, and also some views while\n less easy on the brain, are more powerful. You get to decide what you like best.\n " }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Gauss Jordan in high level block form M[A I]=[I M] so M is a left inverse (algorithm hidden!)" }, { "cell_type": "code", "collapsed": false, "input": "rand3(n)=rand(1:3,n,n); # define rand3 (A little shorthand)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 198, "text": "# methods for generic function rand3\nrand3(n) at In[198]:1" } ], "prompt_number": 198 }, { "cell_type": "code", "collapsed": false, "input": "I=eye(3); A=rand3(3); M=inv(A); # Might be singular, careful (single exception or large numbers indicate singularity)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 200, "text": "3x3 Array{Float64,2}:\n 0.4 -0.2 0.0\n -0.6 -0.2 1.0\n 0.4 0.8 -1.0" } ], "prompt_number": 200 }, { "cell_type": "code", "collapsed": false, "input": "I=eye(3);\nshowcompact(\n M*[A I] ); println('\\n')\nshowcompact(\n [ I M] \n )", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "3x6 Array{Float64,2}:" }, { "output_type": "stream", "stream": "stdout", "text": "\n 1.0 0.0 0.0 0.4 -0.2 0.0\n 0.0 1.0 0.0 -0.6 -0.2 1.0\n 0.0 0.0 1.0 0.4 0.8 -1.0\n\n3x6 Array{Float64,2}:\n 1.0 0.0 0.0 0.4 -0.2 0.0\n 0.0 1.0 0.0 -0.6 -0.2 1.0\n 0.0 0.0 1.0 0.4 0.8 -1.0" } ], "prompt_number": 209 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Gauss Jordan details -- do this with pencil and paper once or twice" }, { "cell_type": "code", "collapsed": false, "input": "Z=[ A I]\n", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 210, "text": "3x6 Array{Float64,2}:\n 3.0 1.0 1.0 1.0 0.0 0.0\n 1.0 2.0 2.0 0.0 1.0 0.0\n 2.0 2.0 1.0 0.0 0.0 1.0" } ], "prompt_number": 210 }, { "cell_type": "code", "collapsed": false, "input": "Z[2,:] -= (Z[2,1]/Z[1,1])*Z[1,:];Z # Method 1. (Easier for Programming) Elimination via row operations", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 211, "text": "3x6 Array{Float64,2}:\n 3.0 1.0 1.0 1.0 0.0 0.0\n 0.0 1.66667 1.66667 -0.333333 1.0 0.0\n 2.0 2.0 1.0 0.0 0.0 1.0" } ], "prompt_number": 211 }, { "cell_type": "code", "collapsed": false, "input": "Z=[ A I];", "language": "python", "metadata": {}, "outputs": [], "prompt_number": 211 }, { "cell_type": "code", "collapsed": false, "input": "E=copy(I); E[2,1]=-Z[2,1]/Z[1,1];E # Method 2: Create an elimination matrix with the negative multiplier in the (2,1) position, say", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 212, "text": "3x3 Array{Float64,2}:\n 1.0 0.0 0.0\n -0.333333 1.0 0.0\n 0.0 0.0 1.0" } ], "prompt_number": 212 }, { "cell_type": "code", "collapsed": false, "input": "Z=E*Z", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 213, "text": "3x6 Array{Float64,2}:\n 3.0 1.0 1.0 1.0 0.0 0.0\n 0.0 1.66667 1.66667 -0.333333 1.0 0.0\n 2.0 2.0 1.0 0.0 0.0 1.0" } ], "prompt_number": 213 }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": "Continue with method 1" }, { "cell_type": "code", "collapsed": false, "input": "Z[3,:] -= (Z[3,1]/Z[1,1])*Z[1,:];Z", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 214, "text": "3x6 Array{Float64,2}:\n 3.0 1.0 1.0 1.0 0.0 0.0\n 0.0 1.66667 1.66667 -0.333333 1.0 0.0\n 0.0 1.33333 0.333333 -0.666667 0.0 1.0" } ], "prompt_number": 214 }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": "Column 2" }, { "cell_type": "code", "collapsed": false, "input": "Z[1,:] -= (Z[1,2]/Z[2,2])*Z[2,:];Z", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 215, "text": "3x6 Array{Float64,2}:\n 3.0 0.0 0.0 1.2 -0.6 0.0\n 0.0 1.66667 1.66667 -0.333333 1.0 0.0\n 0.0 1.33333 0.333333 -0.666667 0.0 1.0" } ], "prompt_number": 215 }, { "cell_type": "code", "collapsed": false, "input": "Z[3,:] -= (Z[3,2]/Z[2,2])*Z[2,:];Z", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 216, "text": "3x6 Array{Float64,2}:\n 3.0 0.0 0.0 1.2 -0.6 0.0\n 0.0 1.66667 1.66667 -0.333333 1.0 0.0\n 0.0 0.0 -1.0 -0.4 -0.8 1.0" } ], "prompt_number": 216 }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": "Column 3" }, { "cell_type": "code", "collapsed": false, "input": "Z[1,:] -= (Z[1,3]/Z[3,3])*Z[3,:];Z", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 217, "text": "3x6 Array{Float64,2}:\n 3.0 0.0 0.0 1.2 -0.6 0.0\n 0.0 1.66667 1.66667 -0.333333 1.0 0.0\n 0.0 0.0 -1.0 -0.4 -0.8 1.0" } ], "prompt_number": 217 }, { "cell_type": "code", "collapsed": false, "input": "Z[2,:] -= (Z[2,3]/Z[3,3])*Z[3,:];Z", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 218, "text": "3x6 Array{Float64,2}:\n 3.0 0.0 0.0 1.2 -0.6 0.0 \n 0.0 1.66667 0.0 -1.0 -0.333333 1.66667\n 0.0 0.0 -1.0 -0.4 -0.8 1.0 " } ], "prompt_number": 218 }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": "Divide out the pivots" }, { "cell_type": "code", "collapsed": false, "input": "Z[1,:]/=Z[1,1];Z", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 219, "text": "3x6 Array{Float64,2}:\n 1.0 0.0 0.0 0.4 -0.2 0.0 \n 0.0 1.66667 0.0 -1.0 -0.333333 1.66667\n 0.0 0.0 -1.0 -0.4 -0.8 1.0 " } ], "prompt_number": 219 }, { "cell_type": "code", "collapsed": false, "input": "Z[2,:]/=Z[2,2];Z", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 220, "text": "3x6 Array{Float64,2}:\n 1.0 0.0 0.0 0.4 -0.2 0.0\n 0.0 1.0 0.0 -0.6 -0.2 1.0\n 0.0 0.0 -1.0 -0.4 -0.8 1.0" } ], "prompt_number": 220 }, { "cell_type": "code", "collapsed": false, "input": "Z[3,:]/=Z[3,3];Z", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 221, "text": "3x6 Array{Float64,2}:\n 1.0 0.0 0.0 0.4 -0.2 0.0\n 0.0 1.0 0.0 -0.6 -0.2 1.0\n -0.0 -0.0 1.0 0.4 0.8 -1.0" } ], "prompt_number": 221 }, { "cell_type": "code", "collapsed": false, "input": "inv(A)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 222, "text": "3x3 Array{Float64,2}:\n 0.4 -0.2 0.0\n -0.6 -0.2 1.0\n 0.4 0.8 -1.0" } ], "prompt_number": 222 }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": "We dit it!" }, { "cell_type": "code", "collapsed": false, "input": "", "language": "python", "metadata": {}, "outputs": [], "prompt_number": 223 }, { "cell_type": "raw", "metadata": {}, "source": "Point of view: we solved Ax =[e1 e2 e3] simultaneously giving a right inverse\n but the block view shows us it is also a left inverse\n hence a left invese is a right inverse" }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": "Elimination = Factorization = LU" }, { "cell_type": "code", "collapsed": false, "input": "A=[ 6.0 5 10; 8 13 8; 3 14 7]; Aoriginal=copy(A);", "language": "python", "metadata": {}, "outputs": [], "prompt_number": 276 }, { "cell_type": "code", "collapsed": false, "input": "A[2,:]-= (4/3)*A[1,:]\nA", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 262, "text": "3x3 Array{Float64,2}:\n 6.0 5.0 10.0 \n 0.0 6.33333 -5.33333\n 3.0 14.0 7.0 " } ], "prompt_number": 262 }, { "cell_type": "code", "collapsed": false, "input": "A[3,:] -= ( 0.5 )*A[1,:]\nA", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 263, "text": "3x3 Array{Float64,2}:\n 6.0 5.0 10.0 \n 0.0 6.33333 -5.33333\n 0.0 11.5 2.0 " } ], "prompt_number": 263 }, { "cell_type": "code", "collapsed": false, "input": "A[3,:]-= (11.5/6.333333 )*A[2,:];A", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 264, "text": "3x3 Array{Float64,2}:\n 6.0 5.0 10.0 \n 0.0 6.33333 -5.33333\n 0.0 -6.05263e-7 11.6842 " } ], "prompt_number": 264 }, { "cell_type": "code", "collapsed": false, "input": "U=ans", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 265, "text": "3x3 Array{Float64,2}:\n 6.0 5.0 10.0 \n 0.0 6.33333 -5.33333\n 0.0 -6.05263e-7 11.6842 " } ], "prompt_number": 265 }, { "cell_type": "code", "collapsed": false, "input": "(11.5/6.333333 )", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 266, "text": "1.8157895692520827" } ], "prompt_number": 266 }, { "cell_type": "code", "collapsed": false, "input": "69/38\n\n\n\n\n", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 267, "text": "1.8157894736842106" } ], "prompt_number": 267 }, { "cell_type": "code", "collapsed": false, "input": "U", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 268, "text": "3x3 Array{Float64,2}:\n 6.0 5.0 10.0 \n 0.0 6.33333 -5.33333\n 0.0 -6.05263e-7 11.6842 " } ], "prompt_number": 268 }, { "cell_type": "code", "collapsed": false, "input": "L=eye(3)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 269, "text": "3x3 Array{Float64,2}:\n 1.0 0.0 0.0\n 0.0 1.0 0.0\n 0.0 0.0 1.0" } ], "prompt_number": 269 }, { "cell_type": "code", "collapsed": false, "input": "L[2,1]=4/3; L[3,1]=0.5; L[3,2]=69/38;L", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 270, "text": "3x3 Array{Float64,2}:\n 1.0 0.0 0.0\n 1.33333 1.0 0.0\n 0.5 1.81579 1.0" } ], "prompt_number": 270 }, { "cell_type": "code", "collapsed": false, "input": "L*U", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 271, "text": "3x3 Array{Float64,2}:\n 6.0 5.0 10.0\n 8.0 13.0 8.0\n 3.0 14.0 7.0" } ], "prompt_number": 271 }, { "cell_type": "code", "collapsed": false, "input": "Aoriginal", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 277, "text": "3x3 Array{Float64,2}:\n 6.0 5.0 10.0\n 8.0 13.0 8.0\n 3.0 14.0 7.0" } ], "prompt_number": 277 }, { "cell_type": "code", "collapsed": false, "input": "E", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 258, "text": "3x3 Array{Float64,2}:\n 1.0 0.0 0.0\n -0.333333 1.0 0.0\n 0.0 0.0 1.0" } ], "prompt_number": 258 }, { "cell_type": "code", "collapsed": false, "input": "inv(E)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 237, "text": "3x3 Array{Float64,2}:\n 1.0 0.0 0.0\n 0.333333 1.0 0.0\n -0.0 -0.0 1.0" } ], "prompt_number": 237 }, { "cell_type": "code", "collapsed": false, "input": "M=rand(3,3)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 238, "text": "3x3 Array{Float64,2}:\n 0.0385084 0.992836 0.113299\n 0.399386 0.757624 0.419412\n 0.428451 0.0878158 0.299829" } ], "prompt_number": 238 }, { "cell_type": "code", "collapsed": false, "input": "E*M", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 239, "text": "3x3 Array{Float64,2}:\n 0.0385084 0.992836 0.113299\n 0.38655 0.426679 0.381646\n 0.428451 0.0878158 0.299829" } ], "prompt_number": 239 }, { "cell_type": "code", "collapsed": false, "input": "inv(E)* (E*M)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 240, "text": "3x3 Array{Float64,2}:\n 0.0385084 0.992836 0.113299\n 0.399386 0.757624 0.419412\n 0.428451 0.0878158 0.299829" } ], "prompt_number": 240 }, { "cell_type": "code", "collapsed": false, "input": "", "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }