I am working an algorithm which is supposed to align a pair of images. The motion model, which describes the pose $p$ of an image (with respect to the second) in 3D space, is purely rotational. 

**Theory to my question:**

According to the Rodrigues' formula for rotation matrices, the matrix exponential $R(p)=e^{\hat{p}}$ for a vector $p=(p\_x,p\_y,p\_z)^T\in\mathbb{R}^3$ is given by
$$ 
\begin{align}
 R(p)&=e^{\hat{p}}=I + \frac{\hat{p}}{||p||} \sin(||p||) + \frac{\hat{p}^2}{||p||^2} (1-\cos{\theta}) 
\end{align}
$$
where $||p||=\sqrt{p\_x^2+p\_y^2+p\_z^2}$ and
$$
\begin{align}
\hat{p} &= 
 \begin{bmatrix}
 0 & -p\_z & p\_y \\
 p\_z & 0 & -p\_x \\
 -p\_y & p\_x & 0
 \end{bmatrix}
\end{align}.
$$

Summing up the elements of $R(p)$ yields
$$
\begin{align}
 R(p) = 
 \begin{bmatrix}
 1-(p\_y^2+p\_z^2)s & -p\_z r+p\_xp\_y s & p\_y r+p\_xp\_z s \\
 p\_z r+p\_xp\_y s & 1-(p\_x^2+p\_z^2)s & -p\_x r+p\_yp\_z s \\
 -p\_z r+p\_xp\_z s & p\_x r+p\_yp\_z s & 1-(p\_x^2+p\_y^2)s
 \end{bmatrix}
\end{align}
$$
where $r=\frac{\sin(||p||)}{||p||}$ and $s=\frac{(1-\cos(||p||))}{||p||^2}$ were introduced for clarity.

Now, let $R^s(p)\in\mathbb{R}^{9\times 1}$ be the stacked version of $R(p)$, i.e. all columns are stacked into one column and let $R^s\_i(p)$ denote the $i$-th element of $R^s(p)$ where $1\leq i \leq 9$.

The Jacobian matrix $J\_{R}\in\mathbb{R}^{9\times 3}$ with respect to vector $p$ is then given by
$$
\begin{align}
 J\_{R}&=\frac{\partial R^s}{\partial p} \\\\
 &=
 \begin{bmatrix}
 \frac{\partial R^s\_1}{\partial p\_x} & \frac{\partial R^s\_1}{\partial p\_y} & 
 \frac{\partial R^s\_1}{\partial p\_z} \\\\
 \vdots & \vdots & \vdots \\\\
 \frac{\partial R^s\_9}{\partial p\_x} & \frac{\partial R^s\_9}{\partial p\_y} & 
 \frac{\partial R^s\_9}{\partial p\_z}
 \end{bmatrix}
\end{align}
$$

The first element of $J\_R$ can then be expressed as
$$
\begin{align}
 \frac{\partial R^s\_1}{\partial p\_x} 
 &=\frac{\partial}{\partial p\_x} 
 \Bigl(1-(p\_y^2+p\_z^2)\frac{(1-\cos(||p||))}{||p||^2}\Bigr) \\\\
 &=-\Bigl[(p\_y^2+p\_z^2)\frac{\sin(||p||) p\_x ||p|| - (1-\cos(||p||) 2 p\_x)}{||p||^4}\Bigr]
\end{align}
$$
the second one as
$$
\begin{align}
 \frac{\partial R^s\_1}{\partial p\_y} 
 &=\frac{\partial}{\partial p\_y} 
 \Bigl(1-(p\_y^2+p\_z^2)\frac{(1-\cos(||p||))}{||p||^2}\Bigr) \\\\
 &=-\Bigl[2p\_y\frac{1-\cos(||p||)}{||p||^2}+(p\_y^2p\_z^2)\frac{\sin(||p||) p\_y ||p|| - (1-\cos(||p||) 2 p\_y)}{||p||^4}\Bigr]
\end{align}
$$
and the third element of the first rowas
$$
\begin{align}
 \frac{\partial R^s\_1}{\partial p\_z} 
 &=\frac{\partial}{\partial p\_z} 
 \Bigl(1-(p\_y^2+p\_z^2)\frac{(1-\cos(||p||))}{||p||^2}\Bigr) \\\\
 &=-\Bigl[2p\_z\frac{1-\cos(||p||)}{||p||^2}+(p\_y^2p\_z^2)\frac{\sin(||p||) p\_z ||p|| - (1-\cos(||p||) 2 p\_z)}{||p||^4}\Bigr]
\end{align}
$$

Already a **big thanks** for reading this far ;-)

**My question:** Is the derivation of these first 3 elements of $J\_R$ mathematically correct? If I did something wrong, can you spot the error(s) and possibly help me correct it(/them)?

Once I have got everything straight, I will post the complete Jacobian matrix as the answer to this question. So anyone confronted with the same problem can double-check his own results ...

**The reason I need this Jacobian:**

The pose $p$ of an image in the application mentioned above can be described by an unit rotation axis and an angle (in radian). Alternatively, the unit axis can be scaled according to the rotation angle, which is what I am using. To do the exponential mapping from $\mathbb{R}^3$ to SO(3) I use the Rodrigues' formula (which is the standard method, I think).

The algorithm "slides" down the gradient of an error function, which is governed by the pose $p$, i.e. 
$$E(p)=\sum\_i(I\_1(x'(x\_i;p))-I\_0(x\_i))^2$$
where $x'(\cdot)$ is the function that maps pixels from one image space to the other image space with respect to the current pose $p$.

In order to compute the gradient of $E(p)$ I will have to compute the gradient of the rotation matrices, which leads to my actual problem.