\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amsmath,amsfonts,amssymb,amsthm,epsfig,epstopdf,titling,url,array, tikz,tkz-berge, calrsfs}
\usepackage{tkz-graph}

\title{Matroids And their Graphs}
\author{o.mcdonnell4@nuigalway.ie }
\date{19 January 2018}

\theoremstyle{plain}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}
\newtheorem{prop}[thm]{Proposition}
\newtheorem*{cor}{Corollary}

\theoremstyle{definition}
\newtheorem{defn}{Definition}[section]
\newtheorem{conj}{Conjecture}[section]
\newtheorem{exmp}{Example}[section]

\theoremstyle{remark}
\newtheorem*{rem}{Remark}
\newtheorem*{note}{Note}

\newenvironment{rcases}
  {\left.\begin{aligned}}
  {\end{aligned}\right\rbrace}

\newcounter{excercise}
\newcounter{solution}
\newcounter{Question}

\newcommand\Excercise{%
  \textbf{Excercise:}~%
  \setcounter{solution}{0}%
}

\newcommand\TheSolution{%
  \textbf{Solution:}\\%
}

\newcommand\Question{%
    \textbf{Question:}~%
    \setcounter{Question}{0}%
}
\newcommand\Notation{%
  \textbf{Notation:}~%
}

\newcommand\Proof{%
    \textbf{Proof:}~%
}

\setlength{\droptitle}{-10em}

\begin{document}
\maketitle

 \section{Cardinality of maximal independent sets}

 \begin{thm}
 Show that if $\mathcal{I}$ is a non-empty hereditary set of subsets of a finite set E, then $(E,\mathcal{I})$ is a matroid if and only if, for all $X \subset E$, all maximal members of $\{I : I \in \mathcal{I} $ and $ I \subset X\}$ have the same number of elements.
 \end{thm}

\noindent \Proof $(\implies)$ Let $B_1 , B_2$ be maximal elements of $\{I : I \in \mathcal{I} $and $ I \subset X\}$ \\
\noindent And assume $|B_1| < |B_2|$
Then since $B_1, B_2 \in \mathcal{I}$
\\
There exists $e \in (B_2 \setminus B_1)$ such that $B_1 \cup \{e\} \in \mathcal{I}$
\\
This contradicts our maximality of $B_1$.\\

 \noindent $\implies$ All maximal elements of the set $\{I : I \in \mathcal{I} $ and $ I \subset X\}$ in our matroid M have the same cardinality.
 \\ \qed
 \end{document}


\section{Introduction}
  Online marketplaces can be categorized as centralized and decentralized.
  Two examples of each category are \href{http://www.ebay.com}{ebay} and \href{https://openbazaar.org/}{OpenBazaar}.
  The common denominator of established online marketplaces is that the reputation of each vendor and client is typically
  expressed in the form of stars and user-generated reviews that are viewable by the whole network.

  The goal of ``Trust Is Risk'' is to offer a reputation system for decentralized marketplaces where the trust each user gives
  to the other users is quantifiable in monetary terms. The central assumption used throughout this paper is that trust is
  equivalent to risk, or the proposition that $Alice$'s \textit{trust} in another user $Charlie$ is defined as the
  \textit{maximum sum of money} $Alice$ can lose when $Charlie$ is free to choose any strategy. To flesh out this concept, we
  will use \textit{lines of credit} as proposed by Sanchez \cite{loc}. $Alice$ joins the network by explicitly entrusting some
  money to another user, say her friend, $Bob$ (see Fig.~\ref{fig:bottleneckA} and~\ref{fig:bottleneckB}). If $Bob$ has
  already entrusted some money to a third user, $Charlie$, then $Alice$ indirectly trusts $Charlie$ since if the latter wished
  to play unfairly, he could have already stolen the money entrusted to him by $Bob$. We will later see that $Alice$ can now
  engage in economic interaction with $Charlie$.

  To implement lines-of-credit, we use Bitcoin \cite{bitcoin}, a decentralized cryptocurrency that differs from conventional
  currencies in that it does not depend on trusted third parties. All transactions are public as they are recorded on a
  decentralized ledger, the blockchain. Each transaction takes some coins as input and produces some coins as output. If the
  output of a transaction is not connected to the input of another one, then this output belongs to the UTXO, the set of
  unspent transaction outputs. Intuitively, the UTXO contains all coins not yet spent.
  \medskip \ \\
  \subimport{common/figures/}{simpleexample.tikz}
  \noindent We propose a new kind of wallet where coins are not exclusively owned, but are placed in shared accounts materialized
  through 1-of-2 multisigs, a bitcoin construct that permits any one of two pre-designated users to spend the coins contained
  within a shared account \cite{masteringbitcoin}. We use the notation 1/$\{Alice, Bob\}$ to represent a 1-of-2 multisig that
  can be spent by either $Alice$ or $Bob$. In this notation, the order of names is irrelevant, as either user can spend.
  However, the user who deposits the money initially into the shared account is relevant -- she is the one risking her money.

  Our approach changes the user experience in a subtle but drastic way. A user no more has to base her trust towards a
  store on stars or ratings which are not expressed in financial units. She can simply consult her wallet to decide whether
  the store is trustworthy and, if so, up to what value, denominated in bitcoin. This system works as follows: Initially
  $Alice$ migrates her funds from her private bitcoin wallet to 1-of-2 multisig addresses shared with friends she
  comfortably trusts. We call this direct trust. Our system is agnostic to the means players use to determine who is
  trustworthy for these direct 1-of-2 deposits. Nevertheless, these deposits contain an objective value visible to the network
  that can be used to deterministically evaluate subjective indirect trust towards other users.

  Suppose $Alice$ is viewing the listings of vendor $Charlie$. Instead of his stars, $Alice$ sees a positive value calculated
  by her wallet representing the maximum value she can safely pay to purchase from $Charlie$. This value, known as indirect
  trust, is calculated in Theorem~\ref{trustflow} -- Trust Flow. Indirect trust towards a user is not global but subjective;
  each user views a personalized indirect trust based on the network topology.  The indirect trust reported by our system
  maintains the following desired security property: If $Alice$ makes a purchase from $Charlie$, then she is exposed to no
  more risk than she was already taking willingly. The existing voluntary risk is exactly that which $Alice$ was taking by
  sharing her coins with her trusted friends. We prove this in Theorem~\ref{riskinv} -- Risk Invariance. Obviously it is not
  safe for $Alice$ to buy anything from any vendor if she has not directly entrusted any value to other users.

  In Trust Is Risk the money is not invested at the time of purchase and directly to the vendor, but at an earlier point in
  time and only to parties that are trustworthy for out of band reasons. The fact that this system can function in a
  completely decentralized fashion will become clear in the following sections. We prove this in Theorem~\ref{sybil} -- Sybil
  Resilience.

  We make the design choice that an entity can express her trust maximally in terms of her available capital. Thus, an
  impoverished player cannot allocate much direct trust to her friends, no matter how trustworthy they are. On the other hand,
  a rich player may entrust a small fraction of her funds to a player that she does not extensively trust and still exhibit
  more direct trust than the impoverished player. There is no upper limit to trust; each player is only limited by her funds.
  We thus take advantage of the following remarkable property of money: To normalise subjective human preferences into
  objective value.

  A user has several incentives to join. First, she has access to otherwise inaccessible stores. Moreover, two friends can
  formalize their mutual trust by directly entrusting the same amount to each other. A company that casually subcontracts
  others can express its trust towards them. Governments can choose to directly entrust citizens with money and confront them
  using a corresponding legal arsenal if they make irresponsible use of this trust. Banks can provide loans as outgoing and
  manage savings as incoming direct trust. Last, the network is an investment and speculation field since it constitutes a new
  area for financial activity.

  Observe that the same physical person can maintain multiple pseudonymous identities in the same trust network and that
  multiple independent trust networks for different purposes can coexist. \ifdefined\proceedings \else On the other hand, the
  same pseudonymous identity can be used to establish trust in different contexts.\fi

  Trust Is Risk is not just a theoretical conception, but can be deployed and applied in existing decentralized markets such
  as OpenBazaar. All the necessary bitcoin constructs such as multisigs are readily available. Our only concern pertains to
  the scalability of such an implementation, but we are confident that such difficulties can be overcome.


    \filetitle{!substitutions}{Define text substitutions}{modellang/substitutions}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
!substitutions
    SubsName := TextString;
    SubsName := TextString;
    ...
\end{verbatim}

\paragraph{Description}\label{description}

The \texttt{!substitutions} starts a block with substitution
definitions. The definition of each substitution must begin with the
name of the substitution, followed by a colon-equal sign, \texttt{:=},
and a text string ended with a semi-colon. The semi-colon is not part of
the substitution.

The substitutions can be used in any of the model equations, i.e.~in
\href{modellang/transitionequations}{transition equations},
\href{modellang/measurementequations}{measurement equations},
\href{modellang/dtrends}{deterministic trend equations}, and
\href{modellang/links}{dynamic links}. Each occurence of the name of a
substitution enclosed in dollar signs, i.e.
\texttt{\$substitution\_name\$}, in model equations will be replaced
with the text string from the substitution's definition.

Substitutions can also refer to other substitutions; make sure, though,
that they are not recursive. Also, remember to parenthesise the
definitions of the substitutions (or the references to them) in the
equations properly so that the resulting mathematical expressions are
evaluated properly.

\paragraph{Example}\label{example}

\begin{verbatim}
!substitution
    a := ((omega1+omega2)/(omega1+omega2+omega3));

!transition_equations
    X = $a$^2*Y + (1-$a$^2)*Z;
\end{verbatim}

In this example, we assume that \texttt{omega1}, \texttt{omega2}, and
\texttt{omega3} are declared as parameters. The equation will expand to

\begin{verbatim}
    X = ((omega1+omega2)/(omega1+omega2+omega3))^2*Y + ...
      (1-((omega1+omega2)/(omega1+omega2+omega3))^2)*Z;
\end{verbatim}

Note that if had not used the outermost parentheses in the definition of
the substitution, the resulting expression would not have given us what
we meant: The square operator would have only applied to the
denominator.


%===============================================================================
% $Id: ifacconf.tex 19 2011-10-27 09:32:13Z jpuente $
% Template for IFAC meeting papers
% Copyright (c) 2007-2008 International Federation of Automatic Control
%===============================================================================
\documentclass{ifacconf}

%\input{./macros.tex}
\usepackage{graphicx}      % include this line if your document contains figures
\usepackage{natbib}        % required for bibliography
\usepackage{amsmath, amsxtra, amsfonts,amscd,amssymb}
\setcounter{tocdepth}{3}
\usepackage{graphicx,wrapfig}
\usepackage{epstopdf}
\usepackage{url}
\usepackage[algo2e,linesnumbered, vlined,ruled]{algorithm2e}
\usepackage{float}
\usepackage{multirow}
\usepackage{mathrsfs}
%===============================================================================

\begin{document}
\begin{frontmatter}

\title{
	Linear Mixed-Effect Models}% \thanksref{footnoteinfo}}    % Title, preferably not more than 10 words.

%\thanks[footnoteinfo]{}

\author[First]{Saket Choudhary}

\address[First]{University of Southern California,
   LA, CA 90089 USA (e-mail: skchoudh@ usc.edu).}

\begin{abstract}                % Abstract of not more than 250 words.
\emph{Linear Mixed-Effect Models} are an extension of \emph{Linear Regression} that describe the relationship between response variable $\mathcal{Y}$ and independent variables ${X}$ such that the coefficients can vary with respect to one or more grouping variables and hence at least one independent covariate should be categorical.

Mixed-Effects models find use in \emph{longitudinal} or repeated measures study, where repeated measurements are made on \emph{experimental} or \emph{observational} units.

Mixed-Effects models make use of constrained optimisation to arrive at the Maximum Likelihood or Restricted Maximum Likelihood estimate of the parameters.

\end{abstract}

\end{frontmatter}

\section{Problem description}
Consider a Linear Regression Problem:
$$
\mathcal{Y} = X\beta + \epsilon
$$

Where $\epsilon \sim \mathcal{N}(0,\sigma^2I)$
and $\beta$ is a p-dimensional coefficient vector;
$X$ is $n \times p$ model matrix. There are two parameters
in this model: $\beta$ and $\sigma^2$

and hence for a linear model:
$$
y \sim \mathcal{N}({X\beta,\sigma^2I})
$$

Mixed-effects models the response with an additional "random-effect" $\mathcal{B}$ such that:

$$
(\mathcal{Y}|\mathcal{B}=b) = \mathcal{N}(XB+Zb, \sigma^2I)
$$
where $Z$ is a $n \times q$ model matrix just like $X$ but for the random-effect covariates $\mathcal{B}$ which we fix at $b$ and then model $b$ as another normal random variable:

$$
\mathcal{B} \sim \mathcal{N}(0,\Sigma)
$$

where $\Sigma$ is a parameterized $q\times q$ covariance matrix.
The parameter estimation now can be down by separating(profiling) the log-likelihood.(Details Skipped, since I do not understand them yet)

\section{Goals}

\begin{itemize}
	\item Understand the derivation/math behind parameter estimation
	\item Use available modeling libraries to demonstrate at least one use case  of mixed-effects models
\end{itemize}

\section{References}
\begin{itemize}
\item Pinherio, J. C., and D. M. Bates. Mixed-Effects Models in S and S-PLUS. Statistics and Computing Series, Springer, 2004.

\item Bates, Douglas, et al. "Fitting linear mixed-effects models using lme4." arXiv preprint arXiv:1406.5823 (2014).

\end{itemize}

\end{document}


\documentclass[11pt, a4]{article}
\usepackage{qtree}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{tikz}

\usetikzlibrary{positioning}
\newcommand\tab[1][0.5cm]{\hspace*{#1}}

%% define left/right/full outer join symbols
\def\ojoin{\setbox0=\hbox{$\bowtie$}%
  \rule[-.02ex]{.25em}{.4pt}\llap{\rule[\ht0]{.25em}{.4pt}}}
\def\leftouterjoin{\mathbin{\ojoin\mkern-5.8mu\bowtie}}
\def\rightouterjoin{\mathbin{\bowtie\mkern-5.8mu\ojoin}}
\def\fullouterjoin{\mathbin{\ojoin\mkern-5.8mu\bowtie\mkern-5.8mu\ojoin}}
\newcommand*{\QEDB}{\null\nobreak\hfill\ensuremath{\square}}%

\setcounter{section}{1}
\begin{document}
\title{Exercise Sheet 3}

\section*{Exercise Sheet 3}
\subsection*{Exercise 1}
Give the query graphs for the two queries from the first exercise sheet.
\begin{verbatim}
SELECT s1.name
FROM studenten s1
JOIN hoeren h1 ON s1.matrnr = h1.matrnr
JOIN hoeren h2 ON h1.vorlnr = h2.vorlnr
JOIN studenten s2 ON h2.matrnr = s2.matrnr
WHERE s2.name = 'Schopenhauer'
AND s1.name != 'Schopenhauer';
\end{verbatim}
\begin{tikzpicture}[node distance=5cm]
  \tikzset{every loop/.style={}}%removes arrow head from all loops.
  \node (s1) at (0,0) {studenten s1};
  \node (h1) [right of=s1] {hoeren h1};
  \node (h2) [right of=h1] {hoeren h2};
  \node (s2) [below=1cm of h2] {studenten s2};
  \draw (s1) -- (h1) node[pos=0.5,above] {\tiny s1.matrnr = h1.matrnr};
  \draw (h1) -- (h2) node[pos=0.5,above] {\tiny h1.vorlnr = h2.vorlnr};
  \draw (h2) -- (s2) node[pos=0.5,left] {\tiny h2.matrnr = s2.matrnr};
  \draw (s1) edge [loop above] node {\tiny s1.name != 'Schopenhauer'} (s1);
  \draw (s2) edge [loop below] node {\tiny s2.name == 'Schopenhauer'} (s2);
\end{tikzpicture}

\begin{verbatim}
SELECT p.persnr, p.name
FROM professoren p
JOIN vorlesungen v ON v.gelesenvon = p.persnr
JOIN hoeren h1 ON h1.vorlnr = v.vorlnr
JOIN hoeren h2 ON h2.vorlnr = h1.vorlnr
WHERE h1.matrnr != h2.matrnr;
\end{verbatim}
\begin{tikzpicture}[node distance=5cm]
  \node (p) at (0,0) {professoren p};
  \node (v) [right of=p] {vorlesungen v};
  \node (h1) [right of=v] {hoeren h1};
  \node (h2) [below=1cm of h1] {hoeren h2};
  \draw (p) -- (v) node[pos=0.5,above] {\tiny p.persnr = v.gelesenvon};
  \draw (v) -- (h1) node[pos=0.5,above] {\tiny v.vorlnr = h1.vorlnr};
  \draw (h1) -- (h2) node[pos=0.5,left] {\tiny h1.vorlnr = h2.vorlnr $\land$ h1.matrnr != h2.matrnr};
\end{tikzpicture}

\newpage
\subsection*{Exercise 2}
$|R_1| = 1, |R_2| = 40, |R_3| = 40, |R_4| = 1, f_{1,2} = 0.75, f_{2,3} = 0.01, f_{3,4} = 0.75$\\
\vspace{.2cm}\\
\begin{tikzpicture}[node distance=2cm]
  \node (R1) at (0,0) {$R_1$};
  \node (R2) [right of=R1] {$R_2$};
  \node (R3) [right of=R2] {$R_3$};
  \node (R4) [right of=R3] {$R_4$};
  \draw (R1) -- (R2) node[pos=0.5,below] {\tiny $f_{1,2} = 0.75$};
  \draw (R2) -- (R3) node[pos=0.5,below] {\tiny $f_{2,3} = 0.01$};
  \draw (R3) -- (R4) node[pos=0.5,below] {\tiny $f_{3,4} = 0.75$};
  \node (CR1) [below=0.01cm of R1] {\tiny{1}};
  \node (CR2) [below=0.01cm of R2] {\tiny{40}};
  \node (CR3) [below=0.01cm of R3] {\tiny{40}};
  \node (CR4) [below=0.01cm of R4] {\tiny{1}};
\end{tikzpicture}\\
\vspace{.2cm}\\
\begin{tabular}{l|r|r}
    $X$ & $C_{\text{out}}$ & $|X|$\\
    \hline
    $R_1 \bowtie R_2$ & 30 & 30\\
    $R_2 \bowtie R_3$ & 16 & 16\\
    $R_3 \bowtie R_4$ & 30 & 30\\
    &&\\
    $R_1 X R_3$ & 40 & 40\\
    $R_1 X R_4$ & 1 & 1\\
    \hline
    $(R_1 \bowtie R_2)\bowtie R_3$ & 42 & 12\\
    $(R_2\bowtie R_3) \bowtie R_1$ & 28 & 12\\
    &&\\
    $(R_1 X R_3)\bowtie R_2$ & 52 & 12\\
    $(R_1 X R_3)\bowtie R_4$ & 70 & 30\\
    $(R_1 X R_4)\bowtie R_2$ & 31 & 30\\
    \hline
    $(R_1 \bowtie R_2)\bowtie (R_3 \bowtie R_4)$ & 69 & 9\\
    $(R_1 X R_3)\bowtie (R_2 X R_4)$ & 90 & 9\\
    \textcolor{red}{$(R_1 X R_4)\bowtie (R_2 \bowtie R_3)$} & \textcolor{red}{26} & \textcolor{red}{9}\\
    &&\\
    $((R_1 \bowtie R_2)\bowtie R_3) \bowtie R_4$ & 51 & 9\\
    $((R_2 \bowtie R_3)\bowtie R_1) \bowtie R_4$ & 37 & 9\\
    $((R_1 X R_3)\bowtie R_2) \bowtie R_4$ & 61 & 9\\
    $((R_1 X R_3)\bowtie R_4) \bowtie R_2$ & 79 & 9\\
    $((R_1 X R_4)\bowtie R_2) \bowtie R_3$ & 40 & 9\\
\end{tabular}

\end{document}


\begin{tabular}{lll}
  \centering
  Author &  O. Bonnefon &2010\\
  Revision& section \ref{Sec:NE_motion} to \ref{Sec:NE_TD} V. Acary&  05/09/2011\\
  Revision& section \ref{Sec:NE_motion}  V. Acary&  01/06/2016\\
  Revision& complete edition V. Acary&  06/01/2017\\

\end{tabular}

\def\glaw{\cdot}
\def\cg{\sf \small g}

\section{The equations of motion}

In the maximal coordinates framework, the most natural choice for the kinematic  variables and for the formulation of the equations of motion is the Newton/Euler formalism, where the equation of motion describes the translational and rotational dynamics of each body using a specific choice of parameters. For the translational motion, the position of the center of mass $x_{\cg}\in \RR^3$ and its velocity  $v_{\cg} = \dot x_{\cg} \in \RR^3$ is usually chosen. For the orientation of the body is usually defined by the rotation matrix $R$ of the body-fixed frame with respect to a given inertial frame.

For the rotational motion, a common choice is to choose the rotational velocity  $\Omega \in \RR^3$ of the body expressed in the body--fixed frame. This choice comes from the formulation of a rigid body motion of a point $X$ in the inertial frame as
\begin{equation}
  \label{eq:1}
  x(t) = \Phi(t,X) = x_{\cg}(t) + R(t) X.
\end{equation}
The velocity of this point can be written as
\begin{equation}
  \label{eq:2}
  \dot x(t) = v_{\cg}(t) + \dot R(t) X
\end{equation}
Since $R^\top R=I$, we get $R^\top \dot R + \dot R^\top R =0$. We can conclude that it exists a matrix $\tilde \Omega := R^\top \dot R $ such that $\tilde \Omega + \tilde \Omega^\top=0$, i.e. a skew symmetric matrix. The notation $\tilde \Omega$ comes from the fact that there is a bijection between the skew symmetric matrix in $\RR^{3\times3}$ and $\RR^3$ such that
\begin{equation}
  \label{eq:3}
  \tilde \Omega x  = \Omega \times x, \quad \forall x\in \RR^3.
\end{equation}
The rotational velocity is then related to the $R$ by :
\begin{equation}
  \label{eq:angularvelocity}
  \widetilde \Omega = R^\top \dot R, \text { or equivalently, } \dot R  = R \widetilde \Omega
\end{equation}

Using these coordinates, the equations of motion are given by
\begin{equation}
  \label{eq:motion-NewtonEuler}
  \left\{\begin{array}{rcl}
      m \;\dot v_{\cg}  & = &f(t,x_{\cg}, v_{\cg},  R,  \Omega) \\
      I \dot \Omega + \Omega \times I \Omega &= & M(t,x_{\cg}, v_{\cg}, R, \Omega) \\
      \dot x_{\cg}&=& v_{\cg}\\
      \dot R  &=& R \widetilde \Omega
    \end{array}
  \right.
\end{equation}
where $m> 0$ is the mass, $I\in \RR^{3\times 3}$ is the matrix of moments of inertia around the center of mass and the axis of the body--fixed frame.

The vectors $f(\cdot)\in \RR^3$ and $M(\cdot)\in \RR^3$ are the total forces and torques applied to the body. It is important to outline that the total applied forces $f(\cdot)$ has to be expressed in a consistent frame w.r.t. to $v_{\cg}$. In our case, it hae to be expressed in the inertial frame. The same applies for the moment $M$ that has to be expressed in the body-fixed frame. If we consider a moment $m(\cdot)$ expressed in the inertial frame, then is has to be convected to  the body--fixed frame thanks to
\begin{equation}
  \label{eq:convected_moment}
  M (\cdot) =R^\top  m (\cdot)
\end{equation}

\begin{remark}
If we perform the time derivation of $RR^\top =I$ rather than $R^\top R=I$, we get $R \dot R^\top + \dot R R^\top =0$.  We can conclude that it exists a matrix $\tilde \omega := \dot R R^\top $ such that $\tilde \omega + \tilde \omega^\top=0$, i.e. a skew symmetric matrix. Clearly, we have
 \begin{equation}
   \label{eq:4}
   \tilde \omega = R \tilde \Omega R^\top
 \end{equation}
 and it can be proved that is equivalent to $ \omega =R \Omega$. The vector $\omega$ is the rotational velocity expressed in the inertial frame. The equation of motion can also be expressed in the inertial frame as follows
  \begin{equation}
  \label{eq:motion-NewtonEuler-inertial}
  \left\{\begin{array}{rcl}
      m \;\dot v_{\cg}  & = &f(t,x_{\cg}, v_{\cg},  R,  R^T \omega) \\
      J(R) \dot \omega + \omega \times J(R) \omega &= & m(t,x_{\cg}, v_{\cg}, R, \omega) \\
      \dot x_{\cg}&=& v_{\cg}\\
      \dot R  &=& \widetilde \omega R
    \end{array}
  \right.
\end{equation}
where the matrix $J(R) = R I R^T$ is the inertia matrix in the inertial frame.
Defining the angular momentum with respect to the inertial frame as
\begin{equation}
  \label{eq:1}
  \pi(t) = J(R(t)) \omega(t)
\end{equation}
the equation of the angular motion is derived from the balance equation of the angular momentum
\begin{equation}
  \label{eq:5}
  \dot \pi(t) = m(t,x_{\cg}, v_{\cg}, R, \omega)).
\end{equation}

\end{remark}

For a given constant (time invariant) $\tilde \Omega$, let us consider the differential equation
\begin{equation}
  \label{eq:5}
  \begin{cases}
    \dot R(t) = R \tilde \Omega\\
    R(0) = I
  \end{cases}
\end{equation}
Let us recall the definition of the matrix exponential,
\begin{equation}
  \label{eq:6}
  \exp(A) = \sum_{k=0}^{\infty} \frac {1}{k!} A^k
\end{equation}
A trivial solution of \eqref{eq:5} is $R(t) = \exp(t\tilde\Omega) $ since
\begin{equation}
  \label{eq:7}
  \frac {d}{dt}(\exp(At)) = \exp(At) A.
\end{equation}
More generally, with the initial condition $R(t_0)= R_0$, we get the solution
\begin{equation}
R(t) = R_0 \exp((t-t_0)\tilde\Omega)\label{eq:8}
\end{equation}

Another interpretation is as follows. From a (incremental) rotation vector, $\Omega$ and its associated matrix $\tilde \Omega$, we obtain a rotation matrix by the exponentation of $\tilde \Omega$:
\begin{equation}
  \label{eq:9}
  R = \exp(\tilde\Omega).
\end{equation}
Since we note that $\tilde \Omega^ 3 = - \theta^2 \tilde \Omega$ with $\theta = \|\Omega\|$, it is possible to get a closed form of the matrix exponential of $\tilde \Omega$
\begin{equation}
  \label{eq:10}
  \begin{array}[lcl]{lcl}
    \exp(\tilde \Omega) &=& \sum_{k=0}^{\infty} \frac {1}{k!} (\tilde \Omega)^k \\
                        &=&  I_{3\times 3} + \sum_{k=1}^{\infty} \frac {(-1)^{k-1}}{(2k-1)!}  \theta ^{2k-1} \tilde \Omega + (\sum_{k=0}^{\infty} \frac {(-1)^{k-1}}{(2k)!} \theta)^{2k-2} \tilde \Omega^2\\[2mm]
                        &=&  I_{3\times 3} + \frac{\sin{\theta}} {\theta} \tilde \Omega +  \frac{(\cos{\theta}-1)}{\theta^2}\tilde \Omega^2
  \end{array}
\end{equation}
that is
\begin{equation}
  \label{eq:11}
  R =  I_{3\times 3} + \frac{\sin{\theta}} {\theta} \tilde \Omega +  \frac{(\cos{\theta}-1)}{\theta^2}\tilde \Omega^2
\end{equation}
The formula \eqref{eq:11} is the Euler--Rodrigues formula that allows to compute the rotation matrix on closed form.

\begin{ndrva}
  todo :
  \begin{itemize}
  \item add the formulation in the inertial frame of the Euler
    equation with $\omega =R \Omega$.
  \item Check that \eqref{eq:10} is the Euler-Rodrigues formula and not the Olinde Rodrigues formula. (division by $\theta$)
\end{itemize}

\end{ndrva}

In the numerical practice, the choice of the rotation matrix is not convenient since it introduces redundant parameters. Since $R$ must belong to $SO^+(3)$, we have also to satisfy $\det(R)=1$ and $R^{-1}=R^\top$. In general, we use a reduced vector of parameters $p\in\RR^{n_p}$ such $R = \Phi(p)$ and $\dot p = \psi(p)\Omega $. We denote  by $q$ the vector of coordinates of the position and the orientation of the body, and by $v$ {the body twist}:
\begin{equation}
  q \coloneqq \begin{bmatrix}
    x_{\cg}\\
    p
  \end{bmatrix},\quad
  v \coloneqq \begin{bmatrix}
     v_{\cg}\\
     \Omega
   \end{bmatrix}.
 \end{equation}
 The relation between $v$ and the time derivative of $q$ is
\begin{equation}
  \label{eq:TT}
  \dot q =
  \begin{bmatrix}
     \dot x_{\cg}\\
     \psi(p) \dot p
   \end{bmatrix}
   =
   \begin{bmatrix}
     I & 0 \\
     0 & \psi(p)
   \end{bmatrix}
   v
   \coloneqq
   T(q) v
\end{equation}
with $T(q) \in \RR^{{3+n_p}\times 6}$.
{Note that the twist $v$ is not directly the time derivative of the coordinate vector as a major difference with Lagrangian systems. }

%
The Newton-Euler equation in compact form may be written as:
\begin{equation}
\label{eq:Newton-Euler-compact}
\boxed{ \left \{
 \begin{aligned}
  &\dot q=T(q)v, \\
  & M \dot v = F(t, q, v)
 \end{aligned}
 \right.}
\end{equation}
where $M\in\RR^{6\times6}$ is the total inertia matrix
\begin{equation}
  M:= \begin{pmatrix}
    m I_{3\times 3} & 0 \\
    0 & I
  \end{pmatrix},
\end{equation}
and $F(t, q, v)\in \RR^6$ collects all the forces and torques applied to the body
\begin{equation}
  F(t,q,v):= \begin{pmatrix}
    f(t,x_{\cg},  v_{\cg}, R, \Omega ) \\
    I \Omega \times \Omega + M(t,x_{\cg}, v_{\cg}, R, \Omega )
  \end{pmatrix}.
\end{equation}
When a collection of bodies is considered, we will use the same notation as in~(\ref{eq:Newton-Euler-compact}) extending the definition of the variables $q,v$ and the operators $M,F$ in a straightforward way.

\input{LieGroupTheory.tex}
\section{ Lie group $SO(3)$ of finite rotations and Lie algebra $\mathfrak{so}(3)$ of infinitesimal rotations}
The presentation is this section follows the notation and the developments taken from~\cite{Iserles.ea_AN2000,Munthe-Kaas.BIT1998}. For more details on Lie groups and Lie algebra, we refer to \cite{Varadarajan_book1984} and \cite{Helgason_Book1978}.

The Lie group $SO(3)$ is the group of linear proper orthogonal transformations in $\RR^3$ that may be represented by a set of matrices in $\RR^{3\times 3}$ as
\begin{equation}
  \label{eq:47}
  SO(3) = \{R \in \RR^{3\times3}\mid R^TR=I , det(R) = +1  \}
\end{equation}
with the group law given by $R_1\glaw R_2 = R_1R_2$ for $R_1,R_2\in SO(3)$. The identity element is $e = I_{3\times 3}$. At any point of $R\in SO(3)$, the tangent space $T_RSO(3)$ is the set of tangent vectors at a point $R$.

\paragraph{Left representation of  the tangent space at $R$, $T_RSO(3)$ } Let $S(t)$ be a smooth curve $S(\cdot) : \RR  \rightarrow SO(3)$ in $SO(3)$. An element $a$ of the tangent space at $R$ is given by
\begin{equation}
  \label{eq:174}
  a  = \left.\frac{d}{dt} S(t)\right|_{t=0}
\end{equation}
such that $S(0)= R$.
Since $S(t)\in SO(3)$, we have  $\frac{d}{dt} (S(t)) = \dot S(t)S^T(t) +  S(t) \dot S^T(t) =0$. At $t=0$, we get $a R^T +  R a^T =0$.
We conclude that it exists a skew--symmetric matrix $\tilde \Omega = R^T a$ such that $\tilde \Omega^T + \tilde \Omega =0$. Hence, a possible representation of  $T_RSO(3)$ is
\begin{equation}
  \label{eq:49}
  T_RSO(3) = \{ a = R \tilde \Omega \in \RR^{3\times 3} \mid \tilde \Omega^T + \tilde \Omega =0 \}.
\end{equation}
For $R=I$, the tangent space is directly given by the set of  skew--symmetric matrices:
\begin{equation}
  \label{eq:50}
  T_ISO(3) = \{ \tilde \Omega\in \RR^{3\times 3} \mid \tilde \Omega^T + \tilde \Omega =0 \}.
\end{equation}
The tangent space $T_ISO(3)$ with the Lie Bracket $[\cdot,\cdot]$ defined by the matrix commutator
\begin{equation}
  \label{eq:51}
  [A,B] = AB-BA
\end{equation}
is a Lie algebra that is denoted by
\begin{equation}
  \label{eq:53}
  \mathfrak{so}(3) =\{\Omega\in \RR^{3\times 3} \mid \Omega + \tilde \Omega^T =0\}.
\end{equation}
 For skew symmetric matrices, the commutator can be expressed with the cross product in $\RR^3$
\begin{equation}
  \label{eq:52}
  [\tilde \Omega, \tilde \Gamma] = \tilde \Omega \tilde \Gamma - \tilde \Gamma \tilde \Omega= \widetilde{\Omega \times \Gamma }
\end{equation}
We use   $T_ISO(3) \cong  \mathfrak{so}(3)$ whenever there is no ambiguity.

The notation $\tilde \Omega$ is implied by the fact that the Lie algebra is isomorphic to $\RR^3$ thanks to the operator $\widetilde{(\cdot)} :\RR^3 \rightarrow \mathfrak{so}(3)$ and defined by
\begin{equation}
  \label{eq:54}
 \widetilde{(\cdot)}: \Omega \mapsto \tilde \Omega =
  \begin{bmatrix}
    0 & -\Omega_3 & \Omega_2 \\
    \Omega_3 & 0 & -\Omega_1 \\
    -\Omega_2  & \Omega_1 & 0
  \end{bmatrix}
\end{equation}
Note that $\tilde \Omega x = \Omega \times x$.

\paragraph{ A special  (right)  action of Lie Group $\mathcal G$ on a manifold $\mathcal M$. }

Let us come back to the representation of  $T_RSO(3)$ given in~\eqref{eq:49}. It is clear it can expressed with a representation that relies on $\mathfrak{so}(3)$
\begin{equation}
  \label{eq:58}
   T_RSO(3) = \{ a = R \tilde \Omega \in \RR^{3\times 3} \mid \tilde \Omega \in \mathfrak{so}(3) \}.
\end{equation}
With \eqref{eq:58}, we see that there is a linear map that relates $T_RSO(3)$ to  $\mathfrak{so}(3)$. This can be formalize by noting that the left translation map for a point $R \in SO(3)$
\begin{equation}
  \label{eq:59}
  \begin{array}[lcl]{rcl}
    L_R& :&   SO(3)  \rightarrow  SO(3)\\
       & &  S  \mapsto L_R(S) = R \glaw S = RS\\
  \end{array}
\end{equation}
which is diffeomorphism on $SO(3)$ is a group action. In our case, we identify the manifold and the group. Hence, the mapping $L_R$ can be viewed as a left or a right group action. We choose a right action such that $\Lambda^r(R,S) = L_{R}(S) =  R \glaw S $. By differentiation, we get a mapping $L'_R: T_I\mathfrak{so(3)} \cong \mathfrak{so(3)} \rightarrow T_R SO(3)$. For a given $\tilde\Omega \in \mathfrak{so(3)}$ and a point $R$, the differential $L'_R$ by computing the tangent vector field $\lambda^r_{*}(a)(R)$ of the group action  $\Lambda^r(R,S)$ for a smooth curve $S(t) : \RR \rightarrow S0(3)$ such that $\dot S(0) = \tilde\Omega$:
\begin{equation}
  \label{eq:60}
   \lambda^r_{*}(a)(R) \coloneqq  \left. \frac{d}{dt} \Lambda^r(R,S(t)) \right|_{t=0} = \left. \frac{d}{dt} L_{R}(S(t)) \right|_{t=0} =  \left. \frac{d}{dt} R \glaw S(t) \right|_{t=0} =  R \glaw \dot S(0) = R \tilde\Omega \in X(\mathcal M)
 \end{equation}
%
Therefore, the vector field in \eqref{eq:60} is a tangent vector field that defines a Lie-Type ordinary differential equation
\begin{equation}
  \label{eq:61}
  \dot R(t) = \lambda^r_{*}(a)(R(t)) = R(t)  \tilde \Omega
\end{equation}

In~\cite{Bruls.Cardona2010}, the linear operator $\lambda^r_{*}(a)$  is defined as  the directional derivative with respect to $S$ an denoted $DL_R(S)$. It defines a diffeomorphism between $T_SSO(3)$ and $T_{RS}SO(3)$. In particular, for $S=I_{3\times3}$, we get
\begin{equation}
  \label{eq:62}
  \begin{array}{rcl}
    DL_R(I_{3\times3}) : \mathfrak{so}(3) & \rightarrow & T_R SO(3) \\
    \tilde \Omega &\mapsto &DL_R(I_{3\times3}). \tilde \Omega = R \tilde \Omega
  \end{array}
\end{equation}
We end up with a possible representation of $T_{R} SO(3)$ as
\begin{equation}
  \label{eq:63}
  T_{R} SO(3) =\{\tilde \Omega_R \mid \tilde \Omega_R = DL_R(I_{3\times3}). \tilde \Omega = R \tilde \Omega, \tilde \Omega \in\mathfrak{so}(3)  \}.
\end{equation}
In other words, a tangent vector $\tilde \Omega \in \mathfrak{so}(3)$ defines a left invariant vector field on $SO(3)$ at the point $R$ given by $R \tilde \Omega$.

\begin{ndrva}
  what happens at $S(0)=R$, with $ a =R \tilde \Omega =\dot S(0)$ and then $\dot y(t) = F(y(t)) = R \tilde \Omega y(t) =  R\Omega \times y(t)= \dot S(0) y(t) $. What else ?
\end{ndrva}

\paragraph{Exponential map $\expm \mathfrak{so(3)} \rightarrow SO(3)$}
The relations \eqref{eq:24} and \eqref{eq:25} shows that is possible to define tangent vector field from a group action. We can directly apply Theorem~\ref{Theorem:solutionofLieODE} and we get that the solution of
\begin{equation}
  \label{eq:130}
  \begin{cases}
  \dot R(t) = \lambda^r_{*}(a)(R(t)) = R(t)  \tilde \Omega \\
  R(0) = R_0
\end{cases}
\end{equation}
 is
\begin{equation}
  \label{eq:138}
  R(t) = R_0 \expm(t \tilde \Omega)
\end{equation}

Let us do the  computation in this case. Let us assume that the solution can be sought as $R(t) = \Lambda^r(y_o,S(t))$. The initial condition imposes that  $R(0) = R_0 = \Lambda(R_0,I) = \Lambda(R_0,S(0))$ that implies $S(0)=I$. Since $\Lambda(R_0,S(t))$ is the flow that is produces by $S(t)$ and let us try to find the relation satisfied by $S(\cdot)$. For a smooth curve $T(s) \in SO(3)$ such that $\dot T(0)= \tilde \Omega$, we have
\begin{equation}
  \label{eq:64}
  \begin{array}[lcl]{lcl}
    \dot R(t) = \lambda^r_*(\tilde \Omega)(R(t)) &=& \left. \frac{d}{ds}\Lambda^r(R(t),T(s)) \right|_{s=0} \\
                                &=& \left. \frac{d}{ds} \Lambda^r(\Lambda(R_0, S(t)),T(s)) \right|_{s=0} \\
                                &=& \left. \frac{d}{ds} (\Lambda^r(R_0, S(t)\glaw T(s)) \right|_{s=0} \\
                                &=& D_2 \Lambda^r(R_0, \glaw S(t) \glaw \dot T(0) ) \\
                                &=& D_2 \Lambda^r(R_0,  S(t)\glaw \tilde \Omega )
  \end{array}
\end{equation}
On the other side, the relation $y(t) = \Lambda^r(y_0,S(t))$ gives $\dot y(t) = D_2 \Lambda^r(y_0,S'(t))$ and we conclude that
\begin{equation}
  \label{eq:65}
  \begin{cases}
    \dot S(t) =  S(t)\glaw\tilde\Omega    = S(t) \tilde \Omega\\
    S(0) = I.
  \end{cases}
\end{equation}
The ordinary differential equation~\eqref{eq:65} is a matrix ODE that admits the following solution
\begin{equation}
  \label{eq:66}
  S(t) = \expm(t\tilde\Omega)
\end{equation}
where $\exp : \RR^{3\times 3} \rightarrow \RR^{3\times 3}$ is the matrix exponential defined by
\begin{equation}
  \label{eq:67}
  \begin{array}[lcl]{lcl}
    \expm(A) &=& \sum_{k=0}^{\infty} \frac {1}{k!} (A)^k.
  \end{array}
\end{equation}
We conclude that $R(t) =\Lambda(R_0,S(t)) = R_0\expm(t\tilde\Omega)$ is the solution of \eqref{eq:35}.

We can use the closed form solution for the matrix exponential of $t \tilde\Omega  \in \mathfrak{so}(3)$ as
\begin{equation}
  \label{eq:68}
  \expm(t\tilde\Omega) = I_{3\times 3} + \frac{\sin{t\theta}} {\theta}  \tilde\Omega  +  \frac{(\cos{t \theta}-1)}{\theta^2} \tilde\Omega^2
\end{equation}
with $\theta = \|\Omega\|$.
For given  $\tilde \Omega \in\mathfrak{so}(3)$, we have
\begin{equation}
  \label{eq:69}
  \det(\tilde \Omega) = \det(\tilde \Omega^T) = \det (-\tilde \Omega^T) = (-1)^3 \det(\tilde \Omega ) = - \det (\tilde \Omega )
\end{equation}
that implies that $\det(\tilde \Omega ) =0 $. From \eqref{eq:68}, we conclude that
\begin{equation}
  \label{eq:70}
  \det( \expm(t\tilde \Omega)) = 1.
\end{equation}
Furthermore, we have $\expm(t\tilde \Omega)\expm( -t\tilde \Omega) = \expm(t(\tilde \Omega-\tilde \Omega)) = I$. We can verify  that  $\expm(t\tilde \Omega) \in SO(3)$.

\paragraph{Adjoint representation}
In the case of $SO(3)$, the definition of the operator $\Ad$ gives
\begin{equation}
  \label{eq:121}
  \Ad_R(\tilde\Omega)  = R \tilde\Omega R^T
\end{equation}
 and then mapping $\ad_{\tilde\Omega}(\tilde \Gamma)$ is defined by
\begin{equation}
  \label{eq:56}
  \ad_{\tilde\Omega}(\tilde\Gamma) = \tilde \Omega \tilde\Gamma - \tilde \Gamma \tilde\Omega  =  [\tilde \Omega,\tilde \Gamma] = \widetilde{\Omega \times \Gamma}.
\end{equation}
Using the isomorphism between $\mathfrak so(3)$ and $\RR^3$, it possible the define the mapping $\ad_{\Omega}(\Gamma) : \RR^3\times\RR^3 \rightarrow \RR^3$ with the realization of the Lie algebra in $\RR^3$ as
\begin{equation}
  \label{eq:55}
  \ad_\Omega(\Gamma) = \tilde\Omega \Gamma = \Omega\times\Gamma
\end{equation}

\paragraph{Differential of the exponential map $\dexpm$}
The differential of the exponential mapping, denoted by $\dexpm$ is defined as the 'right trivialized' tangent of the exponential map
\begin{equation}
  \label{eq:71}
  \frac{d}{dt} (\exp(\tilde \Omega(t))) = \dexp_{\tilde\Omega(t)}(\frac{d \tilde{\Omega}(t)}{dt}) \exp(\tilde\Omega(t))
\end{equation}

% \begin{ndrva}
%   explain briefly the notion of left-invariant vector field
% \end{ndrva}
%\href{https://en.wikipedia.org/wiki/Lie_group}{https://en.wikipedia.org/wiki/Lie_group}
%Finally, the straight line $\alpha \tilde \Omega$ for $\Omega$

The differential of the exponential mapping, denoted by $\dexpm$ is defined as the 'left trivialized' tangent of the exponential map
\begin{equation}
  \label{eq:72}
   \frac{d}{dt} (\exp(\tilde \Omega(t))) = \dexp_{\tilde\Omega(t)}(\frac{d \tilde{\Omega}(t)}{dt}) \exp(\tilde\Omega(t))
\end{equation}

Using the formula~\eqref{eq:43} and the fact that $\ad_\Omega(\Gamma) = \Tilde\Omega \Gamma$, we can write the differential as
\begin{equation}
  \label{eq:122}
  \begin{array}{lcl}
    \dexp_{\tilde\Omega}(\tilde\Gamma) &=& \sum_{k=0}^\infty \frac{1}{(k+1)!} \ad_{\tilde \Omega}^k (\tilde\Gamma) \\
                                       &=& \sum_{k=0}^\infty \frac{1}{(k+1)!} \tilde\Omega^k \tilde \Gamma \\
  \end{array}
\end{equation}
Using again the fact that $\tilde\Omega^3 = -\theta^2 \tilde\Omega$, we get
\begin{equation}
  \label{eq:123}
   \begin{array}{lcl}
     \dexp_{\tilde\Omega} &=& \sum_{k=0}^\infty  \frac{1}{(k+1)!} \tilde\Omega^k \\
                          &=& I  + \sum_{k=0}^\infty  \frac{(-1)^k}{((2(k+1))!} \theta^{2k} \tilde\Omega + \sum_{k=0}^\infty  \frac{(-1)^k}{((2(k+1)+1)!} \theta^{2k} \tilde\Omega^2\\
  \end{array}
\end{equation}
Hence, we get
\begin{equation}
  \label{eq:124}
   \begin{array}{lcl}
     \dexp_{\tilde\Omega}  &=& I  + \frac{(1-\cos(\theta))}{\theta^2}\tilde\Omega + \frac{(\theta-\sin(\theta))}{\theta^3}\tilde\Omega^2
  \end{array}
\end{equation}
Since $\dexp_{\tilde\Omega}$ is a linear mapping from $\mathfrak{so(3)}$ to $\mathfrak{so(3)}$, we will use the following notation
\begin{equation}
  \label{eq:172}
  \dexp_{\tilde\Omega}\tilde\Gamma  \coloneqq T(\Omega)\tilde\Gamma
\end{equation}
with
\begin{equation}
  \label{eq:173}
   T(\Omega) \coloneqq I  + \frac{(1-\cos(\theta))}{\theta^2}\tilde\Omega + \frac{(\theta-\sin(\theta))}{\theta^3}\tilde\Omega^2  \in \RR^{3\times 3}
\end{equation}

\subsection{Newton method and differential of a map $f : \mathcal G \rightarrow \mathfrak g$}
Finally, let us define the differential of the map $f : SO(3) \rightarrow \mathfrak {so}(3)$ as
\begin{equation}
  \label{eq:183}
  \begin{array}[rcl]{rcl}
    f'_R : T_RSO(3) &\rightarrow&T_{f(R)}\mathfrak {so}(3) \cong  \mathfrak {so}(3)\\
           a &\mapsto& \left.\frac{d}{dt} f(R\glaw \expm(t L'_{R^{-1}}(a))) \right|_{t=0}
  \end{array}
\end{equation}
The image of $b$ by $f'_z$   is obtained by first identifying $a$ with an element of $\tilde\Omega \in \mathfrak {so}(3)$ thanks to the left representation of $T_{f(R)}\mathfrak {so}(3)$ view the left translation map $\tilde\Omega= t L'_R(b)$. The exponential mapping transforms $\tilde\Omega$ an element $S$ of the Lie Group $SO(3)$. Then $f'_z$ is obtained by
\begin{equation}
  \label{eq:184}
  f'_R(b) = \lim_{t\rightarrow 0} \frac{f(R\glaw S) - f(R)}{t}
\end{equation}
As we have done for the exponential mapping, it is possible to get a left trivialization of
\begin{equation}
  \label{eq:185}
  \dd f_R = (f\circ L_R)' = f'_R \circ L'_R
\end{equation}
thus
\begin{equation}
  \label{eq:186}
  \dd f_R (\tilde\Omega) =  f'_R \circ L'_R(\tilde\Omega) = f'_R(L'_R(\tilde\Omega)) =  \left.\frac{d}{dt} f(R\glaw \expm(t \tilde\Omega )) \right|_{t=0}
\end{equation}

\begin{ndrva}
  The computation of this differential is non linear with respect to $\tilde\Omega$.

  not clear if we write $\dd f_R (\tilde\Omega)$. Better understand the link with $\dexp_{\tilde \Omega}{\tilde\Gamma}$
\end{ndrva}
Sometimes, it can be formally written as
\begin{equation}
  \label{eq:180}
  \dd f_R (\tilde\Omega) = C(\tilde\Omega)\tilde\Omega
\end{equation}
Nevertheless, an explicit expression of $C(\cdot)$ is not necessarily trivial.

Let us consider a first simple example of a mapping $f(R) = \widetilde{R  x}$ for a given $x\in\RR^3$. The computation yields
\begin{equation}
  \label{eq:181}
  \begin{array}{rcl}
    \dd f_R (\tilde\Omega) &=& \widetilde{ \left.\frac{d}{dt} R \exp(t \tilde\Omega) x  \right|_{t=0}} \\
                           &=& \widetilde{R \left.\frac{d}{dt}\exp(t \tilde\Omega)\right|_{t=0}  x} \\
                           &=& \widetilde{R \left. \dexp_{\tilde\Omega}(\tilde\Omega)\exp(t \tilde\Omega) \right|_{t=0}  x} \\
                           &=& \widetilde{R \dexp_{\tilde\Omega}(\tilde\Omega) x} \\
                           &=& \widetilde{R T(\Omega) \tilde\Omega  x} \\
                           &=& \widetilde{-R T(\Omega) \tilde x \Omega }
  \end{array}
\end{equation}
In that case, it is difficult to find a expression as in \eqref{eq:180}, but considering the function $g(R)$ such that $f(R) = \widetilde g(x)$ we get
\begin{equation}
  \label{eq:181}
  \begin{array}{rcl}
    \dd g_R (\tilde\Omega)  =- R T(\Omega) \tilde x \Omega  = C(\Omega) \Omega
  \end{array}
\end{equation}
with
\begin{equation}
  \label{eq:182}
   C(\Omega) = -R T(\Omega) \tilde x
\end{equation}

\section{Lie group of unit quaternions $\HH_1$ and pure imaginary quaternions $\HH_p$.}

In Siconos we choose to parametrize the rotation with a unit quaternion $p \in \HH$ such that $R = \Phi(p)$. This parameterization has no singularity and has only one redundant variable that is determined by imposing $\|p\|=1$.

\paragraph{Quaternion definition.} There is many ways to define quaternions. The most convenient one is to define a quaternion as
a $2\times 2$ complex matrix, that is an element of $\CC^{2\times 2}$. For this end, we write for $z \in \CC$, $z=a+ib$ with $a,b \in \RR^2$ and $i^2=-1$ and its conjugate $\bar z= a-ib$. Let ${e, \bf, i, j, k}$ the following matrices in $\CC^{2\times 2}$
\begin{equation}
  \label{eq:127}
  e =
  \begin{bmatrix}
    1 & 0 \\
    0 & 1  \\
  \end{bmatrix},
  \quad   \bf{i} =
  \begin{bmatrix}
    i & 0 \\
    0 & -i  \\
  \end{bmatrix}
  \quad   \bf{j} =
  \begin{bmatrix}
    0 & 1 \\
    -1 & 0  \\
  \end{bmatrix}
   \quad   \bf{k} =
  \begin{bmatrix}
    0 & i \\
    i & 0  \\
  \end{bmatrix}
\end{equation}

\begin{definition}
  Let $\HH$ be the set of all matrices of the form
  \begin{equation}
    \label{eq:128}
    p_0 e + p_1 {\bf i} + p_2 {\bf j} + p_3 {\bf k}
  \end{equation}
  where $(p_0,p_1,p_2,p_3) \in \RR^4$. Every Matrix in $\HH$ is of the form
  \begin{equation}
    \label{eq:129}
    \begin{bmatrix}
      x &y  \\
      - \bar y  & \bar x
    \end{bmatrix}
  \end{equation}
where $x = p_0 + i p_1$ and $y = p_2 + i p_3$. The matrices in $\HH$ are called quaternions.
\end{definition}

\begin{definition}
  The null quaternion generated by $[0,0,0,0] \in \RR^4$ is denoted by $0$ . Quaternions of the form $p_1 \bf {i} + p_2 \bf{j} + p_3 \bf{k}$ are called pure quaternions. The set of pure quaternions is denoted by $\HH_p$.
\end{definition}

With the definition of $\HH$ as a set of complex matrices, It can be show that $\HH$ is a real vector space of dimension $4$ with basis ${e, \bf, i, j, k}$. Furthermore, with the matrix product, $\HH$ is a real algebra.

\paragraph{Representation of quaternions} Thanks to the equations~\eqref{eq:128}, ~\eqref{eq:128} and ~\eqref{eq:129}, we see that there are several manner to represent a quaternion $p\in \HH$. It can be represented as a complex matrix as in~\eqref{eq:129}. It can also be represented as a vector in $\RR^4$ , $p= [p_0,p_1,p_2,p_3]$ with the isomorphism~\eqref{eq:128}. In other words,  $\HH$ is isomorphic to $\RR^4$.  The first element $p_0$ can also be viewed as  a scalar and three last ones as a vector in $\RR^3$ denoted by $\vv{p} = [p_1,p_2,p_3]$, and in that case, $\HH$ is viewed as $\RR\times \RR^3$. The quaternion can be written as $p=(p_0,\vv{p})$.

\paragraph{Quaternion product} The quaternion product denoted by $p \glaw q $ for $p,q\in \HH_1$ is naturally defined as the product of complex matrices. With its representation in $\RR\times \RR^3$, the quaternion product is defined by
\begin{equation}
  \label{eq:73}
  p \glaw q =
  \begin{bmatrix}
    p_oq_o - \vv{p}\vv{q} \\
    p_0\vv{q}+q_o\vv{p} + \vv{p}\times\vv{q}
  \end{bmatrix}.
\end{equation}
Since the product is a matrix product, it is not communicative, but it is associative.  The identity element for the quaternion product is
\begin{equation}
e=  \begin{bmatrix}
    1 & 0 \\
    0 & 1  \\
  \end{bmatrix} =(1,\vv{0})\label{eq:57}.
\end{equation}Let us note that
\begin{equation}
  \label{eq:74}
  (0,\vv{p})\glaw (0,\vv(q)) = - (0,\vv{q})\glaw (0,\vv{p}).
\end{equation}
The quaternion multiplication can also be represented as a matrix operation in $\RR^{4\times4}$. Indeed, we have
\begin{equation}
  \label{eq:75}
  p \glaw q  =
  \begin{bmatrix}
    q_0 p_0 -q_1p_1-q_2p_2-q_3p_3\\
    q_0 p_1 +q_1p_0-q_2p_3+q_3p_2\\
    q_0 p_2 +q_1p_3+q_2p_0-q_3p_1\\
    q_0 p_3 -q_1p_2+q_2p_1+q_3p_0\\
  \end{bmatrix}
\end{equation}
that can be represented as
\begin{equation}
  \label{eq:76}
  p \glaw q  =
  \begin{bmatrix}
    p_0 & -p_1 & -p_2 & -p_3 \\
    p_1 & p_0 & -p_3 & p_2 \\
    p_2 & p_3 & p_0 & -p_1 \\
    p_3 & -p_2 & p_1 & p_0 \\
  \end{bmatrix}
  \begin{bmatrix}
    q_0\\
    q_1\\
    q_2\\
    q_3
  \end{bmatrix} := [p_\glaw]q
\end{equation}
or
\begin{equation}
  \label{eq:77}
  p \glaw q  =
  \begin{bmatrix}
    q_0 & -q_1 & -q_2 & -q_3 \\
    q_1 & q_0 & q_3 & -q_2 \\
    q_2 & -q_3 & q_0 & q_1 \\
    q_3 & q_2 & -q_1 & q_0 \\
  \end{bmatrix}
  \begin{bmatrix}
    p_0\\
    p_1\\
    p_2\\
    p_3
  \end{bmatrix} := [{}_\glaw q] p
\end{equation}

\paragraph{Adjoint quaternion, inverse and norm}
The adjoint quaternion of $p$ is denoted by
\begin{equation}
  p^\star = \overline{  \begin{bmatrix}
      x &y  \\
      - \bar y  & \bar x
    \end{bmatrix}}^T
  =\begin{bmatrix}
    \bar x & - y  \\
    \bar y  &  x
  \end{bmatrix} =
  \begin{bmatrix}
    p_0, -p_1, -p_2, -p_3
  \end{bmatrix} = (p_0, - \vv{p})
\end{equation}
We note that
\begin{equation}
  \label{eq:131}
  p \glaw p^\star =
  \begin{bmatrix}
      x &y  \\
      - \bar y  & \bar x
    \end{bmatrix}
    \begin{bmatrix}
    \bar x & - y  \\
    \bar y  &  x
  \end{bmatrix} = \det(\begin{bmatrix}
      x &y  \\
      - \bar y  & \bar x
    \end{bmatrix}) e = (x\bar x + y \bar y) e  = (p^2_0 + p^2_1+ p_2^2 + p_3^2)e
\end{equation}

The norm of a quaternion is given by $|p|^2=p^\top p = p_o^2+p_1^2+p_2^2+p_3^2$. In particular, we have $p \glaw p^\star = p^\star \glaw p = |p|^2 e$. This allows to define the reciprocal of a non zero quaternion by
\begin{equation}
  \label{eq:78}
  p ^{-1} = \frac 1 {|p|^2} p^\star
\end{equation}
A quaternion $p$ is said to be unit if $|p| =1$.

\paragraph{Unit quaternion and rotation}
For two vectors $x\in \RR^3$ and $x'\in \RR^3$, we define the quaternion $p_x = (0,x)\in \HH_p$ and  $p_{x'} = (0,x')\in \HH_p$.
For a given unit quaternion $p$, the transformation
\begin{equation}
  \label{eq:79}
  p_{x'} = p \glaw p_x \glaw  p^\star
\end{equation}
defines a rotation $R$ such that $x'  = R x$ given by
\begin{equation}
  \label{eq:80}
  x' = (p_0^2- p^\top \vv{p}) x +2 p_0(\vv{p}\times x) +  2 (\vv{p}^\top x) p = R x
\end{equation}
The rotation matrix may be computed as
\begin{equation}
  \label{eq:81}
  R = \Phi(p) =
  \begin{bmatrix}
    1-2 p_2^2- 2 p_3^2 & 2(p_1p_2-p_3p_0) & 2(p_1p_3+p_2p_0)\\
    2(p_1p_2+p_3p_0) & 1-2 p_1^2- 2 p_3^2 & 2(p_2p_3-p_1p_0)\\
    2(p_1p_3-p_2p_0) & 2(p_2p_3+p_1p_0)  & 1-2 p_1^2- 2 p_2^2\\
  \end{bmatrix}
\end{equation}

\paragraph{Computation of the time derivative of a unit  quaternion associated with a rotation.}
The derivation with respect to time can obtained as follows. The rotation transformation for a unit quaternion is given by
\begin{equation}
  \label{eq:82}
  p_{x'}(t) = p(t) \glaw p_x \glaw p^\star(t) =  p(t) \glaw p_x \glaw p^{-1}(t)
\end{equation}
and can be derived as
\begin{equation}
  \label{eq:83}
  \begin{array}{lcl}
    \dot p_{x'}(t) &=& \dot p(t) \glaw p_x \glaw p^{-1}(t) + p(t) \glaw p_x \glaw \dot p^{-1}(t) \\
                  &=& \dot p(t) \glaw p^{-1}(t)  \glaw   p_{x'}(t)  +      p_{x'}(t) \glaw p(t)  \glaw \dot p^{-1}(t)
  \end{array}
\end{equation}
From $p(t) \glaw p^{-1}(t) =e$, we get
\begin{equation}
  \label{eq:84}
  \dot p(t) \glaw p^{-1}(t) + p \glaw \dot p^{-1}(t) = 0
\end{equation}
so (\ref{eq:82}) can be rewritten
\begin{equation}
  \label{eq:85}
  \begin{array}{lcl}
    \dot p_{x'}(t) = \dot p(t) \glaw p^{-1}(t)   \glaw   p_{x'}(t)  -    p_{x'}(t) \glaw  \dot p(t) \glaw p^{-1}(t)
  \end{array}
\end{equation}
The scalar part of $\dot p(t) \glaw p^{-1}(t)$ is $(\dot p(t) \glaw p^{-1}(t))_0 = p_o \dot p_0 + \vv{p}^T\vv{\dot p}$. Since $p$ is a unit quaternion, we have
\begin{equation}
  \label{eq:86}
  |p|=1 \implies \frac{d}{dt} (p^\top p) = 0 =  \dot p^\top p + p^\top \dot p =   2( p_o \dot p_0 + \vv{p}^T\vv{\dot p}).
\end{equation}
Therefore, the scalar part $(\dot p(t) \glaw p^{-1}(t))_0 =0$.
The quaternion product $\dot p(t) \glaw p^{-1}(t)$ and  $p_{x'}(t)$ is a product of quaternions with zero scalar part (see~\eqref{eq:74}), so we have
\begin{equation}
  \label{eq:87}
  \begin{array}{lcl}
    \dot p_{x'}(t) = 2 \dot p(t) \glaw p^{-1}(t)   \glaw p_{x'}(t).
  \end{array}
\end{equation}
In terms of vector of $\RR^3$, this corresponds to
\begin{equation}
  \label{eq:88}
  \dot x'(t) = 2 \vv{ \dot p(t) \glaw p^{-1}(t) } \times x'(t).
\end{equation}
Since $x'(t) = R(t) x$, we have $\dot x' = \dot R(t) x = \tilde \omega(t) R(t) x  = \tilde \omega(t) x'(t) $. Comparing \eqref{eq:87} and \eqref{eq:88}, we get
\begin{equation}
  \label{eq:89}
  \tilde \omega(t)  = 2 \vv{ \dot p(t) \glaw p^{-1}(t) }
\end{equation}
or equivalently
\begin{equation}
  \dot p(t) \glaw p^{-1}(t) = (0, \frac{\omega(t)}{2} )
  \label{eq:90}
\end{equation}
Finally, we can conclude that
\begin{equation}
  \label{eq:91}
  \dot p(t) = (0, \frac{\omega(t)}2 ) \glaw p(t).
\end{equation}
Since $\omega(t)=R(t)\Omega(t)$, we have
\begin{equation}
  \label{eq:92}
  (0, \omega(t) ) = (0, R(t) \Omega(t) ) = p(t) \glaw (0, \Omega(t) ) \glaw \bar p(t) = p(t) \glaw (0, \Omega(t) ) \glaw  p^{-1}(t)
\end{equation}
and then
\begin{equation}
  \label{eq:93}
  \dot p(t) =\frac 1 2 p(t) \glaw(0, \Omega(t) ) .
\end{equation}

The time derivation is compactly written
\begin{equation}
  \label{eq:94}
  \dot p = \frac  1 2 p  \glaw(0, \frac\Omega 2 ) =  [p_\glaw] p_{\frac \Omega 2} = \Psi(p)\frac \Omega 2,
\end{equation}
and using the matrix representation of product of  quaternion
we get
\begin{equation}
  \label{eq:95}
  \Psi(p) =  \begin{bmatrix}
    -p_1 & -p_2 & -p_3 \\
    p_0 & -p_3 & p_2 \\
    p_3 & p_0 & -p_1 \\
    -p_2 & p_1 & p_0 \\
  \end{bmatrix}
\end{equation}
The relation \eqref{eq:93} can be also inverted by writing
\begin{equation}
  \label{eq:96}
   (0, \Omega(t) ) = 2 p^{-1}(t) \glaw \dot p(t)
\end{equation}
Using again  matrix representation of product of  quaternion, we get
\begin{equation}
  \label{eq:97}
  \Omega(t)  = 2 \vv{p^{-1}(t) \glaw \dot p(t)}  = 2  \begin{bmatrix}
    -p_1 & p_0 & p_3 & -p_2 \\
    -p_2 & -p_3 & p_0 & p_1 \\
    -p_3 & p_2 & -p_1  & p_0\\
  \end{bmatrix}\dot p(t) = 2 \Psi(p)^\top \dot p(t)
\end{equation}
Note that we have $\Psi^\top(p)\Psi(p)= I_{4\times 4 }$ and  $\Psi(p)\Psi^\top(p)= I_{3\times 3 }$

\paragraph{Lie group structure of unit quaternions.} In terms of complex matrices, an unit quaternion $p$ satisfies
\begin{equation}
  \label{eq:125}
  \det\left(    \begin{bmatrix}
      x &y  \\
      - \bar y  & \bar x
    \end{bmatrix} \right) =1
\end{equation}
The set of all unit quaternions that we denote $\HH_1$ is the set of unitary matrices of determinant equal to $1$. From~\eqref{eq:131}, we get that
\begin{equation}
  \label{eq:126}
  p \glaw p^\star = e
\end{equation}
It implies that the set $\HH_1$ is the set of special unitary complex matrices. The set is a Lie group usually denoted as $SU(2)$. Since we used multiple representation of a quaternion, we continue to use $\HH_1 \cong SU(2)$ as a notation but with the Lie group structure implied by $SU(2)$.

Let us compute the tangent vector at a point $p \in \HH_1$. Let $q(t)$ be  a smooth curve $q(\cdot) : t\in \RR \mapsto q(t)\in H_1$ in $H_1$ such that $q(O)= p$.
Since $q(t)\in H_1$, we have $|q(t)|=1$ and then $\frac{d}{dt} |q(t)| = 2(q_0(0) \dot q_0(0) + \vec{q}^T(0) \vec{\dot q}(0) ) =0$. At $t=0$, we get
\begin{equation}
  \label{eq:48}
  2(p_0 a_0 + \vec{p}^T \vec{a})= 0.
\end{equation}
This relation imposes that the quaternions $2 p^\star \glaw a \in H_1$ and $2 a \glaw p^\star \in H_1$, that is, have to be pure quaternions. Therefore, it exists $\omega \in \RR^3$  and $\Omega \in \RR^3$ such that
\begin{equation}
  \label{eq:132}
  (0, \Omega) = 2 p ^\star \glaw a
\end{equation}
and
\begin{equation}
  \label{eq:132}
  (0, \omega) = 2 a\glaw p^\star
\end{equation}
In other terms, the tangent vector spaces at $p \in \HH_1$ can be represented as a left representation
\begin{equation}
  \label{eq:133}
  T_p\HH_1 = \{ a \mid a = p \glaw (0, \frac \Omega 2 ), \Omega \in \RR^3\}
\end{equation}
or a right representation
\begin{equation}
  \label{eq:1330}
  T_p{\HH_1} = \{ a \mid a =  (0, \frac \omega 2 ) \glaw p, \omega \in \RR^3\}
\end{equation}

At $p=e$, we get the Lie algebra defined by
\begin{equation}
  \label{eq:134}
  \mathfrak h_1 =  T_e{\HH_1} =  \{ a = (0, \frac \Omega 2 ), \Omega \in \RR^3 \}
\end{equation}
equipped with the Lie bracket given by the commutator
\begin{equation}
  \label{eq:135}
  [p,q] = p \glaw q- q\glaw p.
\end{equation}
We can easily verify that for $a = (0, \frac \Omega 2 ), \, b = (0, \frac \Gamma 2 ) \in \mathfrak h_1 $, we have
\begin{equation}
  \label{eq:136}
  [a,b] = (0, \frac \Omega 2 ) \glaw (0, \frac \Gamma 2 ) - (0, \frac \Gamma 2 ) \glaw  (0, \frac \Omega 2 )  = (0, \frac{\Omega\times \Gamma}{2}) \in \mathfrak h_1
\end{equation}
As for $\mathfrak so(3)$,  the Lie algebra $\mathfrak h_1$ is isomorphic to $\RR^3$ thanks to the operator $\widehat{(\cdot)} :\RR^3 \rightarrow \mathfrak h_1$ and defined by
\begin{equation}
  \label{eq:54}
 \widehat{(\cdot)}: \Omega \mapsto \widehat \Omega = (0, \frac \Omega 2 )
\end{equation}
With this operator, the Lie Bracket can be written
\begin{equation}
  \label{eq:137}
  [\widehat{\Omega},\widehat{\Gamma}] = \widehat{\Omega \times \Gamma}
\end{equation}

\paragraph{ A special  (right)  action of Lie Group $\mathcal G$ on a manifold $\mathcal M$. }
Let us come back to the representation of  $T_p\HH_1$ given in~\eqref{eq:133}. It is clear it can expressed with a representation that relies on $\mathfrak h_1$
\begin{equation}
  \label{eq:158}
   T_RSO(3) = \{ a = p \glaw  \widehat \Omega \mid \widehat \Omega \in \mathfrak h_1 \}.
\end{equation}
With \eqref{eq:58}, we see that there is a linear map that relates $T_p\HH_1$ to  $\mathfrak h_1$. This linear map defines a vector field.
A special group action is defined by the left translation map for a point $p \in \HH_1$
\begin{equation}
  \label{eq:159}
  \begin{array}[lcl]{rcl}
    L_p& :&   \HH_1 \rightarrow  \HH_1\\
       & &  q  \mapsto L_p(q) = p \glaw q\\
  \end{array}
\end{equation}
which is diffeomorphism on $\HH_1$. In that case, we identify the manifold and the group. So, $L_p$ can be viewed as a left or a right group action. We choose a right action. For our application where $\mathcal G = \mathcal M = \HH_1$ and $\Lambda^r(p,q) = L_{p}(q) =  p \glaw q $, we get
\begin{equation}
  \label{eq:160}
   \lambda^r_{*}(a)(p) = \left. \frac{d}{dt} L_{p}(q(t)) \right|_{t=0}  = \left. \frac{d}{dt} p \glaw q(t) \right|_{t=0} =  p \glaw \dot q(0) = p  \glaw \dot q(0)  \in X(\mathcal M)
 \end{equation}
 for a smooth curve $q(t)$ in $\HH_1$.
Since $q(\cdot)$ is a smooth curve in $\HH_1$, $\dot q(0)$ is a tangent vector at the point $q(0)=I$, that is an element $a = \widehat  \Omega  \in \mathfrak h_1 $ defined by the relation~\eqref{eq:33}. Therefore, the vector field in \eqref{eq:160} is a tangent vector field and we get
\begin{equation}
  \label{eq:161}
  \dot p(t) = \lambda^r_{*}(a)(p(t)) = p(t)  \glaw \widehat \Omega
\end{equation}

\paragraph{Exponential map $\expq : \mathfrak h_1 \rightarrow \HH_1$}
We can directly apply Theorem~\ref{Theorem:solutionofLieODE} and we get that the solution of
\begin{equation}
  \label{eq:130}
  \begin{cases}
  \dot p(t) = \lambda^r_{*}(a)(p(t)) = p(t) \glaw \widehat \Omega \\
  p(0) = Rp_0
\end{cases}
\end{equation}
 is
\begin{equation}
  \label{eq:138}
  p(t) = p_0 \expq(t \widehat \Omega)
\end{equation}
The exponential mapping $\expq : \mathfrak h_1 \rightarrow \HH_1$ can also be defined as $\expq(\widehat \Omega) = q(1)$ where $q (t)$ satisfies the  differential equation
\begin{equation}
  \label{eq:235}
  \dot q(t) = q(t) \cdot \widehat \Omega , \quad q (0) = e.
\end{equation}
Using the quaternion product, the exponential map can be expressed as
\begin{equation}
  \label{eq:232}
  \expq(t \widehat \Omega ) = \sum_{k=0}^\infty \frac{(t\widehat \Omega)^k}{k!}
\end{equation}
since it is  a solution of \eqref{eq:130}. A simple computation allows to check this claim:
\begin{equation}
  \label{eq:233}
   \frac{d}{dt}\expq(t \widehat \Omega ) = \sum_{k=1}^\infty  k t^{k-1} \frac{ \widehat \Omega ^k}{k!} =  \sum_{k=0}^\infty  t^{k} \frac{t \widehat \Omega ^k}{k!}\glaw  \widehat \Omega  =   \expq(t \widehat \Omega ) \glaw \widehat \Omega.
\end{equation}

A closed form relation for the form the quaternion exponential can also be found by noting that
\begin{equation}
  \label{eq:140}
  \widehat \Omega ^2  = - \left(\frac \theta 2 \right)^2 e, \text{ and } \widehat \Omega ^3  = - \left(\frac \theta 2 \right)^2 \widehat \Omega.
\end{equation}
A simple expansion of \eqref{eq:232} at $t=1$ equals
\begin{equation}
  \label{eq:141}
  \begin{array}{lcl}
    \expq(\widehat \Omega ) &=& \sum_{k=0}^\infty \frac{(\widehat \Omega)^k}{k!}\\
                            &=& \sum_{k=0}^\infty \frac{(-1)^k}{(2k)!}\left(\frac \theta 2 \right)^{2k} e + \sum_{k=0}^\infty \frac{(-1)^k}{(2k+1)!} \left(\frac \theta 2 \right)^{2k+1} \widehat \Omega \\
                            &=& \cos(\frac \theta 2) e + \frac{\sin(\frac \theta 2)}{\frac \theta 2} \widehat \Omega \\
  \end{array}
\end{equation}
that is
\begin{equation}
  \label{eq:144}
  \expq(\widehat \Omega )  = (\cos(\frac \theta 2), \sin(\frac \theta 2) \frac{\Omega}{\theta}   ).
\end{equation}

\paragraph{Adjoint representation}
In the case of $\HH_1$, the definition of the operator $\Ad$ gives
\begin{equation}
  \label{eq:121}
  \Ad_p(\widehat\Omega)  = p\glaw \widehat\Omega p^\star
\end{equation}
 and then mapping $\ad_{\widehat\Omega}(\widehat \Gamma)$ is defined by
\begin{equation}
  \label{eq:56}
  \ad_{\widehat\Omega}(\widehat\Gamma) = \widehat \Omega \widehat\Gamma - \widehat \Gamma \widehat\Omega  =  [\widehat \Omega,\widehat \Gamma] = \widehat{\Omega \times \Gamma}.
\end{equation}
Using the isomorphism between $\mathfrak h_1$ and $\RR^3$, we can use the  the mapping $\ad_{\Omega}(\Gamma) : \RR^3\times\RR^3 \rightarrow \RR^3$ given by \eqref{eq:55} to get
\begin{equation}
  \label{eq:145}
   \ad_{\widehat\Omega}(\widehat\Gamma) = \widehat{\Omega \times \Gamma} = \widehat{\ad_{\Omega}(\Gamma)} =  \widehat{\tilde \Omega \Gamma}
\end{equation}

\paragraph{Differential of the exponential map $\dexpq$}
The differential of the exponential mapping, denoted by $\dexpq$ is defined as the 'right trivialized' tangent of the exponential map
\begin{equation}
  \label{eq:71}
  \frac{d}{dt} (\expq(\widehat \Omega(t))) = \dexpq_{\widehat\Omega(t)}(\frac{d \widehat{\Omega}(t)}{dt}) \expq(\widehat\Omega(t))
\end{equation}

An explicit expression of $\dexp_{\widehat\Omega}(\widehat\Gamma)$ can also be developed either by developing the expansion and~\eqref{eq:137}.
\begin{equation}
  \label{eq:168}
   \dexpq_{\widehat\Omega}(\Gamma) = \sum_{k=0}^\infty \frac{1}{(k+1)!} \ad_{\widehat\Omega}^k (\widehat\Gamma) = \widehat{T(\Omega)\Gamma}
\end{equation}

\begin{remark}
Note that the time derivative in $\RR^4$ is not differential mapping.
The standard time derivative of $\expq$ in the expression \eqref{eq:144} gives
\begin{equation}
  \label{eq:171}
    \frac{d}{dt}\expq(\widehat \Gamma(t)) = (- \frac{\sin(\theta)}{\theta} \Omega^T\Gamma, \frac{\sin(\theta)}{\theta}\Gamma  +\frac{\theta \cos(\theta)-\sin(\theta)}{\theta^3}\Omega^T\Omega \Gamma  )
\end{equation}
that can be expressed in $\RR^4$ by
\begin{equation}
  \label{eq:175}
  \frac{d}{dt}\expq(\widehat \Gamma(t))  = \nabla \expq(\widehat\Omega) \widehat{\dot\Omega}
\end{equation}
with
\begin{equation}
  \label{eq:176}
  \nabla \expq(\widehat\Omega) =
  \begin{bmatrix}
    - \frac{\sin(\theta)}{\theta} \Omega^T \\
    \frac{\sin(\theta)}{\theta}I  +\frac{\theta \cos(\theta)-\sin(\theta)}{\theta^3}\Omega^T\Omega
  \end{bmatrix}
\end{equation}

Clearly, we have
\begin{equation}
  \label{eq:177}
  \nabla \expq(\widehat\Omega) \neq  \dexpq_{\widehat\Omega}
\end{equation}
\end{remark}

\paragraph{Directional derivative and Jacobians of functions of a quaternion}
\begin{ndrva}
  experimental
\end{ndrva}

Let $f : \HH_1 \rightarrow \RR $ be a mapping from the group to $\RR^3$. The directional derivative of $f$ in the direction $\widehat \Omega \in \mathfrak h_1$ at $p\in \HH_1$ is
defined by
\begin{equation}
  \label{eq:139}
 df_p(\widehat \Omega) =\left. \frac{d}{dt} f(p\glaw \expq(t\widehat \Omega)) \right|_{t=0}
\end{equation}

As a first simple example let us choose $f(p) = \vv{p \glaw p_x \glaw p^\star}$ for a given $x \in \RR^3 $, we get
\begin{equation}
  \label{eq:142}
  \begin{array}{lcl}
    D Id \cdot \widehat \Omega (p) = (\widehat \Omega^r f )(p) &=& \left. \frac{d}{dt}\vv{p\glaw \expq(t\widehat \Omega) \glaw p_x \glaw (p \glaw \expq(t\widehat \Omega))^\star}  \right|_{t=0}\\
                                                               & = & \vv{p\glaw \frac{d}{dt}\left. \expq(t\widehat \Omega) \right|_{t=0} \glaw p_x \glaw p^\star +  p \glaw p_x \glaw (p \glaw\frac{d}{dt}\left. \expq(t\widehat \Omega) \right|_{t=0})^\star}\\
  \end{array}
\end{equation}
We have form the definition of the time derivative of the exponential
\begin{equation}
  \label{eq:143}
  \begin{array}{lcl}
    \frac{d}{dt}\left. \expq(t\widehat \Omega) \right|_{t=0} &=&  \left. \dexpq_{\widehat\Omega}(\widehat \Omega)\expq(t\widehat \Omega) \right|_{t=0} \\
                                                            &=&  \dexpq_{\widehat\Omega}(\widehat \Omega)
  \end{array}
\end{equation}

Then, the directional derivative can be written
\begin{equation}
  \label{eq:146}
  \begin{array}{lcl}
    D Id \cdot \widehat \Omega (p) &=& \vv{p\glaw \dexpq_{\widehat\Omega}(\widehat \Omega)\glaw p_x \glaw p^\star  + p \glaw p_x \glaw (\dexpq_{\widehat\Omega}(\widehat \Omega))^* \glaw  p^\star } \\
  &=& \vv{p\glaw ( \dexpq_{\widehat\Omega}(\widehat \Omega)\glaw p_x +   p_x \glaw (\dexpq_{\widehat\Omega}(\widehat \Omega))^*) \glaw  p^\star }
  \end{array}
\end{equation}

\section{Newton-Euler equation in quaternion  form}

\paragraph{Computation of $T$ for unit quaternion} The operator $T(q)$ is directly obtained as
\begin{equation}
  T(q)=\frac 1 2 \label{eq:98}
  \begin{bmatrix}
    2 I_{3\times 3} & & 0_{3\times 3} & \\
    &   -p_1 & -p_2 & -p_3 \\
    0_{4\times 3}  &  p_0 & -p_3 & p_2 \\
    & p_3 & p_0 & -p_1 \\
    & -p_2 & p_1 & p_0
  \end{bmatrix}
\end{equation}

\paragraph{}

\begin{ndrva}
  todo :
  \begin{itemize}
  \item computation of the directional derivative of $R(\Omega)= exp(\tilde \Omega)$ in the direction $\tilde\Omega$, to get $T(\Omega)$
  \end{itemize}
\end{ndrva}

\paragraph{Quaternion representation}If the Lie group is described by unit quaternion, we get
\begin{equation}
  \label{eq:99}
  SO(3) = \{p = (p_0,\vv{p}) \in \RR^{4}\mid |p|=1  \}
\end{equation}
with the composition law  $p_1\glaw p_2$ given by the quaternion product.

Note that the concept of exponential map for Lie group that are not parameterized by matrices is also possible.

\subsection{Mechanical systems  with bilateral and unilateral constraints}
\label{section22}

Let us consider that the system~(\ref{eq:Newton-Euler-compact}) is  subjected to $m$ constraints, with $m_{e}$ holonomic bilateral
constraints
\begin{equation}
  \label{eq:bilateral-constraints}
  h^\alpha(q)=0, \alpha \in \mathcal{E}\subset\NN,  |\mathcal E| = m_e,
\end{equation}
and  $m_{i}$ unilateral constraints
\begin{equation}
  \label{eq:unilateral-constraints}
  g_{\n}^\alpha(q)\geq 0, \alpha \in \mathcal{I}\subset\NN,  |\mathcal I| = m_i.
\end{equation}
%
Let us denote as $J^\alpha_h(q) = \nabla^\top_q h^\alpha(q)  $ the Jacobian matrix of the bilateral constraint $h^\alpha(q)$ with respect to $q$ and as $J^\alpha_{g_\n}(q)$ respectively for $g_{\n}^\alpha(q)$  .
%
The bilateral constraints at the velocity level can be obtained as:
\begin{equation}
  \label{eq:bilateral-constraints-velocity}
 0 = \dot h^\alpha(q)= J^\alpha_h(q)\dot q = J^\alpha_h(q) T(q) v \coloneqq H^\alpha(q)  v,\quad  \alpha \in \mathcal{E}.
\end{equation}
By duality and introducing a Lagrange multiplier $\lambda^\alpha, \alpha \in \mathcal E$, the constraint generates a force applied to the body equal to $H^{\alpha,\top}(q)\lambda^\alpha$. For the unilateral constraints, a Lagrange multiplier $\lambda_{\n}^\alpha, \alpha \in \mathcal I$ is also associated and the constraints at the velocity level can also be derived as
\begin{equation}
  \label{eq:unilateral-constraints-velocity}
 0 \leq  \dot g_\n^\alpha(q)= J^\alpha_{g_\n}(q) \dot q = J^\alpha_{g_\n}(q)  T(q) v , \text{ if } g_{\n}^\alpha(q) = 0,\quad  \alpha \in \mathcal{I}.
\end{equation}
Again, the force applied to the body is given by $(J^\alpha_{g_\n}(q) T(q))^\top\lambda^\alpha_\n$. {Nevertheless, there is no reason that $\lambda^\alpha_\n =r^\alpha_\n$ and $u_\n = J^\alpha_{g_\n}(q) T(q) v$ if the $g_n$ is not chosen as the signed distance (the gap function)}. This is the reason why  we prefer  directly define the normal and the tangential local relative velocity with respect to the {twist vector} as
\begin{equation}
  \label{eq:unilateral-constraints-velocity-kinematic1}
   u^\alpha_\n  \coloneqq G_\n^\alpha(q) v, \quad u^\alpha_\t  \coloneqq G_\t^\alpha(q) v, \quad \alpha \in \mathcal{I},
\end{equation}
and the associated force as $G_\n^{\alpha,\top}(q) r^{\alpha}_\n $ and $G_\t^{\alpha,\top}(q) r^{\alpha}_\t$. For the sake of simplicity, we use the notation $u^\alpha  \coloneqq G^\alpha(q) v$ and its associated total force generated by the contact $\alpha$ as $G^{\alpha,\top}(q) r^{\alpha} \coloneqq G_\n^{\alpha,\top}(q) r^{\alpha}_\n + G_\t^{\alpha,\top}(q) r^{\alpha}_\t $.

The complete system of equation of motion can finally be written as
\begin{numcases}{ }
  ~~\dot q = T(q)v ,\nonumber \\[0.5ex]
  ~~ M \dot v  = F(t,q,v) + H^\top(q) \lambda +  G^\top(q) r, \nonumber \\ [0.5ex]
  ~~\begin{array}{ll}
    H^\alpha(q) v  =  0 ,& \alpha \in \mathcal E \\[1ex]
    \left. \begin{array}{ll}
      r^\alpha= 0 , &\text{ if } g_{\n}^\alpha(q) > 0,\\[1ex]
      {K}^{\alpha,*} \ni \widehat u^\alpha  \bot~ r^\alpha \in {K}^\alpha, &\text{ if } g_{\n}^\alpha(q) = 0, \\[1ex]
      u_{\n}^{\alpha,+} = -e_r^\alpha u_{\n}^{\alpha,-}, &\text{ if } g_{\n}^\alpha(q) = 0 \text{ and } u_{\n}^{\alpha,-} \leq 0,
    \end{array}\right\} & \alpha \in \mathcal I  \label{eq:NewtonEuler-uni}
\end{array}
\end{numcases}
where the definition of the variables $\lambda\in \RR^{m_e}, r\in \RR^{3m_i}$ and the operators $H,G$ are extended to collect all the variables for each constraints.

Note that all the constraints are written at the velocity integrators. {Another strong advantage is the straightforward introduction of  the contact dissipation processes that are naturally written at the velocity level such as the Newton impact law and the Coulomb friction. Indeed, in Mechanics, dissipation processes are always given in terms of rates of changes, or if we prefer, in terms of velocities.}

\paragraph{Siconos Notation} In the siconos notation, we have for the applied torques on the system the following decomposition
\begin{equation}
  F(t,q,v):= \begin{pmatrix}
    f(t,x_{\cg},  v_{\cg}, R, \Omega ) \\
    I \Omega \times \Omega + M(t,x_{\cg}, v_{\cg}, R, \Omega )
  \end{pmatrix}
  := \begin{pmatrix}
    f_{ext}(t)  - f_{int}(x_{\cg},  v_{\cg}, R, \Omega ) \\
    - M_{gyr}(\Omega) + M_{ext}(t) -  M_{int}(x_{\cg}, v_{\cg}, R, \Omega )
  \end{pmatrix}.
\end{equation}
with
\begin{equation}
  M_{gyr} := \begin{pmatrix}
     \Omega \times I\Omega
  \end{pmatrix}
\end{equation}

In the siconos notation, we have for the relation
\begin{equation}
  \label{eq:100}
   C =   J^\alpha(q) \quad CT = J^\alpha(q)T(q)
\end{equation}

\section{Time integration scheme in scheme}

\subsection{Moreau--Jean scheme based on a  $\theta$-method}
The complete Moreau--Jean scheme based on a  $\theta$-method is written as follows
 \begin{equation}
    \label{eq:Moreau--Jean-theta}
    \begin{cases}
      ~~\begin{array}{l}
        q_{k+1} = q_{k} + h T(q_{k+\theta}) v_{k+\theta} \quad \\[1ex]
        M(v_{k+1}-v_k) - h  F(t_{k+\theta}, q_{k+\theta},v_{k+\theta}) =  H^\top(q_{k+1}) Q_{k+1} + G^\top(q_{k+1}) P_{k+1},\quad\,\\[1ex]
      \end{array}\\
      ~~\begin{array}{lcl}
        \begin{array}{l}
          H^\alpha(q_{k+1}) v_{k+1}  =  0\\
        \end{array} & \left. \begin{array}{l}
          \vphantom{H^\alpha(q_{k+1}) v_{k+1}  =  0}\\[1ex]
        \end{array}\right\}    &\alpha \in \mathcal E  \\[1ex]
      ~~~P_{k+1}^\alpha= 0, &
      \left. \begin{array}{l}
          \vphantom{P_{k+1}^\alpha= 0,  \delta^\alpha_{k+1}=0}\\[1ex]
        \end{array}\right\}   & \alpha \not\in \mathcal I^\nu \\[1ex]
      %
      %
      \begin{array}{l}
          {K}^{\alpha,*} \ni \widehat u_{k+1}^\alpha~ \bot~ P_{k+1}^\alpha \in {K}^\alpha \\
      \end{array} &
      \left.\begin{array}{l}
          \vphantom{{K}^{\alpha,*} \ni \widehat u_{k+1}^\alpha~ \bot~ P_{k+1}^\alpha \in {K}^\alpha} \\
        \end{array}\right\}
      &\alpha \in \mathcal I^\nu\\
  \end{array}
\end{cases}
\end{equation}
where $\mathcal I^\nu$ is the set of forecast constraints, that may be evaluated as
\begin{equation}
  \label{eq:101}
  \mathcal I^\nu = \{\alpha \mid \bar g_\n^\alpha \coloneqq g_\n + \frac h 2 u^\alpha_\n \leq 0\}.
\end{equation}

\subsection{Semi-explicit version Moreau--Jean scheme based on a  $\theta$-method}

\begin{equation}
    \label{eq:Moreau--Jean-explicit}
    \begin{cases}
      ~~\begin{array}{l}
        q_{k+1} = q_{k} + h T(q_{k}) v_{k+\theta} \quad \\[1ex]
        M(v_{k+1}-v_k) - h  F(t_{k}, q_{k},v_{k}) =  H^\top(q_{k}) Q_{k+1}+  G^\top(q_{k}) P_{k+1},\quad\,\\[1ex]
      \end{array}\\
      ~~\begin{array}{lcl}
        \begin{array}{l}
          H^\alpha(q_{k+1}) v_{k+1}  =  0\\
        \end{array} & \left. \begin{array}{l}
          \vphantom{H^\alpha(q_{k+1}) v_{k+1}  =  0}\\[1ex]
        \end{array}\right\}    &\alpha \in \mathcal E  \\[1ex]
      ~~P_{k+1}^\alpha= 0, &
      \left. \begin{array}{l}
          \vphantom{P_{k+1}^\alpha= 0,  \delta^\alpha_{k+1}=0}\\[1ex]
        \end{array}\right\}   & \alpha \not\in \mathcal I^\nu \\[1ex]
      %
      %
      \begin{array}{l}
          {K}^{\alpha,*} \ni \widehat u_{k+1}^\alpha~ \bot~ P_{k+1}^\alpha \in {K}^\alpha \\
      \end{array} &
      \left.\begin{array}{l}
          \vphantom{{K}^{\alpha,*} \ni \widehat u_{k+1}^\alpha~ \bot~ P_{k+1}^\alpha \in {K}^\alpha} \\
        \end{array}\right\}
      &\alpha \in \mathcal I^\nu\\
  \end{array}
\end{cases}
\end{equation}

In this version, the new velocity $v_{k+1}$ can be computed explicitly, assuming that the inverse of $M$ is easily written, as

\begin{equation}
  \label{eq:Moreau--Jean-theta--explicit-v}
  v_{k+1}   =  v_k + M^{-1} h  F(t_{k}, q_{k},v_{k}) +  M^{-1} (H^\top(q_{k}) Q_{k+1}+  G^\top(q_{k}) P_{k+1})
\end{equation}

\subsection{Nearly implicit version Moreau--Jean scheme based on a  $\theta$-method implemented in siconos}

A first simplification is made considering a given value of $q_{k+1}$ in $T()$, $H()$ and $G()$ denoted by $\bar q_k$. This limits the computation of the Jacobians of this operators with respect to $q$.
\begin{equation}
    \label{eq:Moreau--Jean-theta-nearly}
    \begin{cases}
      ~~\begin{array}{l}
        q_{k+1} = q_{k} + h T(\bar q_k) v_{k+\theta} \quad \\[1ex]
        M(v_{k+1}-v_k) - h  \theta F(t_{k+1}, q_{k+1},v_{k+1}) - h (1- \theta) F(t_{k}, q_{k},v_{k})  =  H^\top(\bar q_k) Q_{k+1} + G^\top(\bar q_k) P_{k+1},\quad\,\\[1ex]
      \end{array}\\
      ~~\begin{array}{lcl}
        \begin{array}{l}
          H^\alpha(\bar q_k) v_{k+1}  =  0\\
        \end{array} & \left. \begin{array}{l}
          \vphantom{H^\alpha(q_{k+1}) v_{k+1}  =  0}\\[1ex]
        \end{array}\right\}    &\alpha \in \mathcal E  \\[1ex]
      ~~P_{k+1}^\alpha= 0, &
      \left. \begin{array}{l}
          \vphantom{P_{k+1}^\alpha= 0,  \delta^\alpha_{k+1}=0}\\[1ex]
        \end{array}\right\}   & \alpha \not\in \mathcal I^\nu \\[1ex]
      %
      %
      \begin{array}{l}
          {K}^{\alpha,*} \ni \widehat u_{k+1}^\alpha~ \bot~ P_{k+1}^\alpha \in {K}^\alpha \\
      \end{array} &
      \left.\begin{array}{l}
          \vphantom{{K}^{\alpha,*} \ni \widehat u_{k+1}^\alpha~ \bot~ P_{k+1}^\alpha \in {K}^\alpha} \\
        \end{array}\right\}
      &\alpha \in \mathcal I^\nu\\
  \end{array}
\end{cases}
\end{equation}
The nonlinear residu is defined as
\begin{equation}
  \label{eq:Moreau--Jean-theta--nearly-residu}
  \mathcal R(v) =  M(v-v_k) - h  \theta F(t_{k+1}, q(v),v) - h (1- \theta) F(t_{k}, q_{k},v_{k}) - H^\top(\bar q_k) Q_{k+1} - G^\top(\bar q_k) P_{k+1}
\end{equation}
with
\begin{equation}
  \label{eq:Moreau--Jean-theta--nearly-residu1}
  q(v) = q_{k} + h T(\bar q_k)) ((1-\theta) v_k + \theta v).
\end{equation}
At each time step, we have to solve
\begin{equation}
  \label{eq:Moreau--Jean-theta--nearly-residu2}
  \mathcal R(v_{k+1}) =  0
\end{equation}
together with the constraints.

Let us write a linearization of the problem to design a Newton procedure:
\begin{equation}
  \label{eq:Moreau--Jean-theta--nearly-residu3}
  \nabla^\top_v \mathcal R(v^{\tau}_{k+1})(v^{\tau+1}_{k+1}-v^{\tau}_{k+1}) = -  \mathcal R(v^{\tau}_{k+1}).
\end{equation}
The computation of $ \nabla^\top_v \mathcal R(v^{\tau}_{k+1})$ is as follows
\begin{equation}
  \label{eq:102}
  \nabla^\top_v \mathcal R(v) = M - h \theta \nabla_v F(t_{k+1}, q(v),v)
\end{equation}
with
\begin{equation}
  \label{eq:103}
  \begin{array}{lcl}
    \nabla_v F(t_{k+1}, q(v),v) &=& D_2 F(t_{k+1}, q(v),v) \nabla_v q(v) + D_3 F(t_{k+1}, q(v),v) \\
                                &=& h \theta D_2 F(t_{k+1}, q(v),v) T(\bar q_k) + D_3 F(t_{k+1}, q(v),v) \\
  \end{array}
\end{equation}
where $D_i$ denotes the derivation with respect the $i^{th}$ variable. The complete Jacobian is then given by
\begin{equation}
  \label{eq:104}
  \nabla^\top_v \mathcal R(v) = M - h \theta D_3 F(t_{k+1}, q(v),v) - h^2 \theta^2 D_2 F(t_{k+1}, q(v),v) T(\bar q_k)
\end{equation}
In siconos, we ask the user to provide the functions $D_3 F(t_{k+1}, q ,v )$ and $D_2 F(t_{k+1}, q,v)$.

Let us denote by $W^{\tau}$ the inverse of  Jacobian of the residu,
\begin{equation}
  \label{eq:105}
  W^{\tau} = (M - h \theta D_3 F(t_{k+1}, q(v),v) - h^2 \theta^2 D_2 F(t_{k+1}, q(v),v) T(\bar q_k))^{-1}.
\end{equation}
and by $\mathcal R_{free}(v)$ the free residu,
\begin{equation}
  \label{eq:106}
  \mathcal R_{free}(v) =  M(v-v_k) - h  \theta F(t_{k+1}, q(v),v) - h (1- \theta) F(t_{k}, q_{k},v_{k}).
\end{equation}

The linear equation \ref{eq:Moreau--Jean-theta--nearly-residu3} that we have to solve is equivalent to
\begin{equation}
  \label{eq:107}
  \boxed{v^{\tau+1}_{k+1} = v^{\tau}_{k+1} - W  \mathcal R_{free}(v^\tau_{k+1}) + W   H^\top(\bar q_k) Q^{\tau+1}_{k+1} + W G^\top(\bar q_k) P^{\tau+1}_{k+1}}
\end{equation}
We define  $v_{free}$ as
\begin{equation}
  \label{eq:108}
  v_{free}  = v^{\tau}_{k+1} - W  \mathcal R_{free}(v^\tau_{k+1})
\end{equation}

The local velocity at contact can be written
\begin{equation}
  \label{eq:109}
  u^{\tau+1}_{\n,k+1} = G(\bar q_k) [  v_{free}^{\tau} + W   H^\top(\bar q_k) Q^{\tau+1}_{k+1} + W G^\top(\bar q_k) P^{\tau+1}_{k+1}]
\end{equation}
and for the equality constraints
\begin{equation}
  \label{eq:110}
  u^{\tau+1}_{k+1} = H(\bar q_k) [  v_{free}^{\tau} + W   H^\top(\bar q_k) Q^{\tau+1}_{k+1} + W G^\top(\bar q_k) P^{\tau+1}_{k+1}]
\end{equation}
Finally, we get a linear relation between $u^{\tau+1}_{\n,k+1}$ and the multiplier
\begin{equation}
  \label{eq:111}
 \boxed{ u^{\tau+1}_{k+1} =
  \begin{bmatrix}
    H(\bar q_k) \\
    G(\bar q_k)
  \end{bmatrix} v_{free}^{\tau}
  +
  \begin{bmatrix}
    H(\bar q_k)W   H^\top(\bar q_k) & H(\bar q_k)W   G^\top(\bar q_k) \\
    G(\bar q_k)W   H^\top(\bar q_k) & G(\bar q_k)W   G^\top(\bar q_k) \\
  \end{bmatrix}
  \begin{bmatrix}
    Q^{\tau+1}_{k+1} \\
    P^{\tau+1}_{k+1}
  \end{bmatrix}}
\end{equation}

\paragraph{choices for $\bar q_k$} Two choices are possible for $\bar q_k$
\begin{enumerate}
\item $\bar q_k = q_k$
\item $\bar q_k = q^{\tau}_{k+1}$
\end{enumerate}

\begin{ndrva}

  todo list:

  \begin{itemize}

  \item add the projection step for the unit quaternion

  \item describe the computation of H and G that can be hybrid

\end{itemize}

\end{ndrva}

\subsection{Computation of the Jacobian in special case}

\paragraph{Moment of gyroscopic forces}
Let us denote by the basis vector $e_i$ given the $i^{th}$ column of the identity matrix $I_{3\times3}$. The Jacobian of $M_{gyr}$ is given by
\begin{equation}
  \label{eq:112}
  \nabla^\top_\Omega M_{gyr}(\Omega) = \nabla^\top_\Omega (\Omega \times I \Omega) =
  \begin{bmatrix}
    e_i \times I \Omega + \Omega \times I e_i, i =1,2,3
  \end{bmatrix}
\end{equation}

\paragraph{Linear internal wrench}
If the internal wrench  is given by
\begin{equation}
  \label{eq:113}
  F_{int}(t,q,v) =
  \begin{bmatrix}
    f_{int}(t,q,v)\\
    M_{int}(t,q,v)
  \end{bmatrix}
  = C v + K q, \quad C \in \RR^{6\times 6}, \quad K \in \RR^{6\times 7 }
\end{equation}
we get
\begin{equation}
  \label{eq:114}
  \begin{array}{lcl}
    \nabla_v F(t_{k+1}, q(v),v)  &=& h \theta K T(\bar q_k) + C \\
    \nabla^\top_v \mathcal R(v) &=& M - h \theta C - h^2 \theta^2 K T(\bar q_k)
  \end{array}
\end{equation}

\paragraph{External moment given in the inertial frame}

If the external moment denoted by $m_{ext} (t)$ is expressed in inertial frame, we have
\begin{equation}
  \label{eq:115}
  M_{ext}(q,t) = R^T m_{ext}(t)= \Phi(p) m_{ext}(t)
\end{equation}
In that case, $  M_{ext}(q,t)$ appears as a function $q$ and we need to compute its Jacobian w.r.t $q$. This computation needs the computation of
\begin{equation}
  \label{eq:116}
  \nabla_{p} M_{ext}(q,t) = \nabla_{p} \Phi(p) m_{ext}(t)
\end{equation}
Let us compute first
\begin{equation}
  \label{eq:117}
  \Phi(p) m_{ext}(t)  =
  \begin{bmatrix}
    (1-2 p_2^2- 2 p_3^2)m_{ext,1} + 2(p_1p_2-p_3p_0)m_{ext,2} + 2(p_1p_3+p_2p_0)m_{ext,3}\\
    2(p_1p_2+p_3p_0)m_{ext,1}  +(1-2 p_1^2- 2 p_3^2)m_{ext,2} + 2(p_2p_3-p_1p_0)m_{ext,3}\\
    2(p_1p_3-p_2p_0)m_{ext,1}  + 2(p_2p_3+p_1p_0)m_{ext,2}  + (1-2 p_1^2- 2 p_2^2)m_{ext,3}\\
  \end{bmatrix}
\end{equation}
Then we get
\begin{equation}
  \label{eq:118}
  \begin{array}{l}
  \nabla_{p} \Phi(p) m_{ext}(t)  =\\
  \begin{bmatrix}
    -2 p_3 m_{ext,2} + 2 p_2 m_{ext,3} & 2p_2 m_{ext,2}+2 p_3 m_{ext,3}  & -4 p_2 m_{ext,1} +2p_1 m_{ext,2}+2 p_0 m_{ext,3} & -3 p_3 m_{ext,1} -2p_0 m_{ext,2} +2 p_1m_{ext,3}  \\
    2p_3 m_{ext,1} -2p_1m_{ext,3}  & 2p_2m_{ext,1} -4p_1 m_{ext,2} -2p_1 m_{ext,3} & & &  \\
  \end{bmatrix}
  \end{array}
\end{equation}

\subsection{Siconos implementation}

The expression:~$\mathcal R_{free}(v^\tau_{k+1}) = M(v-v_k) - h  \theta F(t_{k+1}, q(v^\tau_{k+1}),v^\tau_{k+1}) - h (1- \theta) F(t_{k}, q_{k},v_{k})$ is computed in {\tt MoreauJeanOSI::computeResidu()} and saved in {\tt ds->workspace(DynamicalSystem::freeresidu)}

The expression:~$\mathcal R(v^\tau_{k+1}) =\mathcal R_{free}(v^\tau_{k+1}) - h (1- \theta) F(t_{k}, q_{k},v_{k}) - H^\top(\bar q_k) Q_{k+1} - G^\top(\bar q_k) P_{k+1}  $ is computed in {\tt MoreauJeanOSI::computeResidu()} and saved in {\tt ds->workspace(DynamicalSystem::free)}.
\begin{ndrva}
  really a bad name for the buffer {\tt ds->workspace(DynamicalSystem::free)}. Why we are chosing this name ? to save some memory ?
\end{ndrva}

The expression:~$v_{free}  = v^{\tau}_{k+1} - W  \mathcal R_{free}(v^\tau_{k+1})$ is compute in {\tt MoreauJeanOSI::computeFreeState()} and saved in {\tt d->workspace(DynamicalSystem::free)}.

The computation:~ $v^{\tau+1}_{k+1} = v_{free} + W   H^\top(\bar q_k) Q^{\tau+1}_{k+1} + W G^\top(\bar q_k) P^{\tau+1}_{k+1}$ is done in {\tt MoreauJeanOSI::updateState} and stored in {\tt d->twist()}.\\

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "DevNotes"
%%% End:


% When using TeXShop on the Mac, let it know the root document. The following must be one of the first 20 lines.
% !TEX root = ../design.tex

\chapter[ARIMA]{ARIMA}
\begin{moduleinfo}
\item[Authors] {Mark Wellons}
\item[History]
	\begin{modulehistory}
    		\item[v0.1] Initial version
	\end{modulehistory}
\end{moduleinfo}

% Abstract. What is the problem we want to solve?
\section{Introduction}

An ARIMA model is an \textit{a}uto-\textit{r}egressive \textit{i}ntegrated
\textit{m}oving \textit{a}verage model.  An ARIMA model is typically expressed
in the form
\begin{equation}
(1 - \phi(B)) Y_t  = (1 + \theta(B)) Z_t,
\end{equation}
where $B$ is the backshift operator. The time $t$ is from $1$ to $N$.

ARIMA models involve the following variables:
\begin{enumerate}
   \item The lag difference $Y_{t}$, where  $Y_{t} = (1-B)^{d}(X_{t} - \mu)$.
    \item The values of the time series $X_t$.
    \item $p$, $q$, and $d$ are the parameters of the ARIMA model.
      $d$ is the differencing order, $p$ is the order of the AR
      operator, and $q$ is the order of the MA operator.
    \item The AR operator $\phi(B)$.
    \item The MA operator $\theta(B)$.
    \item The mean value $\mu$, which is always set to be zero for
      $d>0$ or need to be estimated.
    \item The error terms $Z_t$.
\end{enumerate}

\subsection{AR \& MA Operators}
The  auto regression operator models the prediction for the next
observation  as some linear combination of the previous observations.
More formally, an AR operator of order $p$ is defined as
\begin{align}
\phi(B) Y_t= \phi_1 Y_{t-1}   + \dots +  \phi_{p} Y_{t-p}
\end{align}

The moving average operator is similar, and it models the prediction
for the next observation as a linear combination of the errors in the
previous prediction errors.  More formally, the MA operator of order
$q$ is defined as
\begin{align}
\theta(B) Z_t =   \theta_{1} Z_{t-1} + \dots + \theta_{q} Z_{t-q}.
\end{align}

\section{Solving for the model parameters}\label{sec:para_est}

\subsection{Least Squares}\label{sec:CLS}
We assume that
\begin{equation}
\Pr(Z_t) = \frac{1}{\sqrt{2 \pi \sigma^2}} e^{-Z^2_t/2 \sigma^2}, \quad t > 0
\end{equation}
and that  $Z_{-q+1} = Z_{-q+2} = \dots = Z_0 = Z_1 = \dots = Z_p =
0$. The initial values of $Y_t=X_t-\mu$ for $t=-p+1, -p+2, \dots,
0$ can be solved from the following linear equations
\begin{eqnarray}
\phi_1 Y_0 + \phi_2 Y_{-1} + \cdots + \phi_p Y_{-p+1} &=& Y_1 \nonumber\\
\phi_2 Y_0 + \cdots + \phi_p Y_{-p+2} &=& Y_2 - \phi_1 Y_1  \nonumber\\
&\vdots& \nonumber\\
\phi_{p-1} Y_0 + \phi_p Y_{-1} &=& Y_{p-1} - \phi_1 Y_{p-2} - \cdots -
\phi_{p-2} Y_1 \nonumber \\
\phi_p Y_0  &=& Y_p - \phi_1 Y_{p-1} - \cdots - \phi_{p-1} Y_{1} \label{eq:init_Y}
\end{eqnarray}

The likelihood function $L$ for $N$ values of $Z_t$  is then
\begin{equation}
L(\phi, \theta) = \prod_{t = 1}^N  \frac{1}{\sqrt{2 \pi \sigma^2}} e^{-Z^2_t/2 \sigma^2}
\end{equation}
so the log likelihood function $l$ is
\begin{align}
l(\phi, \theta) &= \sum_{t = 1}^N \ln \left(\frac{1}{\sqrt{2 \pi \sigma^2}} e^{-Z^2_t/2 \sigma^2}
 \right) \nonumber\\
 &=  \sum_{t = 1}^N  - \ln \left( \sqrt{2 \pi \sigma^2}\right)  -\frac{Z^2_t}{2 \sigma^2}\nonumber\\
&=  -\frac{N}{2} \ln \left( 2 \pi \sigma^2\right)  - \frac{1}{2
  \sigma^2} \sum_{t = 1}^N   Z^2_t\ . \label{eq:loglikelihood}
\end{align}
Thus, finding the maximum likelihood is equivalent to solving the
optimization problem (known as the conditional least squares
formation)
\begin{align}
\min_{\theta, \phi} \sum_{t = 1}^N  Z^2_t.
\end{align}
The error term $Z_t$ can be computed iteratively as follows:
\begin{align}\label{equ:error terms}
Z_t = X_t - F_t(\phi, \theta, \mu)
\end{align}
where
\begin{align}
F_t(\phi, \theta, \mu) = \mu + \sum_{i=1}^p \phi_i (X_{t-i}-\mu) + \sum_{i=1}^q \theta_i Z_{t-i}
\end{align}

\subsubsection{Levenberg-Marquardt Algorithm}
In mathematics and computing, the Levenberg-Marquardt algorithm (LMA),
also known as the damped least-squares (DLS) method, provides a
numerical solution to the problem of minimizing a function, generally
nonlinear, over a space of parameters of the function. These
minimization problems arise especially in least squares curve fitting
and nonlinear programming.

%Author's (Mark W) Note: This is taken from http://people.duke.edu/~hpgavin/ce281/lm.pdf
To understand the Levenberg-Marquardt algorithm, it helps to know the
gradient descent method and the Gauss-Newton method.  On many
``reasonable'' functions, the gradient descent method takes large
steps when the current iterate is distant from the true solution, but
is slow to converge an the current iterate nears the true solution.
The Gauss-Newton method is much faster for converging when the current
iterate is in the neighborhood of the true solution.  The
Levenberg-Marquardt algorithm tries to get the best of best worlds,
and combine the gradient descent step with Gauss-Newton step in a
weighted average.  For iterates far from the true solution, the step
favors the gradient descent step, but as the iterate approaches the
true solution, the Gauss-Newton step dominates.

%Author's (Mark W) Note: Sudo code taken from http://users.ics.forth.gr/~lourakis/levmar/levmar.pdf
Like other numeric minimization algorithms, LMA is an iterative
procedure.  To start a minimization, the user has to provide an
initial guess for the parameter vector, $p$, as well as some tuning
parameters $\tau, \epsilon_1, \epsilon_2, \epsilon_3,$ and $k_{max}$.
Let $Z(p)$ be the vector of calculated errors ($Z_t$'s) for the
parameter vector $p$, and let $J = (J_{1}, J_{2}, \dots, J_N)^T$
be a Jacobian matrix.

A proposed implementation is as follows:
\begin{algorithm}
\alginput{An initial guess for parameters $\vec{\phi}_0, \vec{\theta}_0, \mu_0$}
\algoutput{The parameters that maximize the likelihood $\vec{\phi}^*,
  \vec{\theta}^*, \mu^*$}
\begin{algorithmic}[1]
	\State $k \leftarrow 0$  \Comment{Iteration counter}
	\State $v \leftarrow 2$ \Comment{The change in the weighting factor.}
	\State $(\vec{\phi},\vec{\theta},\mu) \leftarrow (\vec{\phi}_0,\vec{\theta}_0,\mu_0)$ \Comment{Initialize parameter vector}
	\State Calculate $Z(\vec{\phi},\vec{\theta},\mu)$ with equation \ref{equ:error terms}.  \Comment{Vector of errors}
	\State $A \leftarrow J^T J$   \Comment{The  Gauss-Newton Hessian approximation}
	\State $u \leftarrow \tau * \max_i(A_{ii})$ \Comment{Weight of the gradient-descent step}
	\State $g \leftarrow J^T Z(\vec{\phi},\vec{\theta},\mu)$ \Comment{The gradient descent step.}
	\State $ \text{stop} \leftarrow (\|g\|_{\infty} \le \epsilon_1)$ \Comment{Termination Variable}
	\While{(not stop) and ($k < k_{max}$)}
		\State $k \leftarrow k + 1$
		\Repeat
			\State $\delta \leftarrow (A + u \times \text{diag}(A))^{-1} g$ \Comment{Calculate step direction}
			\If{$\| \delta \| \le \epsilon_2 \|
              (\vec{\phi},\vec{\theta},\mu) \|$} \Comment{Change in the parameters is too small to continue.}
				\State  $\text{stop} \leftarrow \text{true}$
			\Else
				\State $(\vec{\phi}_{new},\vec{\theta}_{new},\mu_{new}) \leftarrow (\vec{\phi},\vec{\theta},\mu) + \delta$ \Comment{Take a trial step in the new direction}
				\State $\rho \leftarrow (\| Z(\vec{\phi},\vec{\theta},\mu)\|^2 - \| Z(\vec{\phi}_{new},\vec{\theta}_{new},\mu_{new})\|^2 )/(\delta^T(u \delta + g))$ \Comment{Calculate improvement of trial step}
				\If{$\rho > 0$} \Comment{Trial step was good, proceed to next iteration}
					\State $(\vec{\phi},\vec{\theta},\mu) \leftarrow (\vec{\phi}_{new},\vec{\theta}_{new},\mu_{new})$ \Comment{Update variables}
					\State Calculate $Z(\vec{\phi},\vec{\theta},\mu)$ with equation \ref{equ:error terms}.
					\State $A \leftarrow J^T J$
					\State $g \leftarrow J^T Z(\vec{\phi},\vec{\theta},\mu)$
					\State $ \text{stop} \leftarrow (\|g\|_{\infty} \le \epsilon_1)$ or $(\| Z(\vec{\phi},\vec{\theta},\mu)^2 \| \le \epsilon_3)$  \Comment{Terminate if we are close to the solution.}
					\State $v \leftarrow 2$
					\State $u \rightarrow u * \max(1/3, 1 - (2\rho - 1)^3 )$
				\Else  \Comment{Trial step was bad, change weighting on the gradient decent step}
					\State $v \leftarrow 2 v$
					\State $u \leftarrow u v$
				\EndIf
			\EndIf
		\Until{(stop) or ($\rho > 0$) }
	\EndWhile
	\State $(\vec{\phi}^*,\vec{\theta}^*,\mu^*) \leftarrow (\vec{\phi},\vec{\theta},\mu)$
\end{algorithmic}
\label{alg: Levenberg-Marquardt}
\end{algorithm}

Suggested values for the tuning parameters are $\epsilon_1 = \epsilon_2 = \epsilon_3 = 10^{-15}, \tau = 10^{-3}$ and $k_{max} = 100$.
\subsubsection{Partial Derivatives}\label{sec:partial der}
The Jacobian matrix $J = (J_{1}, J_{2}, \dots, J_N)^T$ requires the partial derivatives, which are
\begin{align}
J_t = (J_{t, \phi_1}, \dots, J_{t,\phi_p}, J_{t,\theta_1}, \dots,
J_{t,\theta_q}, J_{t,\mu})^T\ .
\end{align}
Here the last term is present only when \texttt{include\_mean} is
\texttt{True}.
The iteration relations for $J$ are
\begin{align}
J_{t, \phi_i} &= \frac{\partial F_t(\phi,\theta)}{\partial \phi_i} =
-\frac{\partial Z_t}{\partial \phi_i} = X_{t-i}-\mu + \sum_{j=1}^q
\theta_j \frac{\partial Z_{t - j}}{\partial \phi_i} = X_{t-i}-\mu - \sum_{j=1}^q
\theta_j J_{t-j,\phi_i}, \\
J_{t, \theta_i}&=\frac{\partial F_t(\phi,\theta)}{\partial \theta_i} =
-\frac{\partial Z_t}{\partial \theta_i} = Z_{t-i} + \sum_{j =1}^q
\theta_j \frac{\partial Z_{t - j}}{\partial \theta_i} = Z_{t-i} -
\sum_{j=1}^q \theta_j J_{t-j,\theta_i}, \\
J_{t, \mu} &=\frac{\partial F_t(\phi,\theta)}{\partial \mu} =
-\frac{\partial Z_t}{\partial \mu} = 1 -
\sum_{j=1}^p \phi_j - \sum_{j=1}^q \theta_j \frac{\partial
  Z_{t-j}}{\partial \mu} = 1 - \sum_{j=1}^p \phi_j - \sum_{j=1}^q
\theta_j J_{t-j,\mu}.
\end{align}
Note that the mean value $\mu$ is considered separately in the above
formulations. When \texttt{include\_mean} is set to \texttt{False}, $\mu$ will be simply
set to 0. Otherwise, $\mu$ will also be estimated together with
$\vec{\phi}$ and $\vec{\theta}$. The initial conditions for the above
equations are
\begin{equation}
J_{t,\phi_i} = J_{t,\theta_j} = J_{t,\mu} = 0 \quad \mbox{for }
t \leq p, \mbox{ and } i=1,\dots,p; j = 1, \dots, q\ ,
\end{equation}
because we have fixed $Z_t$ for $t\leq p$ to be a constant $0$ in the initial
condition. Note that $J$ is zero not only for $t\leq
0$ but also for $t\leq p$.

\subsection{Estimates of  Other Quantities}
Finally the variance of the residuals is
\begin{equation}
\sigma^2 = \frac{1}{N-p}\sum_{t=1}^N Z_t^2\ . \label{eq:s2}
\end{equation}
The estimate for the maximized log-likelihood is
\begin{equation}
l = -\frac{N}{2}\left[1 + \log(2\pi\sigma^2)\right]\ , \label{eq:loglik-R}
\end{equation}
where $\sigma^2$ uses the value in Eq. (\ref{eq:s2}).
Actually if you put Eq. (\ref{eq:s2}) into
Eq. (\ref{eq:loglikelihood}), you will get a result slightly different
from Eq. (\ref{eq:loglik-R}). However, Eq. (\ref{eq:loglik-R}) is what
R uses for the method \texttt{``CSS''}.

The standard error for coefficient $a$, where $a=\phi_1,\dots,\phi_p,\theta_1,\dots,\theta_q,\mu$, is
\begin{equation}
\mbox{error}_a = \sqrt{(H^{-1})_{aa}}\ .
\end{equation}
The Hessian matrix is
\begin{equation}
H_{ab} = \frac{\partial^2}{\partial a \partial b}
\left(\frac{1}{2\sigma^2}\sum_{t=1}^N Z_t^2 \right) =
\frac{1}{\sigma^2}\sum_{t=1}^N
\left(J_{t,a}J_{t,b} -
  Z_t K_{t,ab} \right) = \frac{1}{\sigma^2}\left(
  A - \sum_{t=1}^N Z_t K_{t,ab}\right)\ ,
\end{equation}
where $a,b=\phi_1,\dots,\phi_p,\theta_1,\dots,\theta_q,\mu$,
$\sigma^2$ is given by Eq. (\ref{eq:s2}), $A=J^TJ$ and
\begin{equation}
K_{t,ab}=\frac{\partial J_{t,a}}{\partial b} = - \frac{\partial^2
  Z_t}{\partial a \partial b} \ .
\end{equation}
And
\begin{eqnarray}
K_{t,\phi_i\phi_j} &=& -\sum_{k=1}^q \theta_k K_{t-k,\phi_i\phi_j} = 0
\nonumber\\
K_{t,\phi_i\theta_j} &=& -J_{t-j,\phi_i} - \sum_{k=1}^q\theta_k
K_{t-k,\phi_i\theta_j} \nonumber\\
K_{t,\phi_i\mu} &=& -1 -\sum_{k=1}^q\theta_k
K_{t-k,\phi_i\mu}\nonumber \\
K_{t,\theta_i\phi_j} &=& -J_{t-i,\phi_j}-\sum_{k=1}^q\theta_k
K_{t-k,\theta_i\phi_j} \nonumber\\
K_{t,\theta_i\theta_j} &=& -J_{t-i,\theta_j} - J_{t-j,\theta_i} -
\sum_{k=1}^q \theta_k K_{t-k,\theta_i\theta_j} \nonumber \\
K_{t,\theta_i\mu} &=& -J_{t-i,\mu} - \sum_{k=1}^q\theta_k
K_{t-k,\theta_i\mu} \nonumber\\
K_{t,\mu\phi_j} &=& -1-\sum_{k=1}^q \theta_k K_{t-k,\mu\phi_j}
\nonumber \\
K_{t,\mu\theta_j} &=& -J_{t-j,\mu} -\sum_{k=1}^q \theta_k
K_{t-k,\mu\theta_j} \nonumber \\
K_{t,\mu\mu} &=& - \sum_{k=1}^q \theta_k K_{t-k,\mu\mu} = 0\ , \label{eq:K}
\end{eqnarray}
where the initial conditions are
\begin{equation}
K_{t,ab} = 0\quad \mbox{for } t\leq p, \mbox{ and }
a,b=\phi_1,\dots,\phi_p,\theta_1,\dots,\theta_q,\mu\ . \label{eq:K_init}
\end{equation}
According to Eqs. (\ref{eq:K},\ref{eq:K_init}), $K_{t,\phi_i\phi_j}$ and
$K_{t,\mu\mu}$ are always $0$.

The iteration equations Eq. (\ref{eq:K}) are quite complicated. Maybe
an easier way to compute $K$ is to numerically differentiate the
function
\begin{equation}
f(\vec{\phi},\vec{\theta},\mu) = \frac{1}{2\sigma^2}\sum_{t=1}^NZ_t^2\ ,
\end{equation}
where $Z_t$ values are computed using given coefficients of
$\vec{\phi}$, $\vec{\theta}$ and $\mu$.
For example,
\begin{eqnarray}
H_{ab} &=&
\frac{1}{\Delta}\left[\frac{f(a+\Delta/2,b+\Delta/2)-f(a-\Delta/2,b+\Delta/2)}{\Delta}
- \right. \nonumber\\
& & \left. \frac{f(a+\Delta/2,b-\Delta/2) - f(a-\Delta/2,b-\Delta/2)}{\Delta}\right] \mbox{ for } a\neq b \
, \\
H_{aa} &=& \frac{f(a+\Delta) - 2f(a) + f(a-\Delta)}{\Delta^2}
\end{eqnarray}
where $\Delta$ is a small number and the coefficients other than $a$
and $b$ are ignored from the arguments of $f(\cdot)$ for simplicity.

\subsubsection{Implementation}
The key of LMA is to compute Jacobian matrix $J$ in each iteration. In order to compute $J$, we need to compute $Z_t$ for $t=1,\dots,N$ in a recursive manner. The difficulty here is how to leverage the parallel capability of MPP databases. By carefully distributing the dataset to the segment nodes - distribute time servies data by the time range,  the recursive computation can be done in parallel via approximation. It's also necessary here to utilize the window function for the recursive computation.

\subsection{Exact Maximum Likelihood Calculation}

%\subsection{Unconditional Least Squares}

\section{Solving for the optimal model}\label{sec:model_opt}

\subsection{Auto-Correlation Function}
Note that there several common definitions of the auto-correlation function.  This implementation uses the normalized form.

The auto-correlation function  is a cross-correlation of a function (or time-series) with itself, and is typically used to find periodic behavior in a time-series.  For a real, discrete time series, the auto-correlation  $R(k)$ for lag $k$ of a time series $X$ with $N$ data points  is
\begin{align}\label{equ:auto corr}
R_X(k) =\sum_{t=k+1}^N  \frac{(x_t -\mu) (x_{t-k} - \mu)}{N \sigma^2} .
\end{align}
where $\sigma^2$ and $\mu$ are the variance and mean of the time series respectively.
For this implementation, the range of desired $k$ values will be small ($\approx 10\log(N)$ ), and the auto-correlation function for the range can be computed naively with equation \ref{equ:auto corr}.

\subsection{Partial Auto-Correlation Function}
%Author's note: this material is taken from http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/tutorials/sfehtmlnode59.html
%Some definitions are also from http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/tutorials/sfehtmlnode48.html
The partial auto-correlation function is a conceptually simple extension to the auto-correlation function, but greatly increases the complexity of the calculations.  The partial auto-correlation is the correlation for lag $k$ after all correlations from lags $<k$ have been accounted for.

Let
\begin{align}
R_{(k)} \equiv  \left[ R_X(1), R_X(2), \dots, R_X(k)\right]^T
\end{align}
and let
\begin{align}
P_k = \left[ \begin{matrix}
1 &  R_X(1) & \dots & R_X(k-1) \\
R_X(1) & 1 & \dots & R_X(k-2) \\
\vdots & \vdots & \ddots & \vdots \\
R_X(k-1) &R_X(k-2) & \dots & 1 \end{matrix} \right]
\end{align}

Then the partial auto-correlation function $\Phi(k)$ for lag $k$ is
\begin{align}
\Phi(k) = \frac{ \det P^*_k}{\det P_k}
\end{align}
where $P^*_k$ is equal to the matrix $P_k$, except the $k$th column is replaced with $R_{(k)}$.

\subsection{Automatic Model Selection}

\section{Seasonal Models}