{ "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 numpy.linalg as la" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "n = 5\n", "\n", "A_dp = np.eye(n) + np.random.randn(n, n)\n", "A_sp = A_dp.astype(np.float32)\n", "x_dp = np.ones(n, np.float64)\n", "b_dp = np.dot(A_dp, x_dp)\n", "b_sp = b_dp.astype(np.float32)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can we solve $Ax=b$ to DP (double precision) accuracy using (almost) only SP matrix operations?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1) Solve $Ax=b$ in SP and compute DP residual $r_0=b-Ax_0$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x0_sp = la.solve(A_sp, b_sp)\n", "r0_dp = b_dp - np.dot(A_dp, x0_sp.astype(np.float64))\n", "\n", "print la.norm(r0_dp)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3.12732473678e-07\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "2) Solve $Az_0=r_0$\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "z0_sp = la.solve(A_sp, r0_dp.astype(np.float32))\n", "\n", "print la.norm(z0_sp)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2.06476e-07\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "3) Use $x_1 = x_0 + z_0$ as improved solution" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x1_dp = (\n", " x0_sp.astype(np.float64)\n", " + z0_sp.astype(np.float64)\n", " )\n", "print repr(float(x0_sp[0]))\n", "print repr(float(z0_sp[0]))\n", "print repr(float(x1_dp[0]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.0000001192092896\n", "-1.192092469182171e-07\n", "1.0000000000000426\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "la.norm(x0_sp-x_dp)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "2.0647654623614278e-07" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "la.norm(x1_dp-x_dp)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "5.1599458938384882e-14" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }