{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import division\n", "\n", "import numpy as np\n", "import scipy.linalg as la" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stderr", "text": [ "/usr/lib/pymodules/python2.7/numpy/oldnumeric/__init__.py:11: ModuleDeprecationWarning: The oldnumeric module will be dropped in Numpy 1.9\n", " warnings.warn(_msg, ModuleDeprecationWarning)\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "n = 5\n", "A = np.random.randn(n, n)\n", "u = np.random.randn(n)\n", "v = np.random.randn(n)\n", "\n", "b = np.random.randn(n)\n", "\n", "Ahat = A + np.outer(u, v)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "LU, piv = la.lu_factor(A)\n", "print LU\n", "print piv" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[-1.34619591 -2.40396885 1.34168773 -1.26156511 -0.57082875]\n", " [-0.65336062 -4.03983814 1.8543316 0.62740517 0.04102615]\n", " [-0.12411925 0.35016924 -3.01157725 1.71780721 -0.10249761]\n", " [-0.23344541 -0.05001819 0.08638383 -1.58556641 0.49576436]\n", " [-0.01497017 0.02626202 -0.16798668 -0.09674568 2.0533571 ]]\n", "[1 4 2 4 4]\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "def solveA(b):\n", " return la.lu_solve((LU, piv), b)\n", "\n", "la.norm(np.dot(A, solveA(b)) - b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "4.002966042486721e-16" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$(A+uv^T)^{-1} = A^{-1} - {A^{-1}uv^T A^{-1} \\over 1 + v^T A^{-1}u}$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "xhat = la.solve(Ahat, b)\n", "\n", "xhat2 = ...\n", "\n", "print la.norm(xhat - xhat2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.57009245868e-16\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "(Edit this cell to see solution.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What's the cost of the Sherman-Morrison procedure?" ] } ], "metadata": {} } ] }