\documentclass[12pt]{cdblatex}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% =================================================================================================
% create checkpoint file

\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   import cdblib
   checkpoint_file = 'tests/semantic/output/geodesic-bvp.json'
   cdblib.create (checkpoint_file)
   checkpoint = []
\end{cadabra}
\egroup

% =================================================================================================
\section*{Geodesic BVP}

Consider a geodesic that connects two points $P_i$ and $P_j$ with RNC coordinates
$x^a_i$ and $x^a_j$. Our aim is to construct a solution $x^a(s)$ of the geodesic
equation such that $x^a(0)= x^a_i$ and $x^a(1)=x^a_j$.

We will do this in two stages. First we will solve
\begin{align}
   \label{eq:twoptBVP}
   x^a_j = x^a_i + y^a - \sum_{k=2}^\infty\>\frac{1}{k!}\>\Gamma^{a}_{\ubk}y^{.\ubk}
\end{align}
for $y^a$ as an explicit polynomial in $x^a_i$ and $x^a_j$. The functions $\Gamma^{a}_{\ubk}$ are
the generalised connections for the RNC frame evaluated at $x^a=x^a_i$.

In the second stage, we will substitute our expression for $y^a$ into
\begin{align}
   \label{eq:solBVP}
   x^a(s) = x^a_i + s y^a - \sum_{k=2}^\infty\>\frac{1}{k!}\>\Gamma^{a}_{\ubk}y^{.\ubk} s^k
\end{align}
to obtain the desired solution to the two point boundary value problem.

% =================================================================================================
\section*{Stage 1: The fixed point iteration scheme}

First we rewrite the main equation \eqref{eq:twoptBVP} in the suggestive form
\begin{align*}
   y^a = \Dx^a + \sum_{k=2}^\infty\>\frac{1}{k!}\>\Gamma^{a}_{\ubk}y^{\ubk}
\end{align*}
where $\Dx^a = x^a_j-x^a_i$. Our approximate solution for $y^a$ will be taken to be the partial
sums for the infinite series. Thus we will solve
\begin{align*}
   \ny{n}^a = \Dx^a + \sum_{k=2}^n\>\frac{1}{k!}\>\Gamma^{a}_{\ubk}\ny{n}^{\ubk}
\end{align*}
for $\ny{n}^a$. Note that in the last term of the sum, the $\Gamma^{a}_{\ubn}$ will contain
curvature terms of order $\BigO{\eps^n}$. Thus in truncating the series at this point we will
loose contributions to the curvature terms of order $\BigO{\eps^{n+1}}$ and higher. So to be
consistent we must truncate all terms of the partial sum to order $\BigO{\eps^n}$ (i.e., exclude
any contributions from terms $\BigO{\eps^{n+1}}$ and higher, these are the terms that would
couple with the terms that we excluded when truncating the original infinite series). Let
$\nT{k}$ be the operator that truncates its argument to contain terms no higher than
$\BigO{\eps^n}$. Then we have the following modified version of the equation for $\ny{n}^a$
\begin{align*}
   \ny{n}^a = \Dx^a
            + \sum_{k=2}^n\>\frac{1}{k!}\>\nT{k}\left(\Gamma^{a}_{\ubk}\ny{n}^{\ubk}\right)
\end{align*}
Finally we note that since $\Gamma^{a}_{\ubk} = \BigO{\eps^k}$, we can use lower order estimates
for the $\ny{k}^a$ in the right hand side of the sum. This allows us to compute $\ny{n}^a$ by
successive approximations such as
\begin{align*}
   \ny{0}^a &= \Dx^a\\
   \ny{2}^a &= \ny{0}^a + \frac{1}{2!}\nT{2}\left(\Gamma^a_{bc}\>\ny{0}^b \ny{0}^c\right)\\[5pt]
   \ny{3}^a &= \ny{0}^a + \frac{1}{2!}\nT{3}\left(\Gamma^a_{bc}\>\ny{2}^b \ny{2}^c\right)
                        + \frac{1}{3!}\nT{3}\left(\Gamma^a_{bcd}\>\ny{0}^b \ny{0}^c \ny{0}^d\right)\\[5pt]
   \ny{4}^a &= \ny{0}^a + \frac{1}{2!}\nT{4}\left(\Gamma^a_{bc}\>\ny{3}^b \ny{3}^c\right)
                        + \frac{1}{3!}\nT{4}\left(\Gamma^a_{bcd}\>\ny{2}^b \ny{2}^c \ny{2}^d\right)
                        + \frac{1}{4!}\nT{4}\left(\Gamma^a_{bcde}\>\ny{0}^b \ny{0}^c \ny{0}^d \ny{0}^e\right)\\[5pt]
   \ny{5}^a &= \ny{0}^a + \frac{1}{2!}\nT{5}\left(\Gamma^a_{bc}\>\ny{4}^b \ny{4}^c\right)
                        + \frac{1}{3!}\nT{5}\left(\Gamma^a_{bcd}\>\ny{3}^b \ny{3}^c \ny{3}^d\right)
                        + \frac{1}{4!}\nT{5}\left(\Gamma^a_{bcde}\>\ny{2}^b \ny{2}^c \ny{2}^d \ny{2}^e\right)
                        + \frac{1}{5!}\nT{5}\left(\Gamma^a_{bcdef}\>\ny{0}^b \ny{0}^c \ny{0}^d \ny{0}^e \ny{0}^f\right)
\end{align*}
and so on. Note that there are no $\ny{1}^a$ terms.

% =================================================================================================
\section*{Stage 2: Introduce the generalised connections}

This is the final stage -- it introduces the generalised connecstion after the
completion of the fixed point scheme.

All that needs be done is to substitute our expression for $y^a$ into \eqref{eq:solBVP}
% \begin{align}
%    \label{eq:solBVP}
%    x^a(s) = x^a_i + s y^a - \sum_{k=2}^\infty\>\frac{1}{k!}\>\Gamma^{a}{}_{\ubk} y^{.\ubk} s^k
% \end{align}
to obtain the desired solution to the two point boundary value problem.

The generalised connections $\Gamma^{a}{}_{\ubk}$ are taken from the results of the
{\tts genGamma} code.

\clearpage

% =================================================================================================
\section*{Stage 1: The fixed point iteration scheme}

\begin{cadabra}
   import time

   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w#}::Indices(position=independent).

   \nabla{#}::Derivative.

   g_{a b}::Metric.
   g^{a b}::InverseMetric.

   R_{a b c d}::RiemannTensor.
   R_{a b c d}::Depends(\nabla{#}).

   {Gam22^{a}_{b c},Gam23^{a}_{b c},Gam24^{a}_{b c},Gam25^{a}_{b c}}::TableauSymmetry(shape={2}, indices={1,2}).
   {Gam33^{a}_{b c d},Gam34^{a}_{b c d},Gam35^{a}_{b c d}}::TableauSymmetry(shape={3}, indices={1,2,3}).
   {Gam44^{a}_{b c d e},Gam45^{a}_{b c d e}}::TableauSymmetry(shape={4}, indices={1,2,3,4}).
   {Gam55^{a}_{b c d e f}}::TableauSymmetry(shape={5}, indices={1,2,3,4,5}).

   {Gam22^{a}_{b c}}::Weight(label=eps,value=2).
   {Gam23^{a}_{b c},Gam33^{a}_{b c d}}::Weight(label=eps,value=3).
   {Gam24^{a}_{b c},Gam34^{a}_{b c d},Gam44^{a}_{b c d e}}::Weight(label=eps,value=4).
   {Gam25^{a}_{b c},Gam35^{a}_{b c d},Gam45^{a}_{b c d e},Gam55^{a}_{b c d e f}}::Weight(label=eps,value=5).

   {Dx^{a}}::Weight(label=eps,value=0).

   {y00^{a},y20^{a},y30^{a},y40^{a},y50^{a}}::Weight(label=eps,value=0).
   {y22^{a},y32^{a},y42^{a},y52^{a}}::Weight(label=eps,value=2).
   {y33^{a},y43^{a},y53^{a}}::Weight(label=eps,value=3).
   {y44^{a},y54^{a}}::Weight(label=eps,value=4).
   {y55^{a}}::Weight(label=eps,value=5).

   # Dx{#}::LaTeXForm{"{\Dx}"}.  # LCB: currently causes a bug, it kills ::KeepWeight for Dx

   # note: keeping numbering as is (out of order) to ensure R appears before \nabla R etc.
   def product_sort (obj):
       substitute (obj,$ x^{a}                            -> A001^{a}               $)
       substitute (obj,$ Dx^{a}                           -> A002^{a}               $)
       substitute (obj,$ g^{a b}                          -> A003^{a b}             $)
       substitute (obj,$ \nabla_{e f g h}{R_{a b c d}}    -> A008_{a b c d e f g h} $)
       substitute (obj,$ \nabla_{e f g}{R_{a b c d}}      -> A007_{a b c d e f g}   $)
       substitute (obj,$ \nabla_{e f}{R_{a b c d}}        -> A006_{a b c d e f}     $)
       substitute (obj,$ \nabla_{e}{R_{a b c d}}          -> A005_{a b c d e}       $)
       substitute (obj,$ R_{a b c d}                      -> A004_{a b c d}         $)
       sort_product   (obj)
       rename_dummies (obj)
       substitute (obj,$ A001^{a}                  -> x^{a}                         $)
       substitute (obj,$ A002^{a}                  -> Dx^{a}                        $)
       substitute (obj,$ A003^{a b}                -> g^{a b}                       $)
       substitute (obj,$ A004_{a b c d}            -> R_{a b c d}                   $)
       substitute (obj,$ A005_{a b c d e}          -> \nabla_{e}{R_{a b c d}}       $)
       substitute (obj,$ A006_{a b c d e f}        -> \nabla_{e f}{R_{a b c d}}     $)
       substitute (obj,$ A007_{a b c d e f g}      -> \nabla_{e f g}{R_{a b c d}}   $)
       substitute (obj,$ A008_{a b c d e f g h}    -> \nabla_{e f g h}{R_{a b c d}} $)

       return obj

   def get_term (obj,n):

       tmp := @(obj).
       foo = Ex("eps = " + str(n))
       distribute  (tmp)
       keep_weight (tmp, foo)

       return tmp

   def truncate (obj,n):

       ans = Ex(0)

       for i in range (0,n+1):
          foo := @(obj).
          bah = Ex("eps = " + str(i))
          distribute  (foo)
          keep_weight (foo, bah)
          ans = ans + foo

       return ans

   def substitute_eps (obj):
       substitute     (obj,epsy0)
       substitute     (obj,epsy2)
       substitute     (obj,epsy3)
       substitute     (obj,epsy4)
       substitute     (obj,epsy5)
       substitute     (obj,epsGam2)
       substitute     (obj,epsGam3)
       substitute     (obj,epsGam4)
       substitute     (obj,epsGam5)
       distribute     (obj)
       obj = truncate     (obj,5)
       obj = product_sort (obj)
       rename_dummies (obj)
       canonicalise   (obj)

       return obj

   beg_stage_1 = time.time()

   # yn = y expanded to terms upto and including O(eps^n)

   y0 := Dx^{a}.
   y2 := Dx^{a} +   (1/2) Gam^{a}_{b c} y0^{b} y0^{c}.
   y3 := Dx^{a} +   (1/2) Gam^{a}_{b c} y2^{b} y2^{c}
                +   (1/6) Gam^{a}_{b c d} y0^{b} y0^{c} y0^{d}.
   y4 := Dx^{a} +   (1/2) Gam^{a}_{b c} y3^{b} y3^{c}
                +   (1/6) Gam^{a}_{b c d} y2^{b} y2^{c} y2^{d}
                +  (1/24) Gam^{a}_{b c d e} y0^{b} y0^{c} y0^{d} y0^{e}.
   y5 := Dx^{a} +   (1/2) Gam^{a}_{b c} y4^{b} y4^{c}
                +   (1/6) Gam^{a}_{b c d} y3^{b} y3^{c} y3^{d}
                +  (1/24) Gam^{a}_{b c d e} y2^{b} y2^{c} y2^{d} y2^{e}
                + (1/120) Gam^{a}_{b c d e f} y0^{b} y0^{c} y0^{d} y0^{e} y0^{f}.

   # epsyN = y expanded to terms upto and including O(eps^N)
   # yPQ = O(eps^Q) term of epsyP

   # expand to O(eps^5)

   epsy0 := y0^{a} -> y00^{a}.
   epsy2 := y2^{a} -> y20^{a}+y22^{a}.
   epsy3 := y3^{a} -> y30^{a}+y32^{a}+y33^{a}.
   epsy4 := y4^{a} -> y40^{a}+y42^{a}+y43^{a}+y44^{a}.
   epsy5 := y5^{a} -> y50^{a}+y52^{a}+y53^{a}+y54^{a}+y55^{a}.

   # epsGamN = gen. gamma with N lower indices (epsGam2 = the connection)
   # epsGamPQ = O(eps^Q) term of epsGamP

   epsGam2 := Gam^{a}_{b c} -> Gam22^{a}_{b c}+Gam23^{a}_{b c}+Gam24^{a}_{b c}+Gam25^{a}_{b c}.
   epsGam3 := Gam^{a}_{b c d} -> Gam33^{a}_{b c d}+Gam34^{a}_{b c d}+Gam35^{a}_{b c d}.
   epsGam4 := Gam^{a}_{b c d e} -> Gam44^{a}_{b c d e}+Gam45^{a}_{b c d e}.
   epsGam5 := Gam^{a}_{b c d e f} -> Gam55^{a}_{b c d e f}.

   y0 = substitute_eps (y0)   # cdb (y0.001,y0)
   y2 = substitute_eps (y2)   # cdb (y2.001,y2)
   y3 = substitute_eps (y3)   # cdb (y3.001,y3)
   y4 = substitute_eps (y4)   # cdb (y4.001,y4)
   y5 = substitute_eps (y5)   # cdb (y5.001,y5)

   y0 = truncate (y0,1)       # cdb (y0.002,y0)
   y2 = truncate (y2,2)       # cdb (y2.002,y2)
   y3 = truncate (y3,3)       # cdb (y3.002,y3)
   y4 = truncate (y4,4)       # cdb (y4.002,y4)
   y5 = truncate (y5,5)       # cdb (y5.002,y5)

   defy0 := y0^{a} -> @(y0).
   defy2 := y2^{a} -> @(y2).
   defy3 := y3^{a} -> @(y3).
   defy4 := y4^{a} -> @(y4).
   defy5 := y5^{a} -> @(y5).

   # -----------------------------------
   def tidy (obj):
       obj = product_sort (obj)
       rename_dummies     (obj)
       canonicalise       (obj)
       return obj

   # -----------------------------------
   # y0

   y00 := @(y0).           # cdb (y00.101,y00)

   defy00 := y00^{a} -> @(y00).

   # -----------------------------------
   # y2

   substitute (y2,defy00)

   distribute (y2)

   y20 = get_term (y2,0)   # cdb (y20.101,y20)
   y22 = get_term (y2,2)   # cdb (y22.101,y22)

   y20 = tidy (y20)        # cdb (y20.201,y20)
   y22 = tidy (y22)        # cdb (y22.201,y22)

   defy20 := y20^{a} -> @(y20).
   defy22 := y22^{a} -> @(y22).

   # -----------------------------------
   # y3

   substitute (y3,defy00)

   substitute (y3,defy20)
   substitute (y3,defy22)

   distribute (y3)

   y30 = get_term (y3,0)   # cdb (y30.101,y30)
   y32 = get_term (y3,2)   # cdb (y32.101,y32)
   y33 = get_term (y3,3)   # cdb (y33.101,y33)

   y30 = tidy (y30)        # cdb (y30.201,y30)
   y32 = tidy (y32)        # cdb (y32.201,y32)
   y33 = tidy (y33)        # cdb (y33.201,y33)

   defy30 := y30^{a} -> @(y30).
   defy32 := y32^{a} -> @(y32).
   defy33 := y33^{a} -> @(y33).

   # -----------------------------------
   # y4

   substitute (y4,defy00)

   substitute (y4,defy20)
   substitute (y4,defy22)

   substitute (y4,defy30)
   substitute (y4,defy32)
   substitute (y4,defy33)

   distribute (y4)

   y40 = get_term (y4,0)   # cdb (y40.101,y40)
   y42 = get_term (y4,2)   # cdb (y42.101,y42)
   y43 = get_term (y4,3)   # cdb (y43.101,y43)
   y44 = get_term (y4,4)   # cdb (y44.101,y44)

   y40 = tidy (y40)        # cdb (y40.201,y40)
   y42 = tidy (y42)        # cdb (y42.201,y42)
   y43 = tidy (y43)        # cdb (y43.201,y43)
   y44 = tidy (y44)        # cdb (y44.201,y44)

   defy40 := y40^{a} -> @(y40).
   defy42 := y42^{a} -> @(y42).
   defy43 := y43^{a} -> @(y43).
   defy44 := y44^{a} -> @(y44).

   # -----------------------------------
   # y5

   substitute (y5,defy00)

   substitute (y5,defy20)
   substitute (y5,defy22)

   substitute (y5,defy30)
   substitute (y5,defy32)
   substitute (y5,defy33)

   substitute (y5,defy40)
   substitute (y5,defy42)
   substitute (y5,defy43)
   substitute (y5,defy44)

   distribute (y5)

   y50 = get_term (y5,0)   # cdb (y50.101,y50)
   y52 = get_term (y5,2)   # cdb (y52.101,y52)
   y53 = get_term (y5,3)   # cdb (y53.101,y53)
   y54 = get_term (y5,4)   # cdb (y54.101,y54)
   y55 = get_term (y5,5)   # cdb (y55.101,y55)

   y50 = tidy (y50)        # cdb (y50.201,y50)
   y52 = tidy (y52)        # cdb (y52.201,y52)
   y53 = tidy (y53)        # cdb (y53.201,y53)
   y54 = tidy (y54)        # cdb (y54.201,y54)
   y55 = tidy (y55)        # cdb (y55.201,y55)

   defy50 := y50^{a} -> @(y50).
   defy52 := y52^{a} -> @(y52).
   defy53 := y53^{a} -> @(y53).
   defy54 := y54^{a} -> @(y54).
   defy55 := y55^{a} -> @(y55).

   end_stage_1 = time.time()

\end{cadabra}

\clearpage
\begin{dgroup*}
   \begin{dmath*} \cdb*{y0.001} \end{dmath*}
   \begin{dmath*} \cdb*{y2.001} \end{dmath*}
   \begin{dmath*} \cdb*{y3.001} \end{dmath*}
   \begin{dmath*} \cdb*{y4.001} \end{dmath*}
   \begin{dmath*} \cdb*{y5.001} \end{dmath*}
\end{dgroup*}

\clearpage
\begin{dgroup*}
   \begin{dmath*} \cdb*{y0.002} \end{dmath*}
   \begin{dmath*} \cdb*{y2.002} \end{dmath*}
   \begin{dmath*} \cdb*{y3.002} \end{dmath*}
   \begin{dmath*} \cdb*{y4.002} \end{dmath*}
   \begin{dmath*} \cdb*{y5.002} \end{dmath*}
\end{dgroup*}

\clearpage
\begin{dgroup*}
   \begin{dmath*} \cdb*{y00.101} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \cdb*{y20.201} \end{dmath*}
   \begin{dmath*} \cdb*{y22.201} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \cdb*{y30.201} \end{dmath*}
   \begin{dmath*} \cdb*{y32.201} \end{dmath*}
   \begin{dmath*} \cdb*{y33.201} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \cdb*{y40.201} \end{dmath*}
   \begin{dmath*} \cdb*{y42.201} \end{dmath*}
   \begin{dmath*} \cdb*{y43.201} \end{dmath*}
   \begin{dmath*} \cdb*{y44.201} \end{dmath*}
\end{dgroup*}

\clearpage
\begin{dgroup*}
   \begin{dmath*} \cdb*{y50.201} \end{dmath*}
   \begin{dmath*} \cdb*{y52.201} \end{dmath*}
   \begin{dmath*} \cdb*{y53.201} \end{dmath*}
   \begin{dmath*} \cdb*{y54.201} \end{dmath*}
   \begin{dmath*} \cdb*{y55.201} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Stage 2a: Introduce the generalised connections, build terms of $y^{a}$}

\begin{cadabra}
   def substitute_gam (obj):

       substitute (obj,defGam22)
       substitute (obj,defGam23)
       substitute (obj,defGam24)
       substitute (obj,defGam25)

       substitute (obj,defGam33)
       substitute (obj,defGam34)
       substitute (obj,defGam35)

       substitute (obj,defGam44)
       substitute (obj,defGam45)

       substitute (obj,defGam55)

       distribute (obj)
       return obj

   import cdblib

   beg_stage_2a = time.time()

   Gam22 = cdblib.get ('genGamma01','genGamma.json')
   Gam23 = cdblib.get ('genGamma02','genGamma.json')
   Gam24 = cdblib.get ('genGamma03','genGamma.json')
   Gam25 = cdblib.get ('genGamma04','genGamma.json')

   Gam33 = cdblib.get ('genGamma11','genGamma.json')
   Gam34 = cdblib.get ('genGamma12','genGamma.json')
   Gam35 = cdblib.get ('genGamma13','genGamma.json')

   Gam44 = cdblib.get ('genGamma21','genGamma.json')
   Gam45 = cdblib.get ('genGamma22','genGamma.json')

   Gam55 = cdblib.get ('genGamma31','genGamma.json')

   # peel off the A^{a}, must then symmetrise over revealed indices

   substitute (Gam22,$A^{a}->1$)
   substitute (Gam23,$A^{a}->1$)
   substitute (Gam24,$A^{a}->1$)
   substitute (Gam25,$A^{a}->1$)

   substitute (Gam33,$A^{a}->1$)
   substitute (Gam34,$A^{a}->1$)
   substitute (Gam35,$A^{a}->1$)

   substitute (Gam44,$A^{a}->1$)
   substitute (Gam45,$A^{a}->1$)

   substitute (Gam55,$A^{a}->1$)

   # now symmetrise

   sym (Gam22,$_{b},_{c}$)
   sym (Gam23,$_{b},_{c}$)
   sym (Gam24,$_{b},_{c}$)
   sym (Gam25,$_{b},_{c}$)

   sym (Gam33,$_{b},_{c},_{d}$)
   sym (Gam34,$_{b},_{c},_{d}$)
   sym (Gam35,$_{b},_{c},_{d}$)

   sym (Gam44,$_{b},_{c},_{d},_{e}$)
   sym (Gam45,$_{b},_{c},_{d},_{e}$)

   sym (Gam55,$_{b},_{c},_{d},_{e},_{f}$)

   defGam22 := Gam22^{a}_{b c} -> @(Gam22).
   defGam23 := Gam23^{a}_{b c} -> @(Gam23).
   defGam24 := Gam24^{a}_{b c} -> @(Gam24).
   defGam25 := Gam25^{a}_{b c} -> @(Gam25).

   defGam33 := Gam33^{a}_{b c d} -> @(Gam33).
   defGam34 := Gam34^{a}_{b c d} -> @(Gam34).
   defGam35 := Gam35^{a}_{b c d} -> @(Gam35).

   defGam44 := Gam44^{a}_{b c d e} -> @(Gam44).
   defGam45 := Gam45^{a}_{b c d e} -> @(Gam45).

   defGam55 := Gam55^{a}_{b c d e f} -> @(Gam55).

   # ---------------------------------------------------
   # y2

   y22 = substitute_gam (y22)

   y22 = tidy (y22)                                        # cdb (y22.301,y22)

   y2 := @(y20) + @(y22).                                  # cdb (y2.301,y2)

   # ---------------------------------------------------
   # y3

   y32 = substitute_gam (y32)
   y33 = substitute_gam (y33)

   y32 = tidy (y32)                                        # cdb (y32.301,y32)
   y33 = tidy (y33)                                        # cdb (y33.301,y33)

   y3 := @(y30) + @(y32) + @(y33).                         # cdb (y3.301,y3)

   # ---------------------------------------------------
   # y4

   y42 = substitute_gam (y42)
   y43 = substitute_gam (y43)
   y44 = substitute_gam (y44)

   y42 = tidy (y42)                                        # cdb (y42.301,y42)
   y43 = tidy (y43)                                        # cdb (y43.301,y43)
   y44 = tidy (y44)                                        # cdb (y44.301,y44)

   y4 := @(y40) + @(y42) + @(y43) + @(y44).                # cdb (y4.301,y4)

   # ---------------------------------------------------
   # y5

   y52 = substitute_gam (y52)
   y53 = substitute_gam (y53)
   y54 = substitute_gam (y54)
   y55 = substitute_gam (y55)

   y52 = tidy (y52)                                        # cdb (y52.301,y52)
   y53 = tidy (y53)                                        # cdb (y53.301,y53)
   y54 = tidy (y54)                                        # cdb (y54.301,y54)
   y55 = tidy (y55)                                        # cdb (y55.301,y55)

   y5 := @(y50) + @(y52) + @(y53) + @(y54) + @(y55).       # cdb (y5.301,y5)

   # ---------------------------------------------------
   cdblib.create ('geodesic-bvp.json')

   cdblib.put ('y2',y2,'geodesic-bvp.json')
   cdblib.put ('y3',y3,'geodesic-bvp.json')
   cdblib.put ('y4',y4,'geodesic-bvp.json')
   cdblib.put ('y5',y5,'geodesic-bvp.json')

   cdblib.put ('y20',y20,'geodesic-bvp.json')
   cdblib.put ('y22',y22,'geodesic-bvp.json')

   cdblib.put ('y30',y30,'geodesic-bvp.json')
   cdblib.put ('y32',y32,'geodesic-bvp.json')
   cdblib.put ('y33',y33,'geodesic-bvp.json')

   cdblib.put ('y40',y40,'geodesic-bvp.json')
   cdblib.put ('y42',y42,'geodesic-bvp.json')
   cdblib.put ('y43',y43,'geodesic-bvp.json')
   cdblib.put ('y44',y44,'geodesic-bvp.json')

   cdblib.put ('y50',y50,'geodesic-bvp.json')
   cdblib.put ('y52',y52,'geodesic-bvp.json')
   cdblib.put ('y53',y53,'geodesic-bvp.json')
   cdblib.put ('y54',y54,'geodesic-bvp.json')
   cdblib.put ('y55',y55,'geodesic-bvp.json')

   end_stage_2a = time.time()

\end{cadabra}

% note that:
%   y00 = y20 = y30 = y40 = y50
%   y22 = y32 = y42 = y52
%   y33 = y43 = y53
%   y44 = y54
%   y55

\clearpage

\begin{dgroup*}
   \begin{dmath*} \cdb*{y50.201} \end{dmath*}  % unchanged
   \begin{dmath*} \cdb*{y52.301} \end{dmath*}
   \begin{dmath*} \cdb*{y53.301} \end{dmath*}
   \begin{dmath*} \cdb*{y54.301} \end{dmath*}
   \begin{dmath*} \cdb*{y55.301} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Stage 2b: Building the terms of $x^a(s)$}

\begin{cadabra}
   def substitute_y (obj):
       substitute (obj,defy00)
       substitute (obj,defy20)
       substitute (obj,defy30)
       substitute (obj,defy32)
       substitute (obj,defy40)
       substitute (obj,defy42)
       substitute (obj,defy43)
       distribute (obj)
       return obj

   beg_stage_2b = time.time()

   term2 := Gam^{a}_{b c} y4^{b} y4^{c}.
   term3 := Gam^{a}_{b c d} y3^{b} y3^{c} y3^{d}.
   term4 := Gam^{a}_{b c d e} y2^{b} y2^{c} y2^{d} y2^{e}.
   term5 := Gam^{a}_{b c d e f} y0^{b} y0^{c} y0^{d} y0^{e} y0^{f}.

   term2 = substitute_eps (term2)   # cdb (term2.401,term2)
   term3 = substitute_eps (term3)   # cdb (term3.401,term3)
   term4 = substitute_eps (term4)   # cdb (term4.401,term4)
   term5 = substitute_eps (term5)   # cdb (term5.401,term5)

   term2 = substitute_y (term2)
   term3 = substitute_y (term3)
   term4 = substitute_y (term4)
   term5 = substitute_y (term5)

   term2 = substitute_gam (term2)
   term3 = substitute_gam (term3)
   term4 = substitute_gam (term4)
   term5 = substitute_gam (term5)

   term2 = tidy (term2)   # cdb (term2.501,term2)
   term3 = tidy (term3)   # cdb (term3.501,term3)
   term4 = tidy (term4)   # cdb (term4.501,term4)
   term5 = tidy (term5)   # cdb (term5.501,term5)

\end{cadabra}

\clearpage
\begin{dgroup*}
   \begin{dmath*} \cdb*{term2.401} \end{dmath*}
   \begin{dmath*} \cdb*{term3.401} \end{dmath*}
   \begin{dmath*} \cdb*{term4.401} \end{dmath*}
   \begin{dmath*} \cdb*{term5.401} \end{dmath*}
\end{dgroup*}

\clearpage
\begin{dgroup*}
   \begin{dmath*} \cdb*{term2.501} \end{dmath*}
   \begin{dmath*} \cdb*{term3.501} \end{dmath*}
   \begin{dmath*} \cdb*{term4.501} \end{dmath*}
   \begin{dmath*} \cdb*{term5.501} \end{dmath*}
\end{dgroup*}

\clearpage

\begin{cadabra}
   # Check:
   #    x^{a} at s=1 should equal x^{a} + Dx^{a}
   #    but x^{a}(s) = x^{a} + s y^{a} - \sum (1/n!) @(termn) s^n
   #    thus foo should equal Dx^{a} and it does (yeah)

   foo := @(y5)
        -   (1/2) @(term2)
        -   (1/6) @(term3)
        -  (1/24) @(term4)
        - (1/120) @(term5).

   distribute         (foo)
   obj = product_sort (foo)
   rename_dummies     (foo)
   canonicalise       (foo)     # cdb (foo.001,foo)

   term2 :=   (1/2) @(term2).   # cdb(term2.502,term2)
   term3 :=   (1/6) @(term3).   # cdb(term3.502,term3)
   term4 :=  (1/24) @(term4).   # cdb(term4.502,term4)
   term5 := (1/120) @(term5).   # cdb(term5.502,term5)

   end_stage_2b = time.time()

\end{cadabra}

\begin{dgroup*}
   \begin{dmath*} \cdb*{foo.001} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \cdb*{y2.301} \end{dmath*}
   \begin{dmath*} \cdb*{y3.301} \end{dmath*}
   \begin{dmath*} \cdb*{y4.301} \end{dmath*}
   % \begin{dmath*} \cdb*{y5.301} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Stage 3: Reformatting and output}

\begin{cadabra}
   def get_Rterm (obj,n):

   # I would like to assign different weights to \nabla_{a}, \nabla_{a b}, \nabla_{a b c} etc. but no matter
   # what I do it appears that Cadabra assigns the same weight to all of these regardless of the number of subscripts.
   # It seems that the weight is assigned to the symbol \nabla alone. So I'm forced to use the following substitution trick.

       Q_{a b c d}::Weight(label=numR,value=2).
       Q_{a b c d e}::Weight(label=numR,value=3).
       Q_{a b c d e f}::Weight(label=numR,value=4).
       Q_{a b c d e f g}::Weight(label=numR,value=5).

       tmp := @(obj).

       distribute (tmp)

       substitute (tmp, $\nabla_{e f g}{R_{a b c d}} -> Q_{a b c d e f g}$)
       substitute (tmp, $\nabla_{e f}{R_{a b c d}} -> Q_{a b c d e f}$)
       substitute (tmp, $\nabla_{e}{R_{a b c d}} -> Q_{a b c d e}$)
       substitute (tmp, $R_{a b c d} -> Q_{a b c d}$)

       foo := @(tmp).
       bah = Ex("numR = " + str(n))
       keep_weight (foo, bah)

       substitute (foo, $Q_{a b c d e f g} -> \nabla_{e f g}{R_{a b c d}}$)
       substitute (foo, $Q_{a b c d e f} -> \nabla_{e f}{R_{a b c d}}$)
       substitute (foo, $Q_{a b c d e} -> \nabla_{e}{R_{a b c d}}$)
       substitute (foo, $Q_{a b c d} -> R_{a b c d}$)

       return foo

   def reformat (obj,scale):
       foo  = Ex(str(scale))
       bah := @(foo) @(obj).
       distribute     (bah)
       bah = product_sort (bah)
       rename_dummies (bah)
       canonicalise   (bah)
       substitute     (bah,$Dx^{b}->zzz^{b}$)
       factor_out     (bah,$x^{a?},zzz^{b?}$)
       substitute     (bah,$zzz^{b}->Dx^{b}$)
       ans := @(bah) / @(foo).
       return ans

   def rescale (obj,scale):
       foo  = Ex(str(scale))
       bah := @(foo) @(obj).
       distribute  (bah)
       substitute  (bah,$Dx^{b}->zzz^{b}$)
       factor_out  (bah,$x^{a?},zzz^{b?}$)
       substitute  (bah,$zzz^{b}->Dx^{b}$)
       return bah

   beg_stage_3 = time.time()

   Rterm22 = get_Rterm (term2,2)                           # cdb(Rterm22.101,Rterm22)
   Rterm23 = get_Rterm (term2,3)                           # cdb(Rterm23.101,Rterm23)
   Rterm24 = get_Rterm (term2,4)                           # cdb(Rterm24.101,Rterm24)
   Rterm25 = get_Rterm (term2,5)                           # cdb(Rterm25.101,Rterm25)

   Rterm32 = get_Rterm (term3,2)                           # cdb(Rterm32.101,Rterm32)  # zero
   Rterm33 = get_Rterm (term3,3)                           # cdb(Rterm33.101,Rterm33)
   Rterm34 = get_Rterm (term3,4)                           # cdb(Rterm34.101,Rterm34)
   Rterm35 = get_Rterm (term3,5)                           # cdb(Rterm35.101,Rterm35)

   Rterm42 = get_Rterm (term4,2)                           # cdb(Rterm42.101,Rterm42)  # zero
   Rterm43 = get_Rterm (term4,3)                           # cdb(Rterm43.101,Rterm43)  # zero
   Rterm44 = get_Rterm (term4,4)                           # cdb(Rterm44.101,Rterm44)
   Rterm45 = get_Rterm (term4,5)                           # cdb(Rterm45.101,Rterm45)

   Rterm52 = get_Rterm (term5,2)                           # cdb(Rterm52.101,Rterm52)  # zero
   Rterm53 = get_Rterm (term5,3)                           # cdb(Rterm53.101,Rterm53)  # zero
   Rterm54 = get_Rterm (term5,4)                           # cdb(Rterm54.101,Rterm54)  # zero
   Rterm55 = get_Rterm (term5,5)                           # cdb(Rterm55.101,Rterm55)

   Rterm22 = rescale ( reformat (Rterm22,   -3),    -3 )   # cdb(Rterm22.102,Rterm22)
   Rterm23 = rescale ( reformat (Rterm23,  -24),   -24 )   # cdb(Rterm23.102,Rterm23)
   Rterm24 = rescale ( reformat (Rterm24, -720),  -720 )   # cdb(Rterm24.102,Rterm24)
   Rterm25 = rescale ( reformat (Rterm25, -360),  -360 )   # cdb(Rterm25.102,Rterm25)

   Rterm33 = rescale ( reformat (Rterm33,  -12),   -12 )   # cdb(Rterm33.102,Rterm33)
   Rterm34 = rescale ( reformat (Rterm34, -720),  -720 )   # cdb(Rterm34.102,Rterm34)
   Rterm35 = rescale ( reformat (Rterm35,-1080), -1080 )   # cdb(Rterm35.102,Rterm35)

   Rterm44 = rescale ( reformat (Rterm44, -180),  -180 )   # cdb(Rterm44.102,Rterm44)
   Rterm45 = rescale ( reformat (Rterm45,-2160), -2160 )   # cdb(Rterm45.102,Rterm45)

   Rterm55 = rescale ( reformat (Rterm55, -360),  -360 )   # cdb(Rterm55.102,Rterm55)
\end{cadabra}

\clearpage

\begin{cadabra}
   # ----------------------------------------------------------------
   # bvp to terms linear in R

   tmp2 := -(1/3) @(Rterm22).

   bvp2 := x^{a}
        + s Dx^{a}
        + (s-s**2) @(tmp2).                                  # cdb(bvp.601,bvp2)

   cdblib.put ('bvp2',bvp2,'geodesic-bvp.json')
   cdblib.put ('bvp22',tmp2,'geodesic-bvp.json')

   y2 := Dx^{a} + @(tmp2).                                   # cdb(y2.600,y2)

   # ----------------------------------------------------------------
   # bvp to terms linear in dR

   tmp2 :=  -(1/3) @(Rterm22) - (1/24) @(Rterm23).
   tmp3 := -(1/12) @(Rterm33).

   bvp3 := x^{a}
        + s Dx^{a}
        + (s-s**2) @(tmp2)
        + (s-s**3) @(tmp3).                                  # cdb(bvp.602,bvp3)

   cdblib.put ('bvp3',bvp3,'geodesic-bvp.json')
   cdblib.put ('bvp32',tmp2,'geodesic-bvp.json')
   cdblib.put ('bvp33',tmp3,'geodesic-bvp.json')

   y3 := Dx^{a} + @(tmp2) + @(tmp3).                         # cdb(y3.600,y3)

   # ----------------------------------------------------------------
   # bvp to terms linear in d^2 R

   tmp2 :=   -(1/3) @(Rterm22) -  (1/24) @(Rterm23) - (1/720) @(Rterm24).
   tmp3 :=  -(1/12) @(Rterm33) - (1/720) @(Rterm34).
   tmp4 := -(1/180) @(Rterm44).

   bvp4 := x^{a}
        + s Dx^{a}
        + (s-s**2) @(tmp2)
        + (s-s**3) @(tmp3)
        + (s-s**4) @(tmp4).                                  # cdb(bvp.603,bvp4)

   cdblib.put ('bvp4',bvp4,'geodesic-bvp.json')
   cdblib.put ('bvp42',tmp2,'geodesic-bvp.json')
   cdblib.put ('bvp43',tmp3,'geodesic-bvp.json')
   cdblib.put ('bvp44',tmp4,'geodesic-bvp.json')

   y4 := Dx^{a} + @(tmp2) + @(tmp3) + @(tmp4).               # cdb(y4.600,y4)

   # ----------------------------------------------------------------
   # bvp to terms linear in d^3 R

   tmp2 := @(term2).
   tmp3 := @(term3).
   tmp4 := @(term4).
   tmp5 := @(term5).

   bvp5 := x^{a}
        + s Dx^{a}
        + (s-s**2) @(tmp2)
        + (s-s**3) @(tmp3)
        + (s-s**4) @(tmp4)
        + (s-s**5) @(tmp5).                                  # cdb(bvp.604,bvp5)

   cdblib.put ('bvp5',bvp5,'geodesic-bvp.json')
   cdblib.put ('bvp52',term2,'geodesic-bvp.json')
   cdblib.put ('bvp53',term3,'geodesic-bvp.json')
   cdblib.put ('bvp54',term4,'geodesic-bvp.json')
   cdblib.put ('bvp55',term5,'geodesic-bvp.json')

   y5 := Dx^{a} + @(tmp2) + @(tmp3) + @(tmp4) + @(tmp5).     # cdb(y5.600,y5)

   end_stage_3 = time.time()

   # cdbBeg (timing)
   print ("Stage 1:  {:7.1f} secs\\hfill\\break".format(end_stage_1-beg_stage_1))
   print ("Stage 2a: {:7.1f} secs\\hfill\\break".format(end_stage_2a-beg_stage_2a))
   print ("Stage 2b: {:7.1f} secs\\hfill\\break".format(end_stage_2b-beg_stage_2b))
   print ("Stage 3:  {:7.1f} secs\\hfill\\break".format(end_stage_3-beg_stage_3))
   # cdbEnd (timing)

\end{cadabra}

\clearpage

% -------------------------------------------------------------------------------------------------
\subsection*{Non-unit tangent vectors at $P$}

These are not unit vectors, their length is the geodesic distance from $P$ to $Q$

\begin{dgroup*}
   \begin{dmath*} \cdb*{y2.600} \end{dmath*}
   \begin{dmath*} \cdb*{y3.600} \end{dmath*}
   \begin{dmath*} \cdb*{y4.600} \end{dmath*}
   % \begin{dmath*} \cdb*{y5.600} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Geodesic boundary value problem to terms linear in $R$}

\begin{dgroup*}
   \begin{dmath*} x^{a}(s) = \cdb{bvp.601} + \BigO{s^3,\eps^3} \end{dmath*}
\end{dgroup*}

\begin{align*}
   x^{a}(s) &= x^{a} + s Dx^{a}
                     + (s-s^2) x^{a}_2
                     + \BigO{s^3,\eps^3}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_2 = \nx{2}^{a}_2 + \BigO{\eps^3} \end{dmath*}
   \begin{dmath*} -3 \nx{2}^{a}_2 = \cdb{Rterm22.102} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Geodesic boundary value problem to terms linear in $\nabla R$}

\begin{dgroup*}
   \begin{dmath*} x^{a}(s) = \cdb{bvp.602} + \BigO{s^4,\eps^4} \end{dmath*}
\end{dgroup*}

\begin{align*}
   x^{a}(s) &= x^{a} + s Dx^{a}
                     + (s-s^2) x^{a}_2
                     + (s-s^3) x^{a}_3
                     + \BigO{s^4,\eps^4}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_2 = \nx{2}^{a}_2 + \nx{3}^{a}_2 + \BigO{\eps^4} \end{dmath*}
   \begin{dmath*}   -3 \nx{2}^{a}_2 = \cdb{Rterm22.102} \end{dmath*}
   \begin{dmath*}  -24 \nx{3}^{a}_2 = \cdb{Rterm23.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_3 = \nx{3}^{a}_3 + \BigO{\eps^4} \end{dmath*}
   \begin{dmath*}   -12 \nx{3}^{a}_3 = \cdb{Rterm33.102} \end{dmath*}
\end{dgroup*}

% =================================================================================================
\section*{Geodesic boundary value problem to terms linear in $\nabla^2 R$}

\begin{dgroup*}
   \begin{dmath*} x^{a}(s) = \cdb{bvp.603} + \BigO{s^5,\eps^5} \end{dmath*}
\end{dgroup*}

\begin{align*}
   x^{a}(s) &= x^{a} + s Dx^{a}
                     + (s-s^2) x^{a}_2
                     + (s-s^3) x^{a}_3
                     + (s-s^4) x^{a}_4
                     + \BigO{s^5,\eps^5}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_2 = \nx{2}^{a}_2 + \nx{3}^{a}_2 + \nx{4}^{a}_2 + \BigO{\eps^5} \end{dmath*}
   \begin{dmath*}   -3 \nx{2}^{a}_2 = \cdb{Rterm22.102} \end{dmath*}
   \begin{dmath*}  -24 \nx{3}^{a}_2 = \cdb{Rterm23.102} \end{dmath*}
   \begin{dmath*} -720 \nx{4}^{a}_2 = \cdb{Rterm24.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_3 = \nx{3}^{a}_3 + \nx{4}^{a}_3 + \BigO{\eps^5} \end{dmath*}
   \begin{dmath*}   -12 \nx{3}^{a}_3 = \cdb{Rterm33.102} \end{dmath*}
   \begin{dmath*}  -720 \nx{4}^{a}_3 = \cdb{Rterm34.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_4 = \nx{4}^{a}_4 + \BigO{\eps^5} \end{dmath*}
   \begin{dmath*}  -180 \nx{4}^{a}_4 = \cdb{Rterm44.102} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
\section*{Geodesic boundary value problem to terms linear in $\nabla^3 R$}

The geodesic that connects the points with RNC coordinates $x^a$ and $x^a+Dx^a$ is described, for $0\le s\le 1$, by
%
% \begin{dgroup*}
%    \begin{dmath*} x^{a}(s) = \cdb{bvp.604} + \BigO{s^6,\eps^6} \end{dmath*} % too big for pdfLaTeX
% \end{dgroup*}
%
\begin{align*}
   x^{a}(s) &= x^{a} + s Dx^{a}
                     + (s-s^2) x^{a}_2
                     + (s-s^3) x^{a}_3
                     + (s-s^4) x^{a}_4
                     + (s-s^5) x^{a}_5
                     + \BigO{s^6,\eps^6}
\end{align*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_2 = \nx{2}^{a}_2 + \nx{3}^{a}_2 + \nx{4}^{a}_2 + \nx{5}^{a}_2 + \BigO{\eps^6} \end{dmath*}
   \begin{dmath*}   -3 \nx{2}^{a}_2 = \cdb{Rterm22.102} \end{dmath*}
   \begin{dmath*}  -24 \nx{3}^{a}_2 = \cdb{Rterm23.102} \end{dmath*}
   \begin{dmath*} -720 \nx{4}^{a}_2 = \cdb{Rterm24.102} \end{dmath*}
   \begin{dmath*} -360 \nx{5}^{a}_2 = \cdb{Rterm25.102} \end{dmath*}
\end{dgroup*}

\clearpage

\begin{dgroup*}
   \begin{dmath*} x^{a}_3 = \nx{3}^{a}_3 + \nx{4}^{a}_3 + \nx{5}^{a}_3 + \BigO{\eps^6} \end{dmath*}
   \begin{dmath*}   -12 \nx{3}^{a}_3 = \cdb{Rterm33.102} \end{dmath*}
   \begin{dmath*}  -720 \nx{4}^{a}_3 = \cdb{Rterm34.102} \end{dmath*}
   \begin{dmath*} -1080 \nx{5}^{a}_3 = \cdb{Rterm35.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_4 = \nx{4}^{a}_4 + \nx{5}^{a}_4 + \BigO{\eps^6} \end{dmath*}
   \begin{dmath*}  -180 \nx{4}^{a}_4 = \cdb{Rterm44.102} \end{dmath*}
   \begin{dmath*} -2160 \nx{5}^{a}_4 = \cdb{Rterm45.102} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} x^{a}_5 = \nx{5}^{a}_5 + \BigO{\eps^6} \end{dmath*}
   \begin{dmath*} -360 \nx{5}^{a}_5 = \cdb{Rterm55.102} \end{dmath*}
\end{dgroup*}

\clearpage

% =================================================================================================
% export selected objects, these will later be imported into a library
% these are the objects that will appear in the paper

\begin{cadabra}
   tmp2 := 8 @(Rterm22) + @(Rterm23).
   tmp3 := @(Rterm33).

   factor_out     (tmp2,$Dx^{a?}$) # cdb(tmp2.001,tmp2)
   rename_dummies (tmp2)
   factor_out     (tmp2,$Dx^{a?}$) # cdb(tmp2.002,tmp2)

   bvp4 := x^{a}
        + \lam Dx^{a}
        - (1/24) (\lam-\lam**2) @(tmp2)
        - (1/12) (\lam-\lam**3) @(tmp3).     # cdb(bvp4,bvp4)

   cdblib.create ('geodesic-bvp.export')

   # 4th order bvp
   cdblib.put ('bvp4',bvp4,'geodesic-bvp.export')

   # 6th order bvp terms, scaled
   cdblib.put ('bvp622',Rterm22,'geodesic-bvp.export')
   cdblib.put ('bvp623',Rterm23,'geodesic-bvp.export')
   cdblib.put ('bvp624',Rterm24,'geodesic-bvp.export')
   cdblib.put ('bvp625',Rterm25,'geodesic-bvp.export')

   cdblib.put ('bvp633',Rterm33,'geodesic-bvp.export')
   cdblib.put ('bvp634',Rterm34,'geodesic-bvp.export')
   cdblib.put ('bvp635',Rterm35,'geodesic-bvp.export')

   cdblib.put ('bvp644',Rterm44,'geodesic-bvp.export')
   cdblib.put ('bvp645',Rterm45,'geodesic-bvp.export')

   cdblib.put ('bvp655',Rterm55,'geodesic-bvp.export')

   checkpoint.append (bvp4)

   checkpoint.append (Rterm22)
   checkpoint.append (Rterm23)
   checkpoint.append (Rterm24)
   checkpoint.append (Rterm25)

   checkpoint.append (Rterm33)
   checkpoint.append (Rterm34)
   checkpoint.append (Rterm35)

   checkpoint.append (Rterm44)
   checkpoint.append (Rterm45)

   checkpoint.append (Rterm55)
\end{cadabra}

\clearpage

% =================================================================================================
\section*{Timing}

\cdb{timing}

% =================================================================================================
% export checkpoints in json format

\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   for i in range( len(checkpoint) ):
      cdblib.put ('check{:03d}'.format(i),checkpoint[i],checkpoint_file)
\end{cadabra}
\egroup

\end{document}


% To compile: pdflatex file.tex
\documentclass[11pt]{article}
\usepackage{fullpage}
\usepackage{pgffor}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{mathtools}
\usepackage{verbatim}
\usepackage{appendix}
\usepackage{graphicx}
\usepackage{color}
\usepackage{subfig}
\usepackage{url} % for underscore in footnote
\usepackage[UKenglish]{isodate} % for: \today
\cleanlookdateon                % for: \today
\usepackage{natbib} % Can remove if no bibliography. bibtex
%\pagestyle{empty} % Removes page number. Graphs too big.

\def\wl{\par \vspace{\baselineskip}\noindent}
% Keeping Figures in the right place %%%%%%%
\usepackage{float}                        %%
\def\beginmyfig{\begin{figure}[H]\center} %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\endmyfig{\end{figure}}
\def\ds{\displaystyle}
\def\tu{\textunderscore}
\definecolor{grey}{rgb}{.2,.2,.2}
\definecolor{lgrey}{rgb}{.8,.8,.8}
\def\hline{ \textcolor{lgrey}{\hrulefill} }
\newcommand{\m}[1]{\mathbf{\bm{#1}}} % Serif bold math
\def\ds{\displaystyle}
\def\inv{^{\raisebox{.2ex}{$\scriptscriptstyle-1$}}}
%\def\pm{^{\raisebox{.2ex}{$\scriptscriptstyle\prime$}}}
\def\norm#1{\left\lVert#1\right\rVert}
\newcommand{\p}[1]{\left(#1\right)}
\newcommand{\bk}[1]{\left[#1\right]}
\newcommand{\bc}[1]{ \left\{#1\right\} }
\newcommand{\abs}[1]{ \left|#1\right| }
\newcommand{\mat}{ \begin{pmatrix} }
\newcommand{\tam}{ \end{pmatrix} }

% \def for THIS ASSIGNMENT!!!%%%%%%%%%%%%%%%%%%%

\begin{document}
% my title:
\begin{center}
  {\huge \textbf{Take Home Final (AMS 205B)}
    %\footnote{\url{https://github.com/luiarthur/bnp_hw/project/bnp_spatialDP}}
  }\\
  \wl
  \noindent\today\\
  Arthur Lui\\
  \hline
\end{center}

\def\ci{\cos(wx_i+\delta)}
\def\si{\sin(wx_i+\delta)}
\def\hi{\alpha-\beta\ci}
\def\hci{\cos(\hat w x_i+\delta)}
\def\hsi{\sin(\hat w x_i+\delta)}
\def\hhi{\alpha-\beta\hci}
\def\suml{\sum_{i=1}^n}
\def\mlevec{(\alpha,\beta,w,\delta,\sigma^2)}
\def\hmlevec{\hat\alpha,\hat\beta,\hat w,\hat\delta,\hat\sigma^2}

\subsection*{1a)}
The MLE for ($\alpha,\beta,w,\delta,\sigma^2$) = $\underset{\alpha,\beta,w,\delta,\sigma^2}{\text{argmax}}\log f(\bm y|\bm x,\alpha,\beta,w,\delta,\sigma^2)$.
\[
\begin{aligned}
  &= \underset{\alpha,\beta,w,\delta,\sigma^2}{\text{argmax}}\bc{-\frac{n}{2} \log(\sigma^2) - \frac{\sum_{i=1}^n \p{y_i-\alpha-\beta \cos\p{wx_i+\delta}}^2}{2\sigma^2}}\\
\end{aligned}
\]

\noindent
The MLE is not available in closed form because the MLE's of $w$ and $\delta$ cannot be obtained in closed form. For example, to calculate the MLE of $w$ is obtained as follows:\\

\noindent
Let $l$ be the log likelihood $\log f(x|\alpha,\beta,w,\delta,\sigma^2)$. Then
\[
\begin{aligned}
  l_w &=  -\beta\si\frac{\suml\p{y_i-\hi}}{\sigma^2}\\
  l_w &:=0\\
  \Rightarrow -\beta\hsi\frac{\suml\p{y_i-\hhi}}{\sigma^2}&=0\\
\end{aligned}
\]

\noindent
So, there is no obvious closed form solution for $\hat w$, and hence \textbf{no closed form expression for the MLE of} ($\alpha,\beta,w,\delta,\sigma^2$). Similarly, no closed form solution is available for the MLE of $\delta$.\\

\noindent
With some algebra and linear model theory, the MLE's for $\alpha$, $\alpha$ and $\beta$ can be computed to be:
\[
\begin{aligned}
  \hat\sigma^2 &= \frac{\suml\p{y_i-\hi}^2}{n}\\
  \begin{pmatrix} \alpha\\\beta\\ \end{pmatrix} &= (C'C)^{-1}C'\mathbf y
\end{aligned}
\]
\noindent where $C=\begin{pmatrix}\mathbf 1 & \mathbf x\end{pmatrix}$.\\

\noindent The MLE of $\mlevec$ can be solved for by optimizing the log
likelihood profile with respect to $w$ and $\delta$. This was implemented in R
using the ``optim" function with a grid of initial values and the negative
likelihood profile as the function to minimize. The hessian matrix was also
returned to compute the second derivaties for computing the standard errors
later. The grid of initial values to test was determined by plotting the
bivariate log likelihood profile evaluated at $w,\delta$. It can be seen from
Figure \ref{fig:grid} that the maximum occurs periodically along $\delta$ and
at $w$ between 1.7 and 1.8. The support of $\delta$ was picked to be
($-\pi,\pi$) since that is most logical for testing the hypothesis in the later
problem. The $100 \times 100$ grid points plotted in Figure \ref{fig:grid} were used as
starting values for the optimizer. The $(w,\delta)$ pair that maximized the
likelihood profile was $(1.75, -.0373)$. \textbf{The resulting MLE in the
orbits data set for} $\mlevec=(1.97,-1.50, 1.75, -.0373, .0198)$.

\beginmyfig
  \includegraphics[scale=.5]{../code/output/grid.pdf}
  \caption{\small Log likelihood profile evaluated at various $\delta$ and $w$ values. log likelihoods range from -58 to 147. Regions in darkred correspond to a higher log likelihood.}
  \label{fig:grid}
\endmyfig

\noindent
New data was simulated using the MLE's to visually examine the goodness of fit.

\beginmyfig
  \includegraphics[scale=.5]{../code/output/mle.pdf}
  \caption{\small Fitted model plotted against data.}
  \label{fig:mle}
\endmyfig

\subsection*{1b)}
In order to use the wald's test normal approximation, we need to check the 6 regularity conditions.

\begin{itemize}
  \item[1.]{Under the model assumptions, $\epsilon_i \sim N(0,\sigma^2)$, so we have iid observations.}
  \item[2.]{The parameters are identifiable as long as constraints are appropriately defined. Specifically, when $w$ is positive and $\delta$ is bound between $-\pi$ and $\pi$, the parameters are identifiable.}
  \item[3.]{The support of the likelihood is indeed independent of the parameters.}
  \item[4.]{In this problem, when $w$ and $\delta$ are at the bounds, it is not practical or meaningful to estimate the parameters. When the frequency $w$ is 0, we don't see a relationship between $x$ and $y$. It may be the case that the true $\delta$ is $\pi$ or $-\pi$. But in problems involving seasons in trends, usually someone with subject matter expertise can inform us of possible trends. So we can avoid modelling problems with a shift parameter at the boundaries.}
  \item[5.]{The likelihood involves linear terms and $w$ and $\delta$ are associated with a trigonometric function in a way such that it is infinitely differentiable with respect to $w$ and $\delta$.}
  \item[6.]{Not sure what to show here...}
\end{itemize}

\noindent
Since the regularity conditions hold, 95\% Confidence intervals for $w$ and
$\delta$ can be constructed by inverting a Wald-like test can be obtained by
computing
\[
\begin{aligned}
  w &= \hat w \pm z_{.025} SE(\hat w) \\
  \delta &= \hat \delta \pm z_{.025} SE(\hat \delta) \\
\end{aligned}
\]
where $\hat w$ and $\hat\delta$ are the MLE's for $w$ and $\delta$
respectively. The standard errors for $\hat w$ and $\delta$ can be computed as
$\bk{I(\hat w)}^{-1/2}$ and $\bk{I(\hat\delta)}^{-1/2}$, where $I(\hat w)$ and
$I(\hat \delta)$ are the observed inverse informations. The observed inverse
informations are obtained from the hessian output of the optim function used in
estimating the MLE's. The following are the results.\\

\noindent
A 95\% Confidence interval for $w$ based on the Wald-like test is (\textbf{1.749, 1.756}).\\
A 95\% Confidence interval for $\delta$ based on the Wald-like test is (\textbf{-0.06396, -0.01061}).

\subsection*{1c)}
A parametric bootstrap algorithm can be implemented in the following way:\\

\begin{itemize}
  \item Initialize $(\hmlevec)^{(0)} = (\hmlevec)$.
  \item Repeat for $b$ = $1$ to $B$, some big number
  \begin{itemize}
    \item Draw $\bm y^{(b)} \sim \bm f(y|\bm x,\hmlevec)$
    \item set $(\hmlevec)^{(b)}= \underset{\alpha,\beta,w,\delta,\sigma^2}{\text{argmax}}\log f(\bm y^{(b)}|\bm x,\mlevec)$
  \end{itemize}
\end{itemize}

\noindent
A 95\% Confidence interval for $w$ based on bootstrap is (\textbf{1.716, 1.804}).\\
A 95\% Confidence interval for $\delta$ based on bootstrap is (\textbf{-0.3922, 0.2558}).\\

\noindent
The bootstrapped 95\% confidence intervals contain the intervals constructed
from a Wald-like test based on the Normal approximation. The 95\% confidence
interval for $\delta$ estimated using bootstrap includes 0, while that estimated
using the wald-like test does not.

\subsection*{1d)}
Typically, the Wald-like test intervals tend to be narrower than the truth when
$n$ is small. In any case, the bootstrap interval is more conservative. So in
this situation where the wald interval for $\delta$ does not include 0 but the
bootstrap interval for $\delta$ includes 0, and where the upper bound for
$\delta$ in the wald interval is so close to 0, I would conclude that $\delta$
is \textbf{not significantly different from 0}.

\subsection*{2a)}
\[
\begin{aligned}
  \lambda(\bm x, \bm y) &= \frac{\underset{\lambda_1-\lambda_2 < 0}{\sup} f(\lambda_1, \lambda_2 | \bm x, \bm y)}{\underset{\lambda_1-\lambda_2 \ne 0}{\sup} f(\lambda_1, \lambda_2 | \bm x, \bm y)}\\
\end{aligned}
\]

\noindent
Under the exponential distribution, the parameter values that maximize the likelihoods are the MLE's for
parameters. In this case, since the two samples are independent, the MLE's are just $\bar x, \bar y$. When
$\bar x < \bar y$, $\lambda(\bm x, \bm y) = 1$ since the numerator and denominators become the same values.
The non trivial case and case we examine is when $\bar x \ge \bar y$. When $\bar x \ge \bar y$, the parameters
that maximize the likelihood are equal. Then, to solve for the parameters in the constrained case,
\[
\begin{aligned}
  L&=\lambda_0^{-2n}\exp\p{-\frac{\suml x_i + y_i}{\lambda_0}}\\
  \Rightarrow l=\log L &= -2n\log(\lambda_0) - \frac{\suml x_i + y_i}{\lambda_0}\\
  \\
  l' &:=0 \\
  \Rightarrow -\frac{2n}{\hat\lambda_0} + \frac{\suml x_i + y_i}{\hat\lambda_0^2} &= 0 \\
  \Rightarrow -2n\hat\lambda_0 + \suml x_i + y_i &= 0 \\
  \Rightarrow \hat\lambda_0 &= \frac{\suml x_i + y_i}{2n} =  \frac{\bar x + \bar y}{2}
\end{aligned}
\]
Therefore, to compute the LRT,
\def\xbar{\bar x}
\def\ybar{\bar y}
\[
\begin{aligned}
  \lambda(\bm x, \bm y) &= \frac{f(\hat\lambda_0, \hat\lambda_0 | \bm x, \bm y)}{f(\bar x, \bar y | \bm x, \bm y)}\\
                        &= \frac{\hat\lambda_0^{-2n}\exp\p{-\frac{\suml x_i + \suml y_i}{\hat\lambda_0}}}{\bar x^{-n} \bar y^{-n}\exp\p{-\suml x_i/\bar x - \suml y_i /\bar y}}\\
                        &= \frac{\hat\lambda_0^{-2n}}{\bar x^{-n} \bar y^{-n}}\\
                        &= \p{\frac{4\bar x \bar y}{(\bar x + \bar y)^2}}^n \\
                        &= \p{\frac{(\bar x + \bar y)^2}{4\bar x \bar y}}^{-n} \\
                        &= \p{\frac{1}{4}\p{1+\frac{\ybar}{\xbar}}\p{1+\frac{\xbar}{\ybar}}}^{-n}\\
                        &= 4^n\p{1+\frac{\ybar}{\xbar}}^{-n}\p{\frac{\ybar/\xbar}{1+\ybar/\xbar}}^n\\
\end{aligned}
\]
Note that under $H_0$, $\frac{\bar Y}{\bar X} = \frac{2\suml
Y_i/(2\lambda_1n)}{2\suml X_i/(2\lambda_1n)} =
\frac{\chi^2_{(2n)}/2n}{\chi^2_{(2n)}/2n} \sim F_{2n,2n}$. Furthermore, it can
be shown the expression above is monotone decreasing in $\ybar/\xbar$ by taking
the derivative w.r.t. the statistic.  So, $\lambda(\bm x,\bm y) < c \iff
\frac{\bar Y}{\bar X} < c$ Therfore, the LRT rejects $H_0$ when $\lambda(\bm x,
\bm y) =
4^n\p{1+\frac{\ybar}{\xbar}}^{-n}\p{\frac{\ybar/\xbar}{1+\ybar/\xbar}}^n > c$,
where $c$ is some constant between 0 and 1. The p-value can be computed as
follows:
\[
\begin{aligned}
  \text{p-value} &= P\p{\Lambda(\bm X,\bm Y) < \lambda(\bm x,\bm y)}\\
                 &= P\p{\frac{\bar Y}{\bar X} < \frac{\ybar}{\xbar}}\\
                 &= P\p{F < \frac{\ybar}{\xbar}}\\
\end{aligned}
\]
where $F\sim F_{2n,2n}$.

\subsection*{2b)}
A wald statistic can be defined as
\[
\begin{aligned}
  Z_n &= \frac{\bar X - \bar Y}{SE(\bar X - \bar Y)} \\
      &= \frac{\bar X - \bar Y}{\sqrt{SE(X)^2 + SE(Y)^2}}\\
      &= \frac{\bar X - \bar Y}{\sqrt{Var(X)/n + Var(Y)/n}}\\
  \\
  \Rightarrow \text{p-value} = P(Z < z_n) &= \Phi\p{\frac{\bar x - \bar y}{\sqrt{var(x)^2/n + var(y)^2/n}}}
\end{aligned}
\]

%z_stat_wald <- (mean(X) - mean(Y)) / sqrt( (mean(X)^2 + mean(Y)^2) / N)
%(p_val_wald <- pnorm(z_stat_wald,lower=FALSE))

\subsection*{2c)}
A permutation test can be implemented by repeating the following steps many times (e.g. 10000):
\begin{itemize}
  \item Relabel the observations (into one of the two groups)
  \item Compute the difference of the means of two groups
\end{itemize}
Computing the proportion of times the sampled difference in means is less than
the actual observed difference in means will give a p-value for this test.

\subsection*{2d)}
For the lifetime data, the p-values using the LRT, Wald statistic, and
permutation test were computed using the above formulas to be:

\begin{center}
\begin{tabular}{c|ccc}
  & LRT & Wald & Permutation \\
  \hline & \hline & \hline & \hline \\
  p-value  & .045 & .0331 &.0365
\end{tabular}
\end{center}

\noindent
The wald and permutation tests agree. At the 95\% confidence level, we would
conclude that the mean lifetime of $x$ is greater than that of $y$.
The LRT p-value is much greater than the other two p-values.

\subsection*{3a)}
\[
\begin{aligned}
  \beta(\theta) &= P_\theta(T>t_\alpha) \\
  \Rightarrow &= P_\theta(\frac{\sqrt{n} (\bar X - \theta_0) }{S} > t_\alpha) \\
  \Rightarrow &= P_\theta(\frac{\sqrt{n} (\bar X - \theta) }{S} > t_\alpha + \frac{\sqrt{n} (\theta_0-\theta)}{S}) \\
  \Rightarrow &= P_\theta(T_{n-1} > t_\alpha + \frac{\sqrt{n} (\theta_0-\theta)}{S}) \\
\end{aligned}
\]
where $T_{n-1}$ is a $t$-distribution with $n-1$ degrees of freedom. And
$t_\alpha$ is chosen for the appropriate size.  That is $t_\alpha =
\Psi_{n-1}^{-1}(1-\alpha)$, where $\Psi_{n}^{-1}(x)$ is the inverse CDF of the
t distribution with $n$ degrees of freedom.

\subsection*{3b)}
\[
\begin{aligned}
  \beta(\theta) &= P_\theta(T>z_\alpha) \\
  \Rightarrow &= P_\theta(\frac{\sqrt{n} (\bar X - \theta_0) }{S} > z_\alpha) \\
  \Rightarrow &= P_\theta(\frac{\sqrt{n} (\bar X - \theta) }{S} > z_\alpha + \frac{\sqrt{n} (\theta_0-\theta)}{S}) \\
  \Rightarrow &= P_\theta(T_{n-1} > z_\alpha + \frac{\sqrt{n} (\theta_0-\theta)}{S}) \\
\end{aligned}
\]
where $T_{n-1}$ is a $t$-distribution with $n-1$ degrees of freedom. And $z_\alpha$ is chosen for the appropriate size.
That is $z_\alpha = \Phi^{-1}(1-\alpha)$.

\subsection*{3c)}
In Figure \ref{fig:power} below, both power curves are plotted for $n=10,100$, and $1000$. The power of the normal
test is greater than that of the t test for small $n$. As $n$ increases, the powers are approximately equal. At
$\theta = \theta_0$, the power is $\alpha$ for both curves.

\beginmyfig
  \includegraphics[scale=.5]{../code/output/power.pdf}
  \caption{\small Power function.}
  \label{fig:power}
\endmyfig

\subsection*{3d)}
The T test above is \textbf{not uniformly most powerful} because for small $n$,
the power $\beta(\theta)$ is not always greater than the power
($\beta'(\theta)$) for all $\theta \in \Theta_0$. For example as seen in Figure
$\ref{fig:power}$, when $n=10$, and $\theta > \theta_0$, the power for the test
based on the Normal test statistic is greater than that of the T test
statistic. \\

\noindent
To detect an increase in the mean (w.r.t. $\theta_0$) equivalent to two times
the true standard deviation of the data with probability 0.8, we need to solve
for the sample size under those conditions when the power is 0.8.

\[
\begin{aligned}
  \beta(\theta) &= .8 \\
  \Rightarrow P_\theta(T_{n-1} > t_\alpha + \frac{\sqrt{n} (\theta_0-(\theta_0+2s))}{s}) &= .8\\
  \Rightarrow P_\theta(T_{n-1} > t_\alpha - 2\sqrt{n}) &= .8\\
  \Rightarrow P_\theta(T_{n-1} > t_{.05} - 2\sqrt{n}) &= .8\\
\end{aligned}
\]

\noindent
Since $n$ cannot be solved analytically, we can compute the power function for different values of $n$ to see
when the power is above 0.8.

\begin{table}[ht]
\centering
\begin{tabular}{rc}
  n & Power \\
  \hline & \hline \\
  1 & 0.06 \\
  2 & 0.18 \\
  3 & 0.37 \\
  4 & 0.54 \\
  5 & 0.66 \\
  6 & 0.75 \\
  \textbf 7 & \textbf{0.81} \\
\end{tabular}
\end{table}

\noindent
When $\mathbf{n=7}$, we can detect detect an increase in the mean equivalent to two
times the true standard deviation of the data with probability 0.8.
\end{document}


\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mwe}

\title{Bayesian Inference in Determining Edge Importance}
\author{Shalin Patel}

\begin{document}
\maketitle
\section{Methodolgy Specification}
Suppose we are given a model $f$ that performs a classification task and outputs logits for $c$ classes. $f$ is a function of some input $x \in V$, $m \in B^e$, and $G$ where $B = \{0, 1\}$ and $e$ represents the number of edges in the underlying graph used by $f$. Hence $m$ represents the mask for the computational graph $G$. We also suppose that $f(x, m, G) = l$ where $l \in \mathbb{R}^c$.

At a high level the model is as follows. First we fix the output of the full graph as $f(x, \{1, 1, \dots, 1\}, G) = L$ and call this the base prediction. We assume that the edge mask is actually described by a random vector $M$ such that the prior distribution of $M$ can be described by
\[M_i \sim \text{Bernoulli}(p_0), \forall M_i \in M\]
where $p_0$ is usually a small number. In current experiments $p_0 = 0.1$. As $M$ is a random vector, we know that $f(x, M, G) = l$ is also a random vector with a particular prior distribution determined by the prior of $M$. We want $l$ to be as close as possible to the base prediction $L$. Indeed, we use the following
\[
L_i \sim N(l_i, \sigma_0^2)
\]
In the experiments we set $\sigma_0^2 = 0.0001$ to ensure that the posterior distribution of the observations remains as close to the base prediction as possible. With this model, we then used a Particle Gibbs sampler from the Turing package in Julia to compute the posterior over the initial edge mask. Since the parameter $p$ is set low for all of the elements of the mask, a posterior distribution that increases $p$ for that edge would imply that such an edge was important in aligning the model output with the base prediction.
\section{Preliminary Results}
There are a few goals for using this approach
\begin{enumerate}
	\item Provide for causal inference in a graph structure w.r.t to a machine learning task
	\item Provide more compact and meaningful interpretation results than that given by GNNExplainer
	\item Provide for an inference method that allows for parallelization and greater utilization of multi-core architectures
	\item Provides for more variability in edge importance wherein values can occupy the space between 0 and 1 rather than being forced to either end
\end{enumerate}
The following images show the outputs from GNNExplainer and the method described above on a Cora Dataset. As you can see the method above reaches the goals we set out with.
\begin{figure}[h!]
	\centering
	\begin{minipage}{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{gnn_exp}
        \caption{GNN Explainer}
    \end{minipage}\hfill
    \begin{minipage}{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{bayes}
        \caption{Bayesian Method}
    \end{minipage}
\end{figure}
\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}

\title{Finding centroid of irregularly shaped 2D object}
\author{William Wang and Duy Nguyen}
\date{20 May 2016}

\begin{document}

\maketitle
\begin{abstract}
    In this paper, we investigate the method to balance an object. Using measurements from the real world, we used the magic of a calculator to regress the curved nature of the 2D object to a quartic equation. This enable us to apply the formula and find the center of mass of the object, and thus balance it.
\end{abstract}
\section{Introduction}
    For this paper, we obtained a 2D object, which have three straight edges and one curve.
\section{Straight edges}
    The object was composed of four equation, three of which are stright lines. The equation for the straight line are presented thusly:

    $$ x = 0 $$
    $$ x = 20 $$
    $$ y = 0 $$

    The other non-linear equation is set to be approximate in the next section.
\section{Curve regression}
    To find the equation for the curve, we first need to gather some data of the curve.

    The data obtained is presented in the appendix. Using this data and a TI-Nspire CX CAS, we obtained three different regression equation that could potentially matched the data.
    \begin{center}
    \textbf{Linear}
    $$ y = 0.01848x + 11.5381 $$
    $$ r^2 = 0.018848 $$
    \textbf{Quadratic}
    $$ y = -0.01959x^2 + 0.420201x + 10.1321 $$
    $$ r^2 = 0.582572 $$
    \textbf{Quartic}
    $$ y = 1.44\cdot10^{-4}x^4 - 0.002676335x^3 - 0.04064956x^2 + 0.879152x + 9.05122141$$
    $$ r^2 =  0.997008211 $$
    \end{center}

    Therefore, we chose the quartic equation for its high $r^2$ value.
    To make things clearer, we graphed these equations with the scatter plot of the data.
    \begin{center}
    \includegraphics[width=10cm]{download}
    \end{center}
\section{Finding the center of mass}
    To find the center of mass, we must find the x-center ($\bar{x}$)and the y-center ($\bar{y}$) of the object.

    The equation we selected to use is the quartic equation.
    $$ f(x) = 1.44\cdot10^{-4}x^4 - 0.002676335x^3 - 0.04064956x^2 + 0.879152x + 9.05122141$$

    First, we must find the area:
    $$Area = \int_{a}^{b} f(x) dx $$
    $$\int_{0}^{20} 1.44\cdot10^{-4}x^4 - 0.002676335x^3 - 0.04064956x^2 + 0.879152x + 9.05122141  dx $$
    $$Area = 233.801$$

    $$\bar{x} = \frac{1}{Area} \int_{a}^{b} xf(x) dx$$
    $$\bar{x} = 10.076 $$
    $$\bar{y} = \frac{1}{Area} \int_{a}^{b} \frac{1}{2} (f(x)^2 - g(x)^2) dx$$
    $$\bar{y} = 5.874 $$

    Therefore, the center of mass should be at \textbf{(10.076, 5.874)}.

\pagebreak
\section{Appendix: Data table for the curve}
    Using graph paper and ruler, we have measured the curve as follow:
\begin{table}[ht]
\begin{minipage}[b]{0.45\linewidth}\centering
\begin{tabular}{c|c}
x (cm)	&	y (cm) \\ \hline
0	&	9.4 \\
0.5	&	9.9 \\
1	&	10.3 \\
1.5	&	10.7 \\
2	&	11 \\
2.5	&	11.3 \\
3	&	11.5 \\
3.5	&	11.8 \\
4	&	12 \\
4.5	&	12.1 \\
5	&	12.3 \\
5.5	&	12.4 \\
6	&	12.55 \\
6.5	&	12.7 \\
7	&	12.7 \\
7.5	&	12.7 \\
8	&	12.7 \\
8.5	&	12.7 \\
9	&	12.7 \\
9.5	&	12.7 \\
10	&	12.6 \\
    \end{tabular}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.45\linewidth}
\centering
\begin{tabular}{c|c}
   x (cm)	&	y (cm) \\ \hline
10.5	&	12.45 \\
11	&	12.3 \\
11.5	&	12.2 \\
12	&	12.1 \\
12.5	&	12 \\
13	&	11.8 \\
13.5	&	11.7 \\
14	&	11.6 \\
14.5	&	11.5 \\
15	&	11.4 \\
15.5	&	11.3 \\
16	&	11.2 \\
16.5	&	11.2 \\
17	&	11.2 \\
17.5	&	11.2 \\
18	&	11.2 \\
18.5	&	11.3 \\
19	&	11.5 \\
19.5	&	11.8 \\
20	&	12.1 \\
    \end{tabular}
\end{minipage}
\end{table}
\end{document}


\documentclass[12]{scrartcl}
\usepackage{amssymb,amsmath,gensymb,dsfont,calc,multicol,fullpage,nbaseprt}
\makeatletter
\newcommand\Aboxed[1]{
   \@Aboxed#1\ENDDNE}
\def\@Aboxed#1&#2\ENDDNE{%
   &
   \settowidth\@tempdima{$\displaystyle#1{}$}
   \setlength\@tempdima{\@tempdima+\fboxsep+\fboxrule}
   \kern-\@tempdima
   \boxed{#1#2}
}
\makeatother

\begin{document}

\title{Homework 1, Section 1.2: 2, 5, 9, 21, 29}
\author{Alex Gordon}
\date{\today}
\maketitle

\section*{Homework}
\subsection*{2. A)}
\begin{multicols}{2}
\begin{align*}
a_n &= 5n - 2
\\a_{k - 1} &= 5(k - 1) - 2
\\ & = 5k - 5 - 2 \\
\Aboxed{&= 5k - 7}
\end{align*}
\begin{align*}
\\a_n &= 5n - 2
\\ a_{k + 1} &= 5(k + 1) - 2
\\ & = 5k + 5 - 2 \\
\Aboxed{& = 5k +3 }
\end{align*}
\end{multicols}

\subsection*{2. B)}
\begin{multicols}{2}
\begin{align*}
a_n &= 3n^2 + n
\\ a_{k - 1} &= 3(k - 1)^2 + (k - 1)
\\ &= 3(k - 1)(k - 1) + (k - 1) \\
\Aboxed{& = 3k^2 - 5k + 2 }
\end{align*}
\begin{align*}
\\ a_n &= 3n^2 + n
\\ a_{k + 1} &= 3(k + 1)^2 + (k + 1)
\\ &= 3(k - 1)(k + 1) + (k + 1) \\
\Aboxed{&= 3k^2 + 7k + 4}
\end{align*}
\end{multicols}

\subsection*{2. C)}
\begin{multicols}{2}
\begin{align*}
a_n &= 2n + 7
\\ a_{k - 1} &= 2(k - 1) + 7
\\ &= 2k - 2 + 7 \\
\Aboxed{&= 2k + 5}
\end{align*}
\begin{align*}
\\ a_n &= 2n + 7
\\ a_{k + 1} &= 2(k + 1) + 7
\\ &= 2k + 2 + 7 \\
\Aboxed{&= 2a + 9}
\end{align*}
\end{multicols}

\subsection*{2. D)}
\begin{multicols}{2}
\begin{align*}
a_n &= 3^n + 4
\\ a_{k - 1} &= 3^{k - 1} + 4 \\
\Aboxed{&= 3^{k - 1} + 4}
\end{align*}
\begin{align*}
\\ a_n &= 3^n + 4
\\ a_{k + 1} &= 3^{k + 1} + 4 \\
\Aboxed{&= 3^{k + 1} + 4}
\end{align*}
\end{multicols}

\subsection*{2. E)}
\begin{multicols}{2}
\begin{align*}
a_n &= n^{3n +1} - 1
\\ a_{k - 1} &= n^{3(k - 1) +1} - 1
\\ &=  2^{3k - 3 +1} - 1\\
\Aboxed{&= 2^{3k - 2} - 1}
\end{align*}
\begin{align*}
\\ a_n &= n^{3n +1} - 1
\\ a_{k + 1} &= n^{3(k + 1) +1} - 1
\\ &= n^{3k + 3 +1} - 1 \\
\Aboxed{&= 2^{3k + 4} - 1}
\end{align*}
\end{multicols}

\subsection*{2. F)}
\begin{multicols}{2}
\begin{align*}
a_n &= \frac{n(2n - 1)(n + 2)}{6}
\\ a_{k - 1} &= \frac{k - 1(2(k - 1) - 1)((k - 1) + 2)}{6}
\\ &=  \frac{k - 1(2k - 2 - 1)(k +2)}{6}\\
\Aboxed{&= \frac{-2k^2-2k-3}{6}}
\end{align*}
\begin{align*}
\\a_n &= \frac{n(2n - 1)(n + 2)}{6}
\\ a_{k + 1} &= \frac{k + 1(2(k + 1) - 1)(k + 1 + 2)}{6}
\\ &= \frac{k + 1(2k + 2) - 1)(k + 3)}{6}\\
\Aboxed{&= \frac{2k^2 +8k +3}{6}}
\end{align*}
\end{multicols}

\subsection*{5. A)}
 2, 4, 6, 8, 10
\\ The pattern here is fairly obvious. It works out to \boxed{2n}.
\subsection*{5. B)}
1, 7, 13, 19, 25
\\There are a few answers to this pattern, however, the easiest one that I could find through experimenting is \boxed{6n - 5}
\subsection*{5. C)}
2, 5, 11, 23, 47
\\The pattern for this was a little harder to figure out. So to figure it out, I used the differences method. The differences are 3, 6, 12, 24.
\subsection*{5. D)}
2, 4, 16, 256, 65536
\\$2^{2^n}$
\subsection*{9. A)}
5, 11, 18, 26, 35
\\$a_n$ = $\frac{1}{2}$
\subsection*{9. B)}
1, 4, 9, 16, 25
$a_n = n^2$
\subsection*{9. C)}
-3, 5, 17, 33, 53
$a_n = 2n(n + 1) -7$
\subsection*{21. A)}
\begin{align*}
\sum\limits_{k=1}^7 &= 3k
\\ \sum\limits_{k=1}^7 &= 3(1 + 2 + 3 + 4 + 5 + 6 +7) \\
\Aboxed{\sum\limits_{k=1}^7 &= 84}
\end{align*}
\subsection*{21. B)}
\begin{align*}
\sum\limits_{k=1}^3 &= 3k
\\ \sum\limits_{k=1}^3 &= 3(1 + 2 + 3) \\
\Aboxed{\sum\limits_{k=1}^3 &= 42}
\end{align*}
\subsection*{21. C)}
\begin{align*}
\sum\limits_{k=1}^9 &= 4
\\ \sum\limits_{k=1}^9 &= 4(9) \\
\Aboxed{\sum\limits_{k=1}^9 &= 36}
\end{align*}
\subsection*{21. D)}
\begin{align*}
\sum\limits_{k=0}^4 &= \frac{1}{2^k} \\
\Aboxed{\sum\limits_{k=0}^4 &= \frac{31}{16}}
\end{align*}
\subsection*{21. E)}
\begin{align*}
\sum\limits_{k=-1}^3 &= 3- 2k\\
\Aboxed{\sum\limits_{k=-1}^3 &= 5}
\end{align*}
\subsection*{29. A)}
\begin{align*}
\\a_3 &= A + ABBA + BA\\
\Aboxed{&= ABBABA}
\end{align*}
\begin{align*}
\\a_4 &= A + a_{4-1} + BA \\
\Aboxed{&= AAABBABABA}
\end{align*}
\begin{align*}
\\a_5 &= A + a_{5-1} + BA \\
\Aboxed{&= AAAABBABABABA}
\end{align*}
\begin{align*}
\\a_6 &= A + a_{6-1} + BA \\
\Aboxed{&= AAAAABBABABABABA}
\end{align*}
\end{document}


\section{Stereo PIV Data processing}

PIV data were processed into text files containing lists of vectors and
positions from raw image data using
INSIGHT\textsuperscript{\textcopyright} software.
This format was considered to be the
starting point for the present analysis. Each vector is recorded as a row in a
"v3d" file, with a set of position coordinates,velocity
vector components, and two quality control flags that were used to reduce
spurious vector counts. An example of these data is shown in Table
\ref{table:v3d_row_example}.

\input{tables/v3d_row_example}

The position coordinates are expressed in units of millimeters away from
the fiducial mark on the target used for calibration. These coordinates are
expressed from the viewpoint of the cameras, which were pointed upstream;
positive $X$ coordinates are to the right, positive $Y$ is upwards, and
positive $Z$ is towards the cameras. Vector components are recorded as $U$,
$V$ and $W$.

\subsection{Reynolds Averaging}
Since each run contained 200 separate vector sets, the data needed to be
synthesized into unsteady velocity can enable examination of the Reynolds
Averaged Navier Stokes equations in cylindrical coordinates. This processing
was performed employing \textit{Python 2.7} and
\textit{Matlab}, with much of the code entirely replicated in each language.
First, text files with tables of vector data were loaded into a parser to
construct a mesh grid of the $X$, $Y$ coordinate space in a given plane. These
mesh grids established the relationship between matrix indices and real
coordinate space with units of $mm$.Then, for each test as referenced in Table
\ref{table:test_matrix_table}, data from
each of the 200 snapshots is Reynolds averaged to produce a mean velocity
component, and an associated fluctuating velocity components, that is

\begin{equation}
u(x,t) = \overline{u}(x) + u^\prime(x,t)
\label{eq:rans_components}
\end{equation}

Each component is given an individual variable name, with mean nonfluctuating
components determined employing simple averages of all sets expressed as the
component letter with an over bar ($\overline{v_x}$, $\overline{v_y}$,
$\overline{v_z}$), and
time averaged fluctuating component expressed with a prime
($\overline{v_x^\prime}$, $\overline{v_y^\prime}$, $\overline{v_z^\prime}$).
Velocities referring to just one of the 200 sets will use a
subscript $i$ as ($v_{x_i}$, $v_{y_i}$, $v_{z_i}$). The stable components are
computed with
a simple average to preserve the sign, while the fluctuating components are
computed with a root mean squared method as shown in Equations \ref{eq:ubar}
through \ref{eq:wprime}.

\begin{equation}
\overline{v_x}  = \frac{1}{N} \sum_{i=1}^{200} v_{x_i}
\label{eq:ubar}
\end{equation}

\begin{equation}
\overline{v_y}  = \frac{1}{N} \sum_{i=1}^{200} v_{y_i}
\end{equation}

\begin{equation}
\overline{v_z}  = \frac{1}{N} \sum_{i=1}^{200} v_{z_i}
\end{equation}

\begin{equation}
\overline{v_x^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200} (v_{x_i} -
\overline{v_x})^2}
\end{equation}

\begin{equation}
\overline{v_y^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200} (v_{y_i} -
	\overline{v_y})^2}
\end{equation}

\begin{equation}
\overline{v_z^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200} (v_{z_i} -
	\overline{v_z})^2}
\label{eq:wprime}
\end{equation}

where $\overline{v_x}$, $\overline{v_y}$ and $\overline{v_z}$ are the time
averaged velocity
components in the $X$, $Y$, $Z$ directions respectively, and
$\overline{v_x^\prime}$, $\overline{v_y^\prime}$ and $\overline{v_z^\prime}$
are the time averaged fluctuating velocity components.

Similarly, time averaged Reynolds stresses can be calculated by multiplying the
fluctuating components together before taking the root mean square average by
Equations \ref{eq:rs_uu} through \ref{eq:rs_vw}.

\begin{equation}
\overline{v_x^\prime v_x^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{x_i} - \overline{v_x})(v_{x_i} - \overline{v_x}))^2}
\label{eq:rs_uu}
\end{equation}

\begin{equation}
\overline{v_y^\prime v_y^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{y_i} - \overline{v_y})(v_{y_i} - \overline{v_y}))^2}
\end{equation}

\begin{equation}
\overline{v_z^\prime v_z^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{z_i} - \overline{v_z})(v_{z_i} - \overline{v_z}))^2}
\end{equation}

\begin{equation}
\overline{v_x^\prime v_y^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{x_i} - \overline{v_x})(v_{y_i} - \overline{v_y}))^2}
\end{equation}

\begin{equation}
\overline{v_x^\prime v_z^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{x_i} - \overline{v_x})(v_{z_i} - \overline{z_y}))^2}
\end{equation}

\begin{equation}
\overline{v_y^\prime v_z^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{y_i} - \overline{v_y})(v_{z_i} - \overline{z_y}))^2}
\label{eq:rs_vw}
\end{equation}

Turbulent kinetic energy $k$ can be calculated as one half of the sum of the
squares of the three instantaneous fluctuation components, taken over the set,
i.e.

\begin{equation}
k = \frac{1}{2} \left(\overline{v_x^\prime v_x^\prime}^2 +
	\overline{v_y^\prime v_y^\prime}^2 +
	\overline{v_z^\prime v_z^\prime}^2\right)
\label{eq:tke}
\end{equation}

Once all the turbulence statistics have been
generated in Cartesian coordinates, the
vortex core must be located in that survey plane in order to represent the
turbulence data in cylindrical coordinates. The core was established in terms
of the minimum in-plane velocity magnitudes near the center of the
interrogation plane, and excluding the stream-wise $w$ component. In the
present case, it was necessary to avoid confusion produced by spurious edge
values, and a threshold value was defined that limited the search for in-plane
velocity minima to the area surrounding the center of the field of view. Once
the lowest value was found, sub-grid accuracy was
achieved by taking utilizing cubic interpolation of the grid points surrounding
the minimum. This technique was sufficiently robust to automatically establish
the core axis correctly for every experiment.

Employing the location for the vortex core, new mesh grids in radial and
tangential coordinates, $R$ and $\theta$, were created from the $X$ and
$Y$ mesh grids. Velocity components, $v_r$ and $v_\theta$, were then
calculated by Equations \ref{eq:uv_r} and \ref{eq:uv_t}.

\begin{equation}
v_{r} = v_{x} \cos{(\theta)} + v_{y} \sin{(\theta)}
\label{eq:uv_r}
\end{equation}

\begin{equation}
v_{\theta} = v_{x} \sin{(\theta)} - v_{y} \cos{(\theta)}
\label{eq:uv_t}
\end{equation}

\noindent
where $v_{r}$ and $v_{\theta}$ represent a radial and tangential velocity
matrix, and $\theta$ is the mesh grid of angles
about the vortex core. These equations were applied on an element wise basis to
every grid point in the vector field. Once this was done, the same Reynolds
averaging techniques were applied to extract the radial and tangential velocity
components $v_{r}$ and $v_{\theta}$ into mean and fluctuating components in
Equations
\ref{eq:rbar} through \ref{eq:tprime}.

\begin{equation}
\overline{v_r}  = \frac{1}{N} \sum_{i=1}^{200} v_{r_i}
\label{eq:rbar}
\end{equation}

\begin{equation}
\overline{v_\theta}  = \frac{1}{N} \sum_{i=1}^{200} v_{\theta_i}
\label{eq:tbar}
\end{equation}

\begin{equation}
\overline{v_r^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200} (v_{r_i} -
\overline{v_r})^2}
\label{eq:rprime}
\end{equation}

\begin{equation}
\overline{v_\theta^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200} (v_{\theta_i}
-
\overline{v_\theta})^2}
\label{eq:tprime}
\end{equation}

The Reynolds stress correlations in cylindrical coordinates were calculated
directly from the transformed cylindrical velocities with equations
\ref{eq:rs_rr) through \ref{eq:rs_tw}.

\begin{equation}
\overline{v_{r}^\prime v_{r}^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{r_i} - \overline{v_{r}})(v_{r_i} - \overline{v_{r}}))^2}
\label{eq:rs_rr}
\end{equation}

\begin{equation}
\overline{v_{\theta}^\prime v_{\theta}^\prime} = \sqrt{\frac{1}{N-1}
\sum_{i=1}^{200}
	((v_{\theta_i} - \overline{v_{\theta}})(v_{\theta_i} -
	\overline{v_{\theta}}))^2}
\end{equation}

\begin{equation}
\overline{v_r^\prime v_\theta^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{r_i} - \overline{v_r})(v_{\theta_i} - \overline{v_\theta}))^2}
\end{equation}

\begin{equation}
\overline{v_r^\prime v_z^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{r_i} - \overline{v_r})(v_{z_i} - \overline{v_z}))^2}
\end{equation}

\begin{equation}
\overline{v_\theta^\prime v_z^\prime} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{200}
	((v_{\theta_i} - \overline{v_\theta})(v_{z_i} - \overline{v_z}))^2}
\label{eq:rs_tw}
\end{equation}

Once this processing was complete, a large set of Reynolds averaged
velocity data were available for each of the 70 test cases. Plots of these data
from experiment 55 will be employed in this thesis as a representative example.
Stream line plots can be produced as shown in
Figure \ref{fig:examp_stream}. Cartesian average velocity components for test
case are shown in Figures \ref{fig:examp_U} through \ref{fig:examp_W}, although
they are not particularly useful when compared to representations in
cylindrical coordinates.
Cylindrical average velocity components are shown in Figures \ref{fig:examp_R}
and \ref{fig:examp_T}. Reynolds stress distributions are shown in Figures
\ref{fig:examp_rt} through \ref{fig:examp_tw}. Variation of the fluctuating
magnitudes of the individual cylindrical velocity components are shown in
Figures \ref{fig:examp_rr} through \ref{fig:examp_ww}. Variation in the total
turbulent kinetic
energy $k$ is shown in Figure \ref{fig:examp_tke}. Scatter plots that
flatten the dataset into one dimension can be generated as in the tangential
velocity vs radial distance from the rotational axis is shown in Figure
\ref{fig:examp_Tscatter}.

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_stream}
	\caption{Example stream plot.}
	\label{fig:examp_stream}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_U_contour}
\caption{Example contour plot of $\overline{v_x}$.}
\label{fig:examp_U}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_V_contour}
\caption{Example contour plot of $\overline{v_y}$.}
\label{fig:examp_V}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_W_contour}
\caption{Example contour plot of $\overline{v_z}$.}
\label{fig:examp_W}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_R_contour}
\caption{Example contour plot of $\overline{v_r}$.}
\label{fig:examp_R}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_T_contour}
\caption{Example contour plot of $\overline{v_\theta}$.}
\label{fig:examp_T}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_rt_contour}
\caption{Example contour plot of $\overline{v_r^\prime v_\theta^\prime}$.}
\label{fig:examp_rt}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_rw_contour}
\caption{Example contour plot of $\overline{v_r^\prime v_z^\prime}$.}
\label{fig:examp_rw}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_tw_contour}
\caption{Example contour plot of $\overline{v_\theta^\prime v_z^\prime}$.}
\label{fig:examp_tw}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_rr_contour}
\caption{Example contour plot of $\overline{v_r^\prime v_r^\prime}$.}
\label{fig:examp_rr}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_tt_contour}
\caption{Example contour plot of $\overline{v_\theta^\prime v_\theta^\prime}$.}
\label{fig:examp_tt}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_ww_contour}
\caption{Example contour plot of $\overline{v_z^\prime v_z^\prime}$.}
\label{fig:examp_ww}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=4.25in]{figs/run_55/run_55_ctke_contour}
\caption{Example contour plot of turbulent kinetic energy $k$.}
\label{fig:examp_tke}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=6.5in]{figs/run_55/run_55_T_vs_r_mesh_ctkescatter}
\caption{Example scatter plot of $\bar{v_\theta}$, colored by $k$.}
\label{fig:examp_Tscatter}
\end{figure}

Insight to the quality and uncertainty in each measurement was gained by
examining the number of vectors successfully extracted from the set of 200
snapshots as shown in Figure \ref{fig:example_num_contour}. Locations with
fewer than twenty valid vectors in each set were omitted. A clear
relationship between the total turbulent kinetic energy, $k$, and the number of
vectors making up the average was observed, and will be discussed in Chapter
\ref{chapter:results}.

\begin{figure}[H]
	\centering
	\includegraphics[width=5in]{figs/example_vortex_figs/example_num_contour}
	\caption{Example contour plot showing the variation in the number of
	measurements within the interrogation plane.}
	\label{fig:example_num_contour}
\end{figure}

%ensures every page has "Figure         Page" at the top of it
\makeatletter
\addtocontents{lof}{%
      	\protect\afterpage{\protect\hbox to \linewidth%
      		{\noindent Figure \hfill Page}\par%
      		\protect\vspace{12\p@}}}
\makeatother


\documentclass[letterpaper]{article}

\usepackage[utf8x]{luainputenc}
\usepackage{aeguill}
%\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage{fullpage}
\usepackage{fancyhdr}
\setlength{\headheight}{12pt}
\pagestyle{fancy}
\chead{Linear Algebra}
\lhead{September 4, 2015}
\rhead{Jon Allen}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\section*{hyperplanes}
distance from origin to $H(\mathbf{x}_0,\mathbf{a})$. we are looking for a vector in the same direction as $\mathbf{a}$. Find $L(\mathbf{0},\mathbf{a})\cap H(\mathbf{x}_0,\mathbf{a})$. Now $\mathbf{x}\in L\cap H\Rightarrow x=t\mathbf{a} $ and $\mathbf{a}\cdot\mathbf{x}=\mathbf{a}\cdot\mathbf{x}_0\Rightarrow \mathbf{a}\cdot(t\mathbf{a})=\mathbf{a}\mathbf{x}_0\to t(\mathbf{a}\cdot\mathbf{a})=\mathbf{a}\mathbf{x}_0\to t=\frac{\mathbf{a}\cdot \mathbf{x}_0}{||a||^2}$

and now take $t\mathbf{a}=\text{proj}_a(\mathbf{x}_0)$ and $||t\mathbf{a}||=\frac{|a\cdot\mathbf{x}_0|}{||a||^2}||a|=\frac{|a\cdot\mathbf{x}_0|}{||a||}$

if we have another point $\mathbf{y}_0$ not on the hyperplane and we take $\text{proj}_{a}(\mathbf{x}_0-\mathbf{y}_0)$ then we have the distance from the hyperplane to $\mathbf{y}_0$
\end{document}


\subsection{Angled crack in an orthotropic body}

\paragraph{}
In this example, consider an orthotropic plate $(b/h = 1)$ with an angled center-crack of length $a/h = 0.5$ under uniform far field tension along the two opposite sides (see Fig.~\ref{iso_fig:angled_crack_geo_bc}).
    \begin{figure}
        \centering
        \scalebox{0.5}{
            \includegraphics{isogeometric_sbfem/images/angled_crack_geo_bc.eps}
        }
        \caption{Angled crack in a rectangular orthotropic body: geometry}
        \label{iso_fig:angled_crack_geo_bc}
    \end{figure}

\paragraph{}
The elastic properties of the material are $E_{11} = E_{22}= E_{33}=\SI{106.17}{\giga \pascal}$, $G_{12} = G_{23} = G_{13} = \SI{108.25}{\giga \pascal}$, and $\nu_{12} = \nu_{23} = \nu_{13} = 0.4009$.
The results are compared with those obtained in \citep{Banks2005}.
The convergence of mode \RN{1} and mode \RN{2} SIF with mesh size is shown in Tab.~\ref{iso_tab:angled_crack_convergence} for a crack at an angle $\alpha = \pi/12$.
The influence of the order of the shape functions is also shown.
It can be seen that increasing the number of degrees of freedom, the error in the numerical SIF decreases.
A very good agreement is observed.

\begin{table}
\caption{Convergence of the generalized stress intensity factors $
    (\mean{K_{\mathrm I}}, \mean{K_{\mathrm{II}}}) =
    (K_{\mathrm I},K_{\mathrm{II}}) /
    P\sqrt(\pi a)
    $}
\label{iso_tab:angled_crack_convergence}
\begin{tabularx}{\textwidth}{XXXXX}
    \toprule
    \multirow{2}{*}{Order}& & \multicolumn{3}{c}{Number of DOF} \\
    \cmidrule{3-5}
    & & 160 & 240 & 320 \\
    \multirow{2}{*}{$2$}    &   $\mean{K_{\mathrm I}}$   &   1.2770  &   1.2723  &   1.2706  \\
                            &   $\mean{K_{\mathrm{II}}}$   &   0.2918  &   0.2915  &   0.2913  \\
    \multirow{2}{*}{$4$}    &   $\mean{K_{\mathrm I}}$   &   1.2700  &   1.2697  &   1.2697  \\
                            &   $\mean{K_{\mathrm{II}}}$   &   0.2914  &   0.2913  &   0.2913  \\
    \multirow{2}{*}{$6$}    &   $\mean{K_{\mathrm I}}$   &           &   1.2668  &   1.2670  \\
                            &   $\mean{K_{\mathrm{II}}}$   &           &   0.2913  &   0.2913  \\
    \midrule
    \bottomrule
\end{tabularx}
\end{table}

\paragraph{}
Also, increasing the order of the NURBS basis functions, the error decreases.
The influence of the crack orientation α on the mode \RN{1} and mode \RN{2} SIF is shown in Tab.~\ref{iso_tab:angled_crack_sif_1} and Tab.~\ref{iso_tab:angled_crack_sif_2}.
A total of 80 control points are used with different order of the NURBS basis functions.
It can be seen that a very good agreement is observed.
\begin{table}
\caption{Generalized stress intensity factors $K_{\mathrm{I}} / P\sqrt{\pi a}$ of angled crack in rectangular orthotropic body}
\label{iso_tab:angled_crack_sif_1}
\begin{tabularx}{\textwidth}{XXXXX}
    \toprule
    Angle   &   $K_{\mathrm{I}}$  &   \multicolumn{3}{c}{ }  \\
    \cmidrule{2-5}
    $\alpha$&\citep{Banks2005}&  \multicolumn{3}{c}{Order of the curve}  \\
    \cmidrule{3-5}
    & & 2 & 4 & 6 \\
    \midrule
    0         & 1.3755 & 1.3603 & 1.3583 & 1.3583 \\
    $\pi/12 $ & 1.2692 & 1.2706 & 1.2697 & 1.2670 \\
    $\pi/ 6 $ & 1.0268 & 1.0277 & 1.0270 & 1.0270 \\
    $\pi/ 4 $ & 0.6952 & 0.6941 & 0.6944 & 0.6943 \\
    $\pi /3 $ & 0.3579 & 0.3582 & 0.3580 & 0.3580 \\
    $5\pi/12$ & 0.1095 & 0.0996 & 0.1003 & 0.1004 \\
    \bottomrule
\end{tabularx}
\end{table}

\begin{table}
    \caption{Generalized stress intensity factors $K_{\mathrm{II}}/P\sqrt{\pi a}$ of angled crack in rectangular orthotropic body}
    \label{iso_tab:angled_crack_sif_2}
    \begin{tabularx}{\textwidth}{XXXXX}
        \toprule
        Angle   &   $K_{\mathrm{II}}$  &   \multicolumn{3}{c}{ }\\
        \cmidrule{2-5}
        $\alpha$&\citep{Banks2005}&  \multicolumn{3}{c}{Order of the curve}\\
        \cmidrule{3-5}
        & & 2 & 4 & 6 \\
        \midrule
        0         & 0.0000 & 0.0000 & 0.0000 & 0.0000 \\
        $\pi/12 $ & 0.2912 & 0.2914 & 0.2913 & 0.2913 \\
        $\pi/ 6 $ & 0.5092 & 0.5087 & 0.5093 & 0.5092 \\
        $\pi/ 4 $ & 0.5807 & 0.5948 & 0.5946 & 0.5946 \\
        $\pi /3 $ & 0.5248 & 0.5247 & 0.5248 & 0.5248 \\
        $5\pi/12$ & 0.3108 & 0.3119 & 0.3117 & 0.3117 \\
        \bottomrule
    \end{tabularx}
\end{table}


\documentclass{article}
\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{ulem}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}

\title{Expectation Maximization for Mixture Model}
\author{dw}

\begin{document}
\maketitle
\section{Kernel Method}
Kernel methods owe their name to the use of kernel functions, which enable them to operate in a high-dimensional, implicit feature space without ever computing the coordinates of the data in that space, but rather by simply computing the inner products between the images of all pairs of data in the feature space. This operation is often computationally cheaper than the explicit computation of the coordinates. This approach is called the "kernel trick".

For models which are based on a fixed nonlinear feature space mapping φ(x), the kernel function is given
by the relation
\begin{equation}
	k(\bf{x},\bf{x}') = \phi(\bf{x})^T \phi(\bf{x}')
	\label{eq: kernel}
\end{equation}
The simplest example of a kernel function is obtained by considering the identity
mapping for the feature space in Eq. (\ref{eq: kernel}) so that $\phi(\bf{x}) = \bf{x}$, in which case $k(\bf{x}, \bf{x}') = \bf{x}^T \bf{x}'$. We shall refer to this as the linear kernel.

For hyperplane $(w,b)$, the functional margin of sample point $(x_i, y_i)$ is
\begin{equation}
	\hat{\gamma}_i = y_i (\frac{\bf{w}}{\lVert{\bf{w}}\rVert}\cdot x_i + \frac{b}{\lVert{\bf{w}}\rVert})
\end{equation}

\begin{equation}
	\max_{\bf{w},b} \gamma
\end{equation}
\begin{equation}
	s.t. y_i (\bf{w}\cdot x_i + b) \geq \hat{\gamma}
\end{equation}
\end{document}


\section{$\alpha$ and $\beta$ convention}

For systems with gain,
\begin{equation}
\tilde{\psi} \equiv \alpha \psi _1 + \beta \psi _2
\end{equation}
where $\psi _1$ is the wave function in the passive system with a center of localization at $\frac{1}{4}$L for the orientation where the wave exponentially grows to a peak before exponentially decaying to x/L=1 (left-to-right).

This then implies that $\psi _2$ is the wave function for a center of localization at $\frac{1}{4}$L for the orientation where the wave exponentially decays before growing to a peak and then decaying again (right-to-left).

\begin{figure}
\vskip -0.5cm
\centerline{
\scalebox{0.3}{\includegraphics{pictures/transmission_derivation_14_LR}}
\scalebox{0.3}{\includegraphics{pictures/transmission_derivation_34_RL}}}
\vskip -0.5cm
\caption{$\psi _1$ on the left and $\psi _2$ on the right for the passive models.}
\label{fig:alphabetacononicaldefectpositions}
\end{figure}

Similarily for $\tilde{ \psi} _1$ denoting the $\frac{1}{4}$L center of localization for left-to-right orientation and $\tilde{ \psi} _2$ being the $\frac{3}{4}$L center of localization for the right-to-left orientation.


\section{Eigenvalues, Eigenvectors and Invariant Subspaces}
\setcounter{subsection}{1}

\subsection{Eigenvectors and Upper-Triangular Matrices}
  \begin{lemma}
    \label{lemma:eigenvalue,polynomial}
    If $\lambda$ is an eigenvalue of $T\in\mathcal{L}(V)$ and $p$ is a
    polynomial, then $p(\lambda)$ is an eigenvalue of $p(T)$. Note that unlike
    the statement in exercise 11, $\mathbb{F}$ does not required to be
    $\mathbb{C}$.
  \end{lemma}
  \begin{proof}
    Suppose that $Tv=\lambda v$ for some $0\ne v\in V$, then
    \[
      p(T)v = \left(\sum_{k=0}^n a_kT^k \right)v =
      \sum_{k=0}^n a_kT^kv = \sum_{k=0}^n a_k\lambda^k v = p(\lambda)v.
    \]
    Hence, $p(\lambda)$ is an eigenvalue of $p(T)$.
  \end{proof}

  \paragraph{1.}
  \begin{proof}
    $\,$\\
    (a) Since $T^n=0$ and
    \[
      (I-T)(I+T+\cdots+T^{n-1}) = I+T+\cdots+T^{n-1} - T-\cdots-T^n
      = I - T^n = I,
    \]
    $I-T$ is invertible and $(I-T)\inv=I+T+\cdots+T^{n-1}$. \\
    (b) The power series expansion of the function $(1-x)\inv$ at $x=0$ is
    $1 + x + \cdots + x^n + \cdots$.
  \end{proof}

  \paragraph{3.}
  \begin{proof}
    Since $1$ is the only eigenvalue of $T^2=I$ and $-1$ is not an eigenvalue of
    $T$, by \lemmaref{lemma:eigenvalue,polynomial}, $1$ is the only eigenvalue
    of $T$ and therefore $T=I$.
  \end{proof}

  \paragraph{5.}
  \begin{proof}
    Since $(STS\inv)^k = S(T(S\inv S)TS\inv\cdots ST)S\inv = ST^kS\inv$,
    \[
      p(STS\inv) = \sum_{k=0}^n a_k(STS\inv)^k = \sum_{k=0}^n a_kST^kS\inv
      = Sp(T)S\inv.
    \]
  \end{proof}

  \paragraph{7.}
  \begin{proof}
    It follows immediately from \lemmaref{lemma:eigenvalue,polynomial}.
  \end{proof}

  \paragraph{9.}
  \begin{proof}
    Since $p(T)v=0=0v$, $0$ is an eigenvalue of $T$. Then by
    \lemmaref{lemma:eigenvalue,polynomial}, some of the zeros of $p$ are the
    eigenvalues of $T$. Assume that there exists some zero $x_0$ of $p$ that is
    not an eigenvalue of $p$. Then $q=p/(x-x_0)$ is a polynomial of degree less
    than $p$ and such that $q(T)v=0$. Contradiction. Hence, every zero of $p$ is
    an eigenvalue of $p$.
  \end{proof}

  \paragraph{11.}
    Note that the proof does not rely on 5.21.
  \begin{proof}
    Suppose that $\alpha$ is an eigenvalue of $p(T)$. If $p$ is a constant
    polynomial, then there is nothing to be proved. If $p$ is non-constant, then
    $p(x)-\alpha = c(x-\lambda_1)\cdots(x-\lambda_m)$ where $m\ge 1$. Since
    $\alpha$ is an eigenvalue of $p(T)$,
    \[
      (p-\alpha)(T)=c(T-\lambda_1 I)\cdots(T-\lambda_m I)
    \]
    is singular. Hence, at least one of $T-\lambda_1 I,\dots,T-\lambda_m T$,
    denoted by $T-\lambda_k I$, is singular and therefore $\lambda_k$ is an
    eigenvalue of $T$ and $p(\lambda_k)=\alpha$. \\
    The converse part is just \lemmaref{lemma:eigenvalue,polynomial}.
  \end{proof}

  \paragraph{13.}
  \begin{proof}
    Suppose that $U$ is a finite-dimensional $T$-invariant subspace of $W$. Then
    $T|_U$ is an operator on $U$, a complex vector space. Hence it has an
    eigenvalue as long as $U=\{0\}$. However, it does not and therefore $U =
    \{0\}$.
  \end{proof}

  \paragraph{15.}
  \begin{proof}
    $\begin{bsmallmatrix} 1 & 1 \\ 1 & 1 \end{bsmallmatrix}$.
  \end{proof}

  \paragraph{17.}
  \begin{proof}
    Let $\varphi$ be the map which takes $p\in\mathcal{P}_{n^2}(\mathbb{C})$ to
    $p(T)\in\mathcal{L}(V)$. It is linear since
    \[
      \varphi(a_1p_1+a_2p_2) = (a_1p_1+a_2p_2)(T) = a_1p_1(T)+a_2p_2(T)=
      a_1\varphi(p_1)+a_2\varphi(p_2).
    \]
    Since $\dim\mathcal{P}_{n^2}(\mathbb{C})=n^2+1$ ans $\dim\mathcal{L}(V) =
    n^2$, $\varphi$ is not injective by 3.23. Namely, there exsits nonequal $p_1
    ,p_2\in\mathcal{P}_{n^2}(\mathbb{C})$ such that $\varphi(p_1)=\varphi(p_2)$.
    Hence, $\varphi(p_1-p_2)=(p_1-p_2)(T)=0$ where $p_1-p_2$ is a nonzero
    polynomial, having zeros in $\mathbb{C}$. Since $0$ is the eigenvalue of
    $(p_1-p_2)(T)$, one of its zeros is an eigenvalue of $T$ by exercise 11.
  \end{proof}

% end

\iffalse
\subsection{Eigenspaces and Diagonal Matrices}
  \paragraph{1.}
  \begin{proof}
    Since $T$ is diagonalizable, $V$ has a basis $(v_1,\dots,v_n)$ consisting of
    eigenvectors of $T$. Suppose that $\lambda_1,\dots,\lambda_n$ are the
    eigenvalues corresponding to $v_1,\dots,v_n$ respectively and assume without
    loss of generality that $v_1,\dots,v_m\in\nul T$. For every $v\in V$,
    \[
      v=x_1v_1+\cdots+x_nv_n
      =(x_1v_1+\cdots+x_mv_m)
      +T\left( \frac{x_{m+1}}{\lambda_{m+1}}v_{m+1}+\cdots
      +\frac{x_n}{\lambda_n}v_n \right).
    \]
    Hence, $V=\nul T + \range T$. \par
    Meanwhile, as $\range T\subset E(\lambda_{m+1},T)\oplus\cdots\oplus
    E(\lambda_n, T)$, $\dim\range T\le n-m$. And clear that $\dim\nul T=m$.
    Hence, $\dim V\ge \dim\nul T+\dim\range T$ and therefore $V=\nul T\oplus
    \range T$.
  \end{proof}

  \paragraph{3.}
  \begin{proof}
    Clear that (a) implies (b) and (c); (b), together with the rank-nullity
    theorem, implies (a). Meanwhile, if $\nul T\cap\range T=\{0\}$, then
    \begin{align*}
      \dim(\nul T+\range T)
      &= \dim\nul T+\dim\range T-\dim(\nul T-\cap\range T) \\
      &= \dim\nul T+\dim\range T = \dim V.
    \end{align*}
    Namely, $V=\nul T\oplus\range T$. Hence, (a), (b) and (c) are equivalent.

  \end{proof}

  \paragraph{5.}
  \begin{proof}
    TODO
  \end{proof}

  \paragraph{7.}
  \begin{proof}
    TODO
  \end{proof}

  \paragraph{9.}
  \begin{proof}
    Since $T$ is invertible $v\in E(\lambda, T)$ iff $(T-\lambda I)v=0$ iff $Tv
    -\lambda v = 0$ iff $\lambda\inv v - T\inv v = 0$ iff $(T\inv-\lambda\inv)v
    =0$ iff $v\in E(\lambda\inv, T\inv)$.
  \end{proof}

  \paragraph{11.}
  \begin{proof}

  \end{proof}

% end
\fi


% !TEX root=../report.tex

\section{Side-by-side composition}

\begin{grammar}
  Expressions
    & e    & ::=& \ldots               & \\
    &      &\mid& \tuple{e_1, e_2}     & – pairs \\
  Values
    & v    & ::=& \ldots               & \\
    &      &\mid& \tuple{v_1, v_2}     & – pair value \\
  Pretasks
    & p    & ::=& \ldots               & \\
    &      &\mid& e_1 \And e_2         & – task pairing \\
  Tasks
    & t    & ::=& \ldots               & \\
    &      &\mid& t_1 \And t_2         & – task pairing \\
  Types
    & \tau & ::=& \ldots               & \\
    &      &\mid& \tau_1 \times \tau_2 & – product type \\
  Inputs
    & i    & ::=& a                    & – action \\
    &      &\mid& \First i             & – pass to first \\
    &      &\mid& \Second i            & – pass to second \\
\end{grammar}

\begin{equation*}
  \userule{T-And}
\end{equation*}

\begin{equation*}
  \userule{E-And}
\end{equation*}

\begin{equation*}
  \userule{S-And}
\end{equation*}

\begin{equation*}
  \begin{array}{lcl}
    \Value(t_1 \And t_2) &=& \left\{
      \begin{array}{ll}
        \tuple{v_1, v_2} & \when\ \Value(t_1) = v_1 \land \Value(t_2) = v_2 \\
        \bot             & \otherwise
      \end{array}
    \right.
  \end{array}
\end{equation*}

\begin{equation*}
  \begin{array}{lcl}
    \Failing(t_1 \And t_2) &=& \Failing(t_1) \lor \Failing(t_2)
  \end{array}
\end{equation*}

\begin{margintext}{Aside: Pairing in iTasks}
  $\And$ is similar to \type{-\&\&-}.
\end{margintext}

\begin{equation*}
  \userule{H-FirstAnd} \qquad \userule{H-SecondAnd}
\end{equation*}

% The rule \refrule{S-AndEval} covers the other three cases and makes sure both $t_1$ and $t_2$ will be normalised towards a value as much as possible.

\subsection{Intermezzo: monoidal functor and its laws}

\begin{equation*}
  \begin{array}{rclr}
    \Map \Snd\ (\Edit \unit \And t)
      &\approxeq& t
      & \text{(left identity)} \\
    \Map \Fst\ (t \And \Edit \unit)
      &\approxeq& t
      & \text{(right identity)} \\
    \Map \Assoc\ (r \And\ (s \And t))
      &\approxeq& (r \And s) \And t
      & \text{(associativity)} \\
    \Map\ (\lambda \tuple{x, y}. \tuple{g\ x, h\ y})\ (s \And t)
      &\approxeq& \Map\ g\ s \And \Map\ h\ t
      & \text{(naturality)} \\
  \end{array}
\end{equation*}

\subsection{Example: ...}