%
%  2004/Lecture notes/Lecture7/lecture7.tex
%
\documentclass[pdf,mmoll,slideColor%,colorBG
]{prosper}
\RequirePackage{color}

\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb,natbib}
%\usepackage[T1,noTS1]{mathtime}
\SetMathAlphabet{\mathbf}{normal}{\encodingdefault}{ptm}{b}{n}
\usepackage{subfigure}
\usepackage{rotating}
\usepackage{graphicx}
%\usepackage[dvips]{color}
\usepackage{epsfig}
\graphicspath{{../figs/}}

\newcommand{\ee}{\end{equation}} 
\newcommand{\be}{\begin{equation}} 
\newcommand{\ec}{\end{center}} 
\newcommand{\bc}{\begin{center}} 
\newcommand{\eea}{\end{eqnarray}} 
\newcommand{\bea}{\begin{eqnarray}} 
%\newcommand{\sumprime}{\mathop{{\sum}'}} 
\newcommand{\bd}{\begin{description}} 
\newcommand{\ed}{\end{description}} 
\newcommand{\bi}{\begin{itemize}} 
\newcommand{\ei}{\end{itemize}} 
\newcommand{\pa}{\partial} 
\newcommand{\hor}{\left(\frac{h}{R_0}\right)} 

\newcommand{\code}[0]{\ttfamily}
\newcommand{\co}[0]{\hbox{c}}
\newcommand{\si}[0]{\hbox{s}}
\newcommand{\pprime}[0]{\prime\prime}
\newcommand{\ppprime}[0]{\prime\prime\prime}
\newcommand{\conv}{\operatorname{conv}}
\newcommand{\pos}{\operatorname{pos}}
\newcommand{\lin}{\operatorname{lin}}
\newcommand{\vertop}{\operatorname{vert}}
\newcommand{\spanop}{\operatorname{span}}
\newcommand{\supp}{\operatorname{supp}}
\newcommand{\intoper}{\operatorname{int}}
\newcommand{\half}{\operatorname{half}}
\newcommand{\rad}{\operatorname{rad}}
\newcommand{\diam}{\operatorname{diam}}
\newcommand{\push}{\operatorname{push}}
\newcommand{\sqz}{\operatorname{sqz}}
\newcommand{\sgn}{\operatorname{sgn}}
%\renewcommand{\Re}{\Bbb{R}}
\renewcommand{\Re}{\mathbf{R}}
\newcommand{\vv}[1]{\mathbf{#1}}
\newcommand{\uu}[1]{\hat {\vv #1}}
\newcommand{\defterm}[1]{\textbf{#1}}
\newcommand{\rot}{\operatorname{rot}}

\newcommand{\btimes}{\times}
\newcommand{\bomega}{\omega}

 
\newcommand{\bis}{\begin{itemstep}} 
\newcommand{\eis}{\end{itemstep}} 

\makeatletter
\define@key{PDF}{Movie}{\pdf@addtoks{#1}{Movie}}
\define@key{PDF}{Activation}{\pdf@addtoks{#1}{Activation}}
\newcommand{\moviewithpreview}[3]{% args: width, preview, movie
   \pdfmark[{\includegraphics[width=#1]{#2}}]{%
   pdfmark=/ANN,Subtype=/Movie,Movie=<< /F (#3) >>,%
   Activation=<< /ShowControls true /Mode /Repeat >>}}
\newcommand{\movie}[3]{% args:width, height, movie
   \pdfmark[{\hbox to #1 {\vbox to #2 { }}}]{%
   pdfmark=/ANN,Subtype=/Movie,Movie=<< /F (#3) /Poster true >>,%
   Activation=<< /ShowControls true /Mode /Repeat >>}}
\makeatother

\title{7.  Quaternions}
\subtitle{ Mechanics of Manipulation}
\author{Matt Mason}
\institution{Carnegie Mellon}
\email{matt.mason@cs.cmu.edu \\
http://www.cs.cmu.edu/\symbol{"7E}mason}
\slideCaption{Lecture 7. \hfill Mechanics of Manipulation }

\begin{document}

\maketitle

%=======================================================================
%
% SLIDE 2
%

\begin{slide}[R]{Lecture 7.  Quaternions.}
\vspace{-0.5in}
\includegraphics[height=3.0in]{TOC7}
\end{slide}

%=======================================================================
%
% SLIDE 3
%
%

\begin{slide}[R]{Outline.}

\begin{itemize}
\item What is a quaternion
\item Representing rotation
\item Geometric view
\item Transformations to other representations
\item Topological and metric properties
\end{itemize}

\end{slide}


%=======================================================================
%
% SLIDE 4
%
%
\overlays{4}{
\begin{slide}[R]{Why can't we invert vectors in $\mathbf{R}^3$?}
\begin{itemize}
\item[]We can invert reals.  $x \times \frac{1}{x} = 1$.
\item[]We can invert elements of $\mathbf{R}^2$ using complex numbers.
$z \times z^* = 1$, where $^*$ is complex conjugate.
\item[]Can we invert $\vv v \in \mathbf{R}^3$?  \fromSlide{2}{No.}
\fromSlide{3}{\item[]How about $\vv v \in \mathbf{R}^4$?}\fromSlide{4}{Yes!
\item[]Hamilton's quaternions are to $\mathbf{R}^3$ what complex numbers
are to $\mathbf{R}$.}
\end{itemize}

\end{slide}}

%=======================================================================
%
% SLIDE 5
%
%
\overlays{2}{
\begin{slide}[R]{Complex numbers versus quaternions}
\begin{itemize}
\item[]To define complex numbers:
\begin{itemize}
\item[]Basis elements $1$ and $i$;
\item[]Vector space over reals:  elements have the form $x+i y$;
\item[]One more axiom required:  $i^2$ = -1.
\end{itemize}
\fromSlide{2}{%
\item[]To define quaternions:
\begin{itemize}
\item[]Basis elements $1$, $i$, $j$, $k$;
\item[]Vector space over reals:  elements have the form $q_0 + q_1 i + q_2 j + q_3 k$;
\item[]Six more axioms:
\begin{align*}
  i^2 &= j^2 = k^2 = -1 \\
  ij &= k \\
  jk &= i \\
  ki &= j
\end{align*}
\end{itemize}}
\end{itemize}
\end{slide}}

%========================================================================
%
% SLIDE 6
%
%

\begin{slide}[R]{Quaternion notation}

We can write a quaternion several ways:
\begin{align*}
  q &= q_0 + q_1 i + q_2 j + q_3 k\\
  q &= (q_0, q_1, q_2, q_3)\\
  q &= q_0 + \vv q
\end{align*}
where $q_0$ is the \emph{scalar part} and $\vv q$ is the \emph{vector part}
\end{slide}

%========================================================================
%
% SLIDE 7
%
%
\overlays{3}{
\begin{slide}[R]{Quaternion product}
We can write a quaternion product several ways:
\begin{align*}
  pq &= (p_0 + p_1 i + p_2 j + p_3 k)  (q_0 + q_1 i + q_2 j + q_3 k)\\
        &= (p_0 q_0 - p_1 q_1 - p_2 q_2 - p_3 q_3) + \ldots i + \ldots j + \ldots k \\
  pq &= (p_0 + \vv p) (q_0 + \vv q) \\
        &= (p_0 q_0 + p_0 \vv q + q_0 \vv p + \vv p \vv q)
\end{align*}
So what is $\vv p \vv q$?  Cross product?  Dot product?\fromSlide{2}{Both!  Cross product minus dot product!}
\fromSlide{3}{%
\[
    pq = (p_0 q_0 - \vv p \cdot \vv q + p_0 \vv q + q_0 \vv p + \vv p \times \vv q) 
\]}
\end{slide}}

%=======================================================================
%
% SLIDE 8
%
%

\begin{slide}[R]{Conjugate, length}
\begin{itemize}
\item[]Quaternion \defterm{conjugate}:
\[  q^* = q_0 - q_1 i - q_2 j - q_3 k \]
\item[]Note that
\begin{align*}
   q q^* &= (q_0 + \vv q)(q_0 - \vv q) \\
             &= q_0^2 + q_0 \vv q - q_0 \vv q - \vv q \vv q  \\
             &= q_0^2 + \vv q \cdot \vv q - \vv q \times \vv q  \\
             &= q_0^2 + q_1^2 + q_2^2 + q_3^2
\end{align*}
\item[]Quaternion \defterm{length}:
\[  |q| = \sqrt{q q^*} = \sqrt{q_0^2 + q_1^2 + q_2^2 + q_3^2}  \]
\end{itemize}
\end{slide}

%=======================================================================
%
% SLIDE 9
%
%

\begin{slide}[R]{Quaternion inverse}
\begin{itemize}
\item[]Note that every quaternion other than the additive identity $0$ has an inverse:
\[ q^{-1} = \frac{q^*}{|q|^2} \]
\end{itemize}
That means quaternions are a linear algebra and a field.  Hamilton's dream.  Quaternions are the only extension of  complex numbers that is both a linear algebra and a field.  If 1D numbers are the reals, and 2D numbers are the complex numbers, then 4D numbers are quaternions, and that's all there is.  (Frobenius?)
\end{slide}

%=======================================================================
%
% SLIDE 10
%
%

\begin{slide}[R]{Rotation using unit quaternions}

\begin{itemize}
\item[] Let $q$ be a unit quaternion, i.e. $|q|=1$.
\begin{itemize}
\item[]  It can be expressed as
\[  q = \cos \frac{\theta}{2} + \sin \frac{\theta}{2} \uu n \]
\end{itemize}
\item[] Let $x = 0 + \vv x$ be a ``pure vector''.
\item[] Let $x^\prime = qxq^*$.
\item[] Then $x^\prime$ is the pure vector $\rot(\theta,\uu n) \vv x$!!!
\end{itemize}
\end{slide}

%=======================================================================
%
% SLIDE11
%
%

\begin{slide}[R]{Proof that unit quaternions work}

\begin{itemize}
\item[] Expand the product $qxq^*$;
\item[] Apply half angle formulas;
\item[] Simplify;
\item[] to obtain Rodrigues's formula.
\end{itemize}
\end{slide}

%=======================================================================
%
% SLIDE 12
%
%

\begin{slide}[R]{Why $\theta / 2$?  Why $qxq^*$ instead of $qx$?}

Two puzzling things.  In analogy with complex numbers, why not use
\begin{align*} 
       p &= \cos\theta + \uu n \sin\theta \\
       \vv x^\prime &= p\vv x
\end{align*}
To explore that idea, define a map $L_p(q) = pq$ with $p$ a unit pure vector.
Note that $L_p(q)$ can be written:
\begin{equation*}
        L_p(q) = \left(\begin{array}{cccc}
                         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{array}\right)
                 \left(\begin{array}{c}
                        q_0 \\ q_1 \\ q_2 \\ q_3 \\
                 \end{array}\right)
\end{equation*}
Note that the matrix above is orthonormal.  $L_p$ is a rotation of
Euclidean 4 space!
(Without even using the fact that $p$ is a pure vector.)
\end{slide}

%=======================================================================
%
% SLIDE 13
%
%

\begin{slide}[R]{Geometrical explanation}
\begin{trivlist}
\item  
  Although $L_p(q)$ rotates the 4D space of quaternions, it is
  \emph{not} a rotation of the 3D subspace of pure vectors.  Some of
  the 3D subspace leaks into the fourth dimension.

\item
Consider an example using $p = i$.  Is it a rotation about $i$ of $\pi/2$?
\end{trivlist}
\begin{figure}[h!]
\centerline{
\includegraphics[height = 4cm]{quatrot.eps}}
\end{figure}
\end{slide}

%=======================================================================
%
% SLIDE 15
%
%

\begin{slide}[R]{What do we do with a representation?}
\begin{trivlist}
\item
Rotate a point:  $qxq^*$.

\item
Compose two rotations:
\[
  q(p\vv x p^*)q^* = (qp)\vv x(qp)^*
\]

\item
Convert to other representations:
\begin{itemize}
    \item[]  From axis-angle to quaternion:
      \[  q = \cos \frac{\theta}{2} + \sin \frac{\theta}{2} \uu n \]
    \item[]  From quaternion to axis-angle:
      \begin{align*}
        \theta &= 2 \tan^{-1}(|{\vv q}|, q_0) \\
        \uu n &= {\vv q}/|{\vv q}|
      \end{align*}
      assuming $\theta$ is nonzero. 
    \end{itemize}
\end{trivlist}
\end{slide}

%=======================================================================
%
% SLIDE 16
%
%

\begin{slide}[R]{From quaternion to rotation matrix}
Just expand the product
\[  qxq^* = 
 \left( \begin{array}{ccc}
                                q_0^2 + q_1^2 - q_2^2 - q_3^2 
                                        & 2(q_1 q_2 - q_0 q_3) 
                                        & 2(q_1 q_3 + q_0 q_2) \\
                                2(q_1 q_2 + q_0 q_3) 
                                        & q_0^2 - q_1^2 + q_2^2 - q_3^2 
                                        & 2 (q_2 q_3 - q_0 q_1) \\
                                2(q_1 q_3 - q_0 q_2) 
                                        & 2 (q_2 q_3 + q_0 q_1)
                                        & q_0^2 - q_1^2 - q_2^2 + q_3^2 \\
                   \end{array}\right) \vv x
\]
\end{slide}

%=======================================================================
%
% SLIDE 17
%
%

\begin{slide}[R]{From rotation matrix to quaternion}
\begin{trivlist}
\item[] Given $R = (r_{ij})$, solve expression on previous page for
quaternion elements $q_i$ 
\item[] Linear combinations of diagonal elements seem to solve the problem:
\begin{align*}
        q_0^2 =& \frac{1}{4} (1 + r_{11} + r_{22} + r_{33}) \\
        q_1^2 =& \frac{1}{4} (1 + r_{11} - r_{22} - r_{33}) \\
        q_2^2 =& \frac{1}{4} (1 - r_{11} + r_{22} - r_{33}) \\
        q_3^2 =& \frac{1}{4} (1 - r_{11} - r_{22} + r_{33})   
\end{align*}

so take four square roots and you're done?  You have to figure the
signs out.  There is a better way $\ldots$
\end{trivlist}
\end{slide}

%=======================================================================
%
% SLIDE 18
%
%

\begin{slide}[R]{Look at the off-diagonal elements}
\begin{itemize}
\item[]
\begin{align*}
        q_0 q_1 =& \frac{1}{4}(r_{32} - r_{23}) \\
        q_0 q_2 =& \frac{1}{4}(r_{13} - r_{31}) \\
        q_0 q_3 =& \frac{1}{4}(r_{21} - r_{12}) \\
        q_1 q_2 =& \frac{1}{4}(r_{12} + r_{21}) \\
        q_1 q_3 =& \frac{1}{4}(r_{13} + r_{31}) \\
        q_2 q_3 =& \frac{1}{4}(r_{23} + r_{32})
\end{align*}

\item[]  Given any one $q_i$, could solve the above for the other three.
\end{itemize}
\end{slide}

%=======================================================================
%
% SLIDE 19
%
%

\begin{slide}[R]{The procedure}
\begin{enumerate}
\item Use first four equations to find the largest $q_i^2$.   Take its square root.
\item Use the last six equations (well, three of them anyway) to solve for the other $q_i$.
\end{enumerate}

\begin{itemize}
\item[]  That way, only have to worry about getting one sign right.
\item[]  Actually $q$ and $-q$ represent the same rotation, so no worries about signs.
\item[]  Taking the largest square root avoids division by small numbers.
\end{itemize}
\end{slide}

%=======================================================================
%
% SLIDE 20
%
%

\begin{slide}[R]{Properties of unit quaternions}
\begin{itemize}

\item[]Unit quaternions live on the unit sphere in $\mathbf{R}^4$.
\item[] Quaternions $q$ and $-q$ represent the same rotation.
\item[]Inverse of rotation $q$ is the conjugate $q^*$.
\item[]Null rotation, the identity, is the quaternion $1$.
\end{itemize}
\end{slide}

%=======================================================================
%
% SLIDE 21
%
%

\begin{slide}[R]{Metrics and topologies}
\begin{trivlist}

\item[]Quaternions have the right metric. Consider unit quaternion
\[  q = \cos \frac{\theta}{2} + \sin \frac{\theta}{2} \uu n \]
Shortest path on the unit sphere joining $\pm q$ with $1$ has length
$\theta/2$. 
\item[]  What is the shortest distance on the sphere from $\pm p$ to
$\pm q$?  The same as the distance from $\pm pq^*$ to $1$.
I.e. $\alpha / 2$, where $\alpha$ is the rotation angle required from
$p$ to $q$. 
\item[]The right metric matters.  Uniform distribution on the
three-sphere maps to uniform distribution on $SO(3)$.  Hence problem~2
on the problem set. 
\item[]What is the topology of $SO(3)$?  Since unit quaternion
representation has the right metric, it also has the right topology.
What do we call the topology of a three-sphere with antipodes
identified? 
\end{trivlist}
\end{slide}

%=======================================================================
%
% SLIDE 22
%

\begin{slide}[R]{Next:  Representing displacements.}
\vspace{-0.5in}
\includegraphics[height=3.0in]{TOC8}
\end{slide}
\end{document}



\end{document}
