{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sympy import symbols, sin, cos\n",
    "from ga import Ga\n",
    "from printer import Format, Fmt\n",
    "from IPython.display import Latex\n",
    "Format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0 & 0\\\\0 & 1 & 0\\\\0 & 0 & 1\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  1  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  1⎦"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xyz_coords = (x, y, z) = symbols('x y z', real=True)\n",
    "(o3d, ex, ey, ez) = Ga.build('e', g=[1, 1, 1], coords=xyz_coords, norm=True)\n",
    "o3d.g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} f = f  \\end{equation*}"
      ],
      "text/plain": [
       "f "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = o3d.mv('f', 'scalar', f=True)\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla^{2} = \\frac{\\partial^{2}}{\\partial x^{2}} + \\frac{\\partial^{2}}{\\partial y^{2}} + \\frac{\\partial^{2}}{\\partial z^{2}} \\end{equation*}"
      ],
      "text/plain": [
       "\\frac{\\partial^{2}}{\\partial x^{2}} + \\frac{\\partial^{2}}{\\partial y^{2}} + \\frac{\\partial^{2}}{\\partial z^{2}}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F = o3d.mv('F', 'vector', f=True)\n",
    "lap = o3d.grad*o3d.grad\n",
    "lap.Fmt(1,r'\\nabla^{2}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla^{2} = \\frac{\\partial^{2}}{\\partial x^{2}} + \\frac{\\partial^{2}}{\\partial y^{2}} + \\frac{\\partial^{2}}{\\partial z^{2}} \\end{equation*}"
      ],
      "text/plain": [
       "\\frac{\\partial^{2}}{\\partial x^{2}} + \\frac{\\partial^{2}}{\\partial y^{2}} + \\frac{\\partial^{2}}{\\partial z^{2}}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "lap.Fmt(1,r'\\nabla^{2}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\partial^{2}_{x} f  + \\partial^{2}_{y} f  + \\partial^{2}_{z} f  \\end{equation*}"
      ],
      "text/plain": [
       "\\partial^{2}_{x} f  + \\partial^{2}_{y} f  + \\partial^{2}_{z} f "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lapf = lap*f\n",
    "lapf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\nabla \\\\cdot (\\\\nabla f) = \\\\partial^{2}_{x} f  + \\\\partial^{2}_{y} f  + \\\\partial^{2}_{z} f '"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lapf = o3d.grad | (o3d.grad * f)\n",
    "lapf.Fmt(1,r'\\nabla \\cdot (\\nabla f)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'x = = \\\\partial_{x} F^{x}  + \\\\partial_{y} F^{y}  + \\\\partial_{z} F^{z} '"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "divF = o3d.grad|F\n",
    "divF.Fmt(1,'x =')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\nabla F = \\\\left ( \\\\partial_{x} F^{x}  + \\\\partial_{y} F^{y}  + \\\\partial_{z} F^{z} \\\\right )  + \\\\left ( - \\\\partial_{y} F^{x}  + \\\\partial_{x} F^{y} \\\\right ) \\\\boldsymbol{e}_{x}\\\\wedge \\\\boldsymbol{e}_{y} + \\\\left ( - \\\\partial_{z} F^{x}  + \\\\partial_{x} F^{z} \\\\right ) \\\\boldsymbol{e}_{x}\\\\wedge \\\\boldsymbol{e}_{z} + \\\\left ( - \\\\partial_{z} F^{y}  + \\\\partial_{y} F^{z} \\\\right ) \\\\boldsymbol{e}_{y}\\\\wedge \\\\boldsymbol{e}_{z}'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gradF = o3d.grad * F\n",
    "gradF.Fmt(1,r'\\nabla F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0 & 0\\\\0 & r^{2} & 0\\\\0 & 0 & r^{2} \\sin^{2}{\\left (\\theta \\right )}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1  0       0     ⎤\n",
       "⎢                 ⎥\n",
       "⎢    2            ⎥\n",
       "⎢0  r       0     ⎥\n",
       "⎢                 ⎥\n",
       "⎢        2    2   ⎥\n",
       "⎣0  0   r ⋅sin (θ)⎦"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sph_coords = (r, th, phi) = symbols('r theta phi', real=True)\n",
    "(sp3d, er, eth, ephi) = Ga.build('e', g=[1, r**2, r**2 * sin(th)**2], coords=sph_coords, norm=True)\n",
    "sp3d.g_raw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla = \\boldsymbol{e}_{r} \\frac{\\partial}{\\partial r} + \\boldsymbol{e}_{\\theta } \\frac{1}{r} \\frac{\\partial}{\\partial \\theta } + \\boldsymbol{e}_{\\phi } \\frac{1}{r \\sin{\\left (\\theta  \\right )}} \\frac{\\partial}{\\partial \\phi } \\end{equation*}"
      ],
      "text/plain": [
       "\\boldsymbol{e}_{r} \\frac{\\partial}{\\partial r} + \\boldsymbol{e}_{\\theta } \\frac{1}{r} \\frac{\\partial}{\\partial \\theta } + \\boldsymbol{e}_{\\phi } \\frac{1}{r \\sin{\\left (\\theta  \\right )}} \\frac{\\partial}{\\partial \\phi }"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp3d.grad.Fmt(1,r'\\nabla')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla^{2}  = \\frac{2}{r} \\frac{\\partial}{\\partial r} + \\frac{1}{r^{2} \\tan{\\left (\\theta  \\right )}} \\frac{\\partial}{\\partial \\theta } + \\frac{\\partial^{2}}{\\partial r^{2}} + r^{-2} \\frac{\\partial^{2}}{\\partial \\theta ^{2}} + \\frac{1}{r^{2} \\sin^{2}{\\left (\\theta  \\right )}} \\frac{\\partial^{2}}{\\partial \\phi ^{2}} \\end{equation*}"
      ],
      "text/plain": [
       "\\frac{2}{r} \\frac{\\partial}{\\partial r} + \\frac{1}{r^{2} \\tan{\\left (\\theta  \\right )}} \\frac{\\partial}{\\partial \\theta } + \\frac{\\partial^{2}}{\\partial r^{2}} + r^{-2} \\frac{\\partial^{2}}{\\partial \\theta ^{2}} + \\frac{1}{r^{2} \\sin^{2}{\\left (\\theta  \\right )}} \\frac{\\partial^{2}}{\\partial \\phi ^{2}}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = sp3d.mv('f', 'scalar', f=True)\n",
    "F = sp3d.mv('F', 'vector', f=True)\n",
    "B = sp3d.mv('B', 'bivector', f=True)\n",
    "sp3d.grad.Fmt(1,r'\\nabla')\n",
    "lap = sp3d.grad*sp3d.grad\n",
    "lap.Fmt(1,r'\\nabla^{2} ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\nabla^{2} f = \\\\frac{1}{r^{2}} \\\\left(r^{2} \\\\partial^{2}_{r} f  + 2 r \\\\partial_{r} f  + \\\\partial^{2}_{\\\\theta } f  + \\\\frac{\\\\partial_{\\\\theta } f }{\\\\tan{\\\\left (\\\\theta  \\\\right )}} + \\\\frac{\\\\partial^{2}_{\\\\phi } f }{\\\\sin^{2}{\\\\left (\\\\theta  \\\\right )}}\\\\right)'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Lapf = lap*f\n",
    "Lapf.Fmt(1,r'\\nabla^{2} f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\nabla \\\\cdot (\\\\nabla f) = \\\\frac{1}{r^{2}} \\\\left(r^{2} \\\\partial^{2}_{r} f  + 2 r \\\\partial_{r} f  + \\\\partial^{2}_{\\\\theta } f  + \\\\frac{\\\\partial_{\\\\theta } f }{\\\\tan{\\\\left (\\\\theta  \\\\right )}} + \\\\frac{\\\\partial^{2}_{\\\\phi } f }{\\\\sin^{2}{\\\\left (\\\\theta  \\\\right )}}\\\\right)'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lapf = sp3d.grad | (sp3d.grad * f)\n",
    "lapf.Fmt(1,r'\\nabla \\cdot (\\nabla f)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\nabla F = \\\\frac{1}{r} \\\\left(r \\\\partial_{r} F^{r}  + 2 F^{r}  + \\\\frac{F^{\\\\theta } }{\\\\tan{\\\\left (\\\\theta  \\\\right )}} + \\\\partial_{\\\\theta } F^{\\\\theta }  + \\\\frac{\\\\partial_{\\\\phi } F^{\\\\phi } }{\\\\sin{\\\\left (\\\\theta  \\\\right )}}\\\\right)'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gradF = sp3d.grad | F\n",
    "gradF.Fmt(1,r'\\nabla F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\nabla \\\\wedge F = \\\\frac{1}{r} \\\\left(r \\\\partial_{r} F^{\\\\theta }  + F^{\\\\theta }  - \\\\partial_{\\\\theta } F^{r} \\\\right) \\\\boldsymbol{e}_{r}\\\\wedge \\\\boldsymbol{e}_{\\\\theta } + \\\\frac{1}{r} \\\\left(r \\\\partial_{r} F^{\\\\phi }  + F^{\\\\phi }  - \\\\frac{\\\\partial_{\\\\phi } F^{r} }{\\\\sin{\\\\left (\\\\theta  \\\\right )}}\\\\right) \\\\boldsymbol{e}_{r}\\\\wedge \\\\boldsymbol{e}_{\\\\phi } + \\\\frac{1}{r} \\\\left(\\\\frac{F^{\\\\phi } }{\\\\tan{\\\\left (\\\\theta  \\\\right )}} + \\\\partial_{\\\\theta } F^{\\\\phi }  - \\\\frac{\\\\partial_{\\\\phi } F^{\\\\theta } }{\\\\sin{\\\\left (\\\\theta  \\\\right )}}\\\\right) \\\\boldsymbol{e}_{\\\\theta }\\\\wedge \\\\boldsymbol{e}_{\\\\phi }'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "curlF = sp3d.grad ^ F\n",
    "curlF.Fmt(1,r'\\nabla \\wedge F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\nabla \\\\cdot B = - \\\\frac{1}{r} \\\\left(\\\\frac{B^{r\\\\theta } }{\\\\tan{\\\\left (\\\\theta  \\\\right )}} + \\\\partial_{\\\\theta } B^{r\\\\theta }  + \\\\frac{\\\\partial_{\\\\phi } B^{r\\\\phi } }{\\\\sin{\\\\left (\\\\theta  \\\\right )}}\\\\right) \\\\boldsymbol{e}_{r} + \\\\frac{1}{r} \\\\left(r \\\\partial_{r} B^{r\\\\theta }  + B^{r\\\\theta }  - \\\\frac{\\\\partial_{\\\\phi } B^{\\\\phi \\\\phi } }{\\\\sin{\\\\left (\\\\theta  \\\\right )}}\\\\right) \\\\boldsymbol{e}_{\\\\theta } + \\\\frac{1}{r} \\\\left(r \\\\partial_{r} B^{r\\\\phi }  + B^{r\\\\phi }  + \\\\partial_{\\\\theta } B^{\\\\phi \\\\phi } \\\\right) \\\\boldsymbol{e}_{\\\\phi }'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "divB = sp3d.grad | B\n",
    "divB.Fmt(1,r'\\nabla \\cdot B')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} F = F^{r}  \\boldsymbol{e}_{r} + F^{\\theta }  \\boldsymbol{e}_{\\theta } + F^{\\phi }  \\boldsymbol{e}_{\\phi } \\end{equation*}"
      ],
      "text/plain": [
       "F^{r}  \\boldsymbol{e}_{r} + F^{\\theta }  \\boldsymbol{e}_{\\theta } + F^{\\phi }  \\boldsymbol{e}_{\\phi }"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'F =  \\\\begin{align*}  & F^{r}  \\\\boldsymbol{e}_{r} \\\\\\\\  &  + F^{\\\\theta }  \\\\boldsymbol{e}_{\\\\theta } \\\\\\\\  &  + F^{\\\\phi }  \\\\boldsymbol{e}_{\\\\phi }  \\\\end{align*} \\n'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F.Fmt(3,'F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
