## Jacobian involving Quaternions

I have this function I need to build a Jacobian matrix for:

$
F = R\left(\bar{q}\right) . p
$

where p is a position vectors (x,y,z) and R(q) is the function converting a quaternion to a rotation matrix.
Note that in the function F, the rotation matrix is obtained from the conjugate of q, i.e. (q0, -q1, -q2, -q3).

$
R(q) = \left(
\begin{array}{ccc}
{q0}^2+{q1}^2-{q2}^2-{q3}^2 & 2 ({q1} {q2}-{q0} {q3}) & 2 ({q0} {q2}+{q1} {q3}) \\
2 ({q1} {q2}+{q0} {q3}) & {q0}^2-{q1}^2+{q2}^2-{q3}^2 & 2 (-{q0} {q1}+{q2} {q3}) \\
2 (-{q0} {q2}+{q1} {q3}) & 2 ({q0} {q1}+{q2} {q3}) & {q0}^2-{q1}^2-{q2}^2+{q3}^2
\end{array}
\right)
$

The jacobian matrix I obtain for dH/dq is as follows: err.. can't post it here, the latex parser doesn't like it. Let's just say it's

$
\frac{\partial F}{\partial q}=\frac{\partial R(\text{q0},-\text{q1},-\text{q3},-\text{q3}) . p}{\partial (\text{q0},\text{q1},\text{q3},\text{q3})}
$

However, I saw someone obtaining this jacobian by separating dF/dq into
$
\frac{\partial F}{\partial q}=\frac{\partial R\left(\bar{q}\right) . p}{\partial \bar{q}}.\frac{\partial \bar{q}}{\partial q}=\frac{\partial R(\text{q0},\text{q1},\text{q3},\text{q3}) . p}{\partial (\text{q0},\text{q1},\text{q3},\text{q3})}.\frac{\ partial \bar{q}}{\partial q}
$

where
$
\frac{\partial \bar{q}}{\partial q}=\left(
\begin{array}{cccc}
1 & 0 & 0 & 0 \\
0 & -1 & 0 & 0 \\
0 & 0 & -1 & 0 \\
0 & 0 & 0 & -1
\end{array}
\right)
$

The resulting jacobian is not the same as mine... it seems that
$
\frac{\partial F}{\partial q}=\frac{\partial R(\text{q0},-\text{q1},-\text{q3},-\text{q3}) . p}{\partial (\text{q0},\text{q1},\text{q3},\text{q3})}\neq \frac{\partial R(\text{q0},\text{q1},\text{q3},\text{q3}) . p}{\partial (\text{q0},\text{q1},\text{q3},\text{q3})}.\frac{\ partial \bar{q}}{\partial q}
$

so which is right? i'm quite confused now. Either Mathematica is wrong, or that other guy has put some screwy math in his source code.

thanks,

Lwi