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