{ "metadata": { "language": "Julia", "name": "18.06 Sep 9 2013 Lecture 3" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": "Matrix Multiply and Inverses" }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": "Many ways to multiply matrices" }, { "cell_type": "code", "collapsed": false, "input": "A=rand(1:3,4,6)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": "4x6 Array{Int64,2}:\n 3 3 1 3 3 1\n 2 2 2 3 1 3\n 3 1 2 2 1 3\n 3 3 3 2 1 2" } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": "B=rand(1:3,7,8)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": "7x8 Array{Int64,2}:\n 3 3 3 3 3 1 1 2\n 1 1 1 1 3 2 3 1\n 3 2 3 2 2 3 2 2\n 3 3 1 2 3 2 3 1\n 3 3 3 1 3 2 3 2\n 2 1 3 2 2 1 2 3\n 2 3 1 1 1 2 1 2" } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": "A*B # matmul only works if A is m x n and B is n x p", "language": "python", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "*: argument shapes do not match\nat In[6]:1", "output_type": "pyerr", "traceback": [ "*: argument shapes do not match\nat In[6]:1", " in generic_matmatmul at linalg/matmul.jl:409", " in generic_matmatmul at linalg/matmul.jl:398", " in * at linalg/matmul.jl:391" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": "B=rand(1:3,6,8)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": "6x8 Array{Int64,2}:\n 2 2 2 2 2 2 3 2\n 3 3 2 2 1 1 3 2\n 1 1 2 1 2 1 2 1\n 3 2 2 2 1 1 2 2\n 1 1 1 1 2 2 2 2\n 1 2 2 1 3 1 3 2" } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": "A*B", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": "4x8 Array{Int64,2}:\n 29 27 25 23 23 20 35 27\n 25 25 25 20 24 16 33 24\n 21 22 23 18 24 16 31 22\n 27 27 27 22 25 18 36 25" } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": "A[3,:]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": "1x6 Array{Int64,2}:\n 3 1 2 2 1 3" } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": "B[:,4]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": "6-element Array{Int64,1}:\n 2\n 2\n 1\n 2\n 1\n 1" } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": "A[3,:]*B[:,4]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": "1-element Array{Int64,1}:\n 18" } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": "Linear Combinations of Columns of A" }, { "cell_type": "code", "collapsed": false, "input": "[A*B[:,1] A*B[:,2] A*B[:,3] A*B[:,4] A*B[:,5] A*B[:,6] A*B[:,7] A*B[:,8]]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": "4x8 Array{Int64,2}:\n 29 27 25 23 23 20 35 27\n 25 25 25 20 24 16 33 24\n 21 22 23 18 24 16 31 22\n 27 27 27 22 25 18 36 25" } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": "A*B # Every column of A*B is some linear combination of columns of A", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": "4x8 Array{Int64,2}:\n 29 27 25 23 23 20 35 27\n 25 25 25 20 24 16 33 24\n 21 22 23 18 24 16 31 22\n 27 27 27 22 25 18 36 25" } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": "Linear Combinations of Rows of B" }, { "cell_type": "code", "collapsed": false, "input": "[ A[1,:]*B; A[2,:]*B; A[3,:]*B; A[4,:]*B]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": "4x8 Array{Int64,2}:\n 29 27 25 23 23 20 35 27\n 25 25 25 20 24 16 33 24\n 21 22 23 18 24 16 31 22\n 27 27 27 22 25 18 36 25" } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": "A[1,:] % 3 x first row of B + 3 * 2nd row + 1* 3rd row + etc.", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": "1x6 Array{Int64,2}:\n 3 3 1 3 3 1" } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": "Outer Product Matrix Multiply" }, { "cell_type": "code", "collapsed": false, "input": "v=rand(5,1); w=rand(1,5)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": "1x5 Array{Float64,2}:\n 0.648415 0.107518 0.49727 0.0484907 0.148389" } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": "v*w;w*v", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": "1x1 Array{Float64,2}:\n 0.341224" } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": "[1:12]*[1:12]' # Outer Products (recognize the 3rd grade mult table)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": "12x12 Array{Int64,2}:\n 1 2 3 4 5 6 7 8 9 10 11 12\n 2 4 6 8 10 12 14 16 18 20 22 24\n 3 6 9 12 15 18 21 24 27 30 33 36\n 4 8 12 16 20 24 28 32 36 40 44 48\n 5 10 15 20 25 30 35 40 45 50 55 60\n 6 12 18 24 30 36 42 48 54 60 66 72\n 7 14 21 28 35 42 49 56 63 70 77 84\n 8 16 24 32 40 48 56 64 72 80 88 96\n 9 18 27 36 45 54 63 72 81 90 99 108\n 10 20 30 40 50 60 70 80 90 100 110 120\n 11 22 33 44 55 66 77 88 99 110 121 132\n 12 24 36 48 60 72 84 96 108 120 132 144" } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": "A*B", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": "4x8 Array{Int64,2}:\n 29 27 25 23 23 20 35 27\n 25 25 25 20 24 16 33 24\n 21 22 23 18 24 16 31 22\n 27 27 27 22 25 18 36 25" } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": "A[:,1]*B[1,:] + A[:,2]*B[2,:] + A[:,3]*B[3,:] + A[:,4]*B[4,:]+A[:,5]*B[5,:]+A[:,6]*B[6,:]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": "4x8 Array{Int64,2}:\n 29 27 25 23 23 20 35 27\n 25 25 25 20 24 16 33 24\n 21 22 23 18 24 16 31 22\n 27 27 27 22 25 18 36 25" } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": "A*B\n", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 43, "text": "4x8 Array{Int64,2}:\n 29 27 25 23 23 20 35 27\n 25 25 25 20 24 16 33 24\n 21 22 23 18 24 16 31 22\n 27 27 27 22 25 18 36 25" } ], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": "A", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 44, "text": "4x6 Array{Int64,2}:\n 3 3 1 3 3 1\n 2 2 2 3 1 3\n 3 1 2 2 1 3\n 3 3 3 2 1 2" } ], "prompt_number": 44 }, { "cell_type": "code", "collapsed": false, "input": "B", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": "6x8 Array{Int64,2}:\n 2 2 2 2 2 2 3 2\n 3 3 2 2 1 1 3 2\n 1 1 2 1 2 1 2 1\n 3 2 2 2 1 1 2 2\n 1 1 1 1 2 2 2 2\n 1 2 2 1 3 1 3 2" } ], "prompt_number": 45 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": "Block Matrix Multiply" }, { "cell_type": "code", "collapsed": false, "input": "A[1:2,1:2]*B[1:2,1:2] + A[1:2,3:4]*B[3:4,1:2]+ A[1:2,5:6]*B[5:6,1:2]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 49, "text": "2x2 Array{Int64,2}:\n 29 27\n 25 25" } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": "A*B\n", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 50, "text": "4x8 Array{Int64,2}:\n 29 27 25 23 23 20 35 27\n 25 25 25 20 24 16 33 24\n 21 22 23 18 24 16 31 22\n 27 27 27 22 25 18 36 25" } ], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": "A[3:4,1:2]*B[1:2,1:2] + A[3:4,3:4]*B[3:4,1:2]+ A[3:4,5:6]*B[5:6,1:2]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 51, "text": "2x2 Array{Int64,2}:\n 21 22\n 27 27" } ], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": "A[3:4,1:2]*B[1:2,5:6] + A[3:4,3:4]*B[3:4,5:6]+ A[3:4,5:6]*B[5:6,5:6\n ]", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 52, "text": "2x2 Array{Int64,2}:\n 24 16\n 25 18" } ], "prompt_number": 52 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": "Matrix Inverses" }, { "cell_type": "code", "collapsed": false, "input": "A= rand(3,4)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 53, "text": "3x4 Array{Float64,2}:\n 0.973557 0.278488 0.300054 0.723139\n 0.222204 0.860992 0.562578 0.846122\n 0.130402 0.852375 0.108227 0.26576 " } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": "eye(4)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 54, "text": "4x4 Array{Float64,2}:\n 1.0 0.0 0.0 0.0\n 0.0 1.0 0.0 0.0\n 0.0 0.0 1.0 0.0\n 0.0 0.0 0.0 1.0" } ], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": "A=rand(1:3,3,3)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 72, "text": "3x3 Array{Int64,2}:\n 1 1 2\n 1 1 2\n 1 1 1" } ], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": "inv(A)", "language": "python", "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "SingularException(2)\nat In[73]:1", "output_type": "pyerr", "traceback": [ "SingularException(2)\nat In[73]:1", " in inv at linalg/factorization.jl:231", " in inv at linalg/dense.jl:425" ] } ], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": "B=ans ", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 59, "text": "3x3 Array{Float64,2}:\n -0.0 -0.333333 0.666667\n -1.0 1.66667 -0.333333\n 1.0 -1.0 0.0 " } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": "A*B", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 60, "text": "3x3 Array{Float64,2}:\n 1.0 -4.44089e-16 1.11022e-16\n 0.0 1.0 1.11022e-16\n 0.0 -1.11022e-16 1.0 " } ], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": "B*A", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 61, "text": "3x3 Array{Float64,2}:\n 1.0 0.0 0.0 \n 0.0 1.0 -2.22045e-16\n 0.0 0.0 1.0 " } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": "2.0^(-52)", "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 63, "text": "2.220446049250313e-16" } ], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": "", "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }