{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "8.2.5 Alternative Gauss Jordon Algorithm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, you will implement the alternative Gauss Jordan algorithm that overwrites $ A $ in one sweep with the identity matrix and $ B $ with the inverse of the original matrix $ A $.\n", "\n", " Be sure to make a copy!!!! \n", "\n", "

First, let's create a matrix $ A $ and set $ B $ to the identity.

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import laff\n", "import flame\n", "\n", "L = np.matrix( ' 1, 0, 0. 0;\\\n", " -2, 1, 0, 0;\\\n", " 1,-3, 1, 0;\\\n", " 2, 3,-1, 1' )\n", "\n", "U = np.matrix( ' 2,-1, 3,-2;\\\n", " 0,-2, 1,-1;\\\n", " 0, 0, 1, 2;\\\n", " 0, 0, 0, 3' )\n", "\n", "A = L * U\n", "Aold = np.matrix( np.copy( A ) ) \n", "B = np.matrix( np.eye( 4 ) )\n", "\n", "print( 'A = ' )\n", "print( A )\n", "\n", "print( 'B = ' )\n", "print( B )\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Implement the alternative Gauss-Jordan algorithm from 8.2.5

\n", "\n", "Here is the algorithm:\n", "\n", "\"Alternative\n", " \n", " Important: if you make a mistake, rerun ALL cells above the cell in which you were working, and then the one where you are working. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the routine\n", " GJ_Inverse_alt \n", "with the Spark webpage for the algorithm" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# insert code here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Test the routine

\n", "\n", " Important: if you make a mistake, rerun ALL cells above the cell in which you were working, and then the one where you are working. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "GJ_Inverse_alt( A, B )\n", "\n", "print( A )\n", "print( B )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matrix $ A $ should now be an identity matrix and $ B $ should no longer be an identity matrix.\n", "\n", "Check if $ B $ now equals (approximately) the inverse of the original matrix $ A $:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print( Aold * B )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }