{
 "metadata": {
  "name": "cross_product_error"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The error in a cross product calculation with (3,) tuples"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sympy import Symbol, symarray, Matrix, matrices\n",
      "u = Matrix(symarray('u', (3, 1)))\n",
      "v = Matrix(symarray('v', (3, 1)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "c = u.cross(v)\n",
      "c"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "[u_1_0*v_2_0 - u_2_0*v_1_0, -u_0_0*v_2_0 + u_2_0*v_0_0, u_0_0*v_1_0 - u_1_0*v_0_0]"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Assuming same error $\\delta$ for both vectors:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "d = Symbol('d')\n",
      "e = matrices.ones((3,1)) * d"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Same calculation as above, with error"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ue = u + e\n",
      "ve = v + e\n",
      "ce = ue.cross(ve)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Calculate absolute error by subtracting true result from result with error"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "cce = ce - c\n",
      "cce.simplify()\n",
      "cce"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 5,
       "text": [
        "[d*(u_1_0 - u_2_0 - v_1_0 + v_2_0), d*(-u_0_0 + u_2_0 + v_0_0 - v_2_0), d*(u_0_0 - u_1_0 - v_0_0 + v_1_0)]"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Floating point calculation error given by operations on elements:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "c"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "[u_1_0*v_2_0 - u_2_0*v_1_0, -u_0_0*v_2_0 + u_2_0*v_0_0, u_0_0*v_1_0 - u_1_0*v_0_0]"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Each element has two products and one subtraction; The input values are $\\le 1$.  Calculation error per element then $3 \\epsilon / 2$"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}
