{"nbformat": 3, "worksheets": [{"metadata": {}, "cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# Gaussian elimination with elimination matrices"]}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["import numpy as np\n", "import numpy.linalg as la"], "outputs": [], "prompt_number": 56, "collapsed": false}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["n = 3\n", "\n", "np.random.seed(15)\n", "A = np.round(5*np.random.randn(n, n))\n", "\n", "A"], "outputs": [{"output_type": "pyout", "prompt_number": 57, "metadata": {}, "text": ["array([[-2., 2., -1.],\n", " [-3., 1., -9.],\n", " [-5., -5., -2.]])"]}], "prompt_number": 57, "collapsed": false}, {"cell_type": "markdown", "metadata": {}, "source": ["`U` is the copy of `A` that we'll modify:"]}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["U = A.copy()"], "outputs": [], "prompt_number": 58, "collapsed": false}, {"cell_type": "markdown", "metadata": {}, "source": ["Now eliminate `U[1,0]`:"]}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["M1 = np.eye(n)\n", "M1[1,0] = -U[1,0]/U[0,0]\n", "M1"], "outputs": [{"output_type": "pyout", "prompt_number": 59, "metadata": {}, "text": ["array([[ 1. , 0. , 0. ],\n", " [-1.5, 1. , 0. ],\n", " [ 0. , 0. , 1. ]])"]}], "prompt_number": 59, "collapsed": false}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["U = M1.dot(U)\n", "U"], "outputs": [{"output_type": "pyout", "prompt_number": 60, "metadata": {}, "text": ["array([[-2. , 2. , -1. ],\n", " [ 0. , -2. , -7.5],\n", " [-5. , -5. , -2. ]])"]}], "prompt_number": 60, "collapsed": false}, {"cell_type": "markdown", "metadata": {}, "source": ["Now eliminate `U[2,0]`:"]}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["M2 = np.eye(n)\n", "M2[2,0] = -U[2,0]/U[0,0]"], "outputs": [], "prompt_number": 61, "collapsed": false}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["U = np.dot(M2, U)\n", "U"], "outputs": [{"output_type": "pyout", "prompt_number": 62, "metadata": {}, "text": ["array([[ -2. , 2. , -1. ],\n", " [ 0. , -2. , -7.5],\n", " [ 0. , -10. , 0.5]])"]}], "prompt_number": 62, "collapsed": false}, {"cell_type": "markdown", "metadata": {}, "source": ["Now eliminate `U[2,1]`:"]}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["M3 = np.eye(n)\n", "M3[2,1] = -U[2,1]/U[1,1]"], "outputs": [], "prompt_number": 63, "collapsed": false}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["U = M3.dot(U)\n", "U"], "outputs": [{"output_type": "pyout", "prompt_number": 64, "metadata": {}, "text": ["array([[ -2. , 2. , -1. ],\n", " [ 0. , -2. , -7.5],\n", " [ 0. , 0. , 38. ]])"]}], "prompt_number": 64, "collapsed": false}, {"cell_type": "markdown", "metadata": {}, "source": ["------------------\n", "\n", "Try inverting one of the `M`s:"]}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["print(M2)\n", "print(la.inv(M2))"], "outputs": [{"stream": "stdout", "output_type": "stream", "text": ["[[ 1. 0. 0. ]\n", " [ 0. 1. 0. ]\n", " [-2.5 0. 1. ]]\n", "[[ 1. -0. -0. ]\n", " [ 0. 1. 0. ]\n", " [ 2.5 0. 1. ]]\n"]}], "prompt_number": 65, "collapsed": false}, {"cell_type": "markdown", "metadata": {}, "source": ["----------------\n", "\n", "So we've built `M3*M2*M1*A=U`. Test:"]}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["U2 = M3.dot(M2.dot(M1.dot(A)))\n", "U2"], "outputs": [{"output_type": "pyout", "prompt_number": 66, "metadata": {}, "text": ["array([[ -2. , 2. , -1. ],\n", " [ 0. , -2. , -7.5],\n", " [ 0. , 0. , 38. ]])"]}], "prompt_number": 66, "collapsed": false}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["U"], "outputs": [{"output_type": "pyout", "prompt_number": 67, "metadata": {}, "text": ["array([[ -2. , 2. , -1. ],\n", " [ 0. , -2. , -7.5],\n", " [ 0. , 0. , 38. ]])"]}], "prompt_number": 67, "collapsed": false}, {"cell_type": "markdown", "metadata": {}, "source": ["Now define `L`:"]}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["L = la.inv(M1).dot(la.inv(M2).dot(la.inv(M3)))\n", "L"], "outputs": [{"output_type": "pyout", "prompt_number": 68, "metadata": {}, "text": ["array([[ 1. , 0. , 0. ],\n", " [ 1.5, 1. , 0. ],\n", " [ 2.5, 5. , 1. ]])"]}], "prompt_number": 68, "collapsed": false}, {"cell_type": "markdown", "metadata": {}, "source": ["Observations? (Shape? Diagonal values?)"]}, {"cell_type": "code", "metadata": {}, "language": "python", "input": ["np.dot(L, U) - A"], "outputs": [{"output_type": "pyout", "prompt_number": 69, "metadata": {}, "text": ["array([[ 0., 0., 0.],\n", " [ 0., 0., 0.],\n", " [ 0., 0., 0.]])"]}], "prompt_number": 69, "collapsed": false}]}], "metadata": {"name": "", "signature": "sha256:d8315dda54f5436b61ec34bb60f62b1133fccbd522580a355c1c27da50d2769d"}, "nbformat_minor": 0}