{"nbformat": 3, "worksheets": [{"metadata": {}, "cells": [{"source": ["# Computing the Nullspace"], "cell_type": "markdown", "metadata": {}}, {"outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 41, "input": ["import numpy as np\n", "import numpy.linalg as la"], "metadata": {}}, {"outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 46, "input": ["n = 5\n", "np.random.seed(25)\n", "A = np.random.randn(n, n)\n", "\n", "# Decrease the rank\n", "A[4] = A[0] + 5 * A[2]\n", "A[1] = 3 * A[0] -2 * A[3]"], "metadata": {}}, {"outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 47, "input": ["from scipy.linalg import lu"], "metadata": {}}, {"outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 48, "input": ["PT, L, U = lu(A.T)\n", "P = PT.T"], "metadata": {}}, {"outputs": [{"prompt_number": 49, "output_type": "pyout", "text": ["1.1322097734007351e-15"], "metadata": {}}], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 49, "input": ["la.norm(\n", " P.dot(A.T) - L.dot(U))"], "metadata": {}}, {"outputs": [{"prompt_number": 50, "output_type": "pyout", "text": ["array([[ 1.027e+00, 6.761e-01, -2.323e-01, 1.202e+00, -1.347e-01],\n", " [ 0.000e+00, -3.498e+00, -1.468e+00, 1.749e+00, -7.342e+00],\n", " [ 0.000e+00, 0.000e+00, -2.140e+00, 4.441e-16, -1.070e+01],\n", " [ 0.000e+00, 0.000e+00, 0.000e+00, 5.761e-16, 0.000e+00],\n", " [ 0.000e+00, 0.000e+00, 0.000e+00, 0.000e+00, 8.882e-16]])"], "metadata": {}}], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 50, "input": ["np.set_printoptions(precision=3)\n", "U"], "metadata": {}}, {"source": ["Now define `NUT` as vectors spanning the nullspace of $N(U^T)$."], "cell_type": "markdown", "metadata": {}}, {"outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 55, "input": ["NUT = np.eye(n)[:, 3:]"], "metadata": {}}, {"source": ["Check that it's actually a nullspace:"], "cell_type": "markdown", "metadata": {}}, {"outputs": [{"prompt_number": 56, "output_type": "pyout", "text": ["array([[ 0.000e+00, 0.000e+00],\n", " [ 0.000e+00, 0.000e+00],\n", " [ 0.000e+00, 0.000e+00],\n", " [ 5.761e-16, 0.000e+00],\n", " [ 0.000e+00, 8.882e-16]])"], "metadata": {}}], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 56, "input": ["U.T.dot(NUT)"], "metadata": {}}, {"source": ["Now define `NA` as some vectors spanning $N(A)$:"], "cell_type": "markdown", "metadata": {}}, {"outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 58, "input": ["NA = P.T.dot(la.solve(L.T, NUT))"], "metadata": {}}, {"source": ["And check:"], "cell_type": "markdown", "metadata": {}}, {"outputs": [{"prompt_number": 59, "output_type": "pyout", "text": ["array([[ -1.110e-16, 0.000e+00],\n", " [ 0.000e+00, 0.000e+00],\n", " [ 0.000e+00, 1.110e-16],\n", " [ 1.110e-16, 5.551e-17],\n", " [ 0.000e+00, 4.441e-16]])"], "metadata": {}}], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 59, "input": ["A.dot(NA)"], "metadata": {}}]}], "nbformat_minor": 0, "metadata": {"name": "", "signature": "sha256:12c8d94bcce1af854986ce040986874d9c12e59ebe11d48f2471b6f01a190a8d"}}