# Quaternion vector rotation

Using the hamilton product, we can rotate a vector using a quaternion. Suppose vector is $$v = {0, v_1, v_2, v_3}$$ and after rotation it is $$v'$$ and $$Q = {q_0, q_1, q_2, q_3}.$$ So function to rotate by quaternion is $$f(rot) = QvQ'$$. Now what I want is to rotate the vector back to $$v'$$ from $$v$$. I have tried $$Q'v'Q$$ where $$Q'$$ is the conjugate but it does not seem to work. How do I calculate the original vector after it was rotated using $$Q$$?

• $Q$ should be a unit quaternion, such that $Q'Q=QQ'=1$. Jan 27, 2020 at 18:40
• @Berci maybe the OP is asking how to do this with arbitrary $Q$? Jan 27, 2020 at 18:41
• Oh well it works perfectly... Thanks anyway... Jan 27, 2020 at 23:27

## 1 Answer

You have adopted the scalar-vector notation of quaternions with $$Q = \pmatrix{ s \\ \boldsymbol{v} } \tag{1}$$

and the inverse

$$Q^{-1} = \frac{Q^\star}{\sqrt{s^2 + \| \boldsymbol{v} \|^2}} = \frac{1}{\sqrt{s^2 + \boldsymbol{v}\cdot\boldsymbol{v} }} \pmatrix{ s \\ -\boldsymbol{v} } \tag{2}$$

Where $$\boldsymbol{v} = \pmatrix{v_x & v_y & v_z}$$ is the vector part of the quaternion and $$s$$ the scalar part.

All rotation operations need to be done with unit quaternions by setting $$Q \leftarrow \frac{1}{\mathrm{mag}(Q)} Q$$, which yields the identity $$s^2 + \boldsymbol{v} \cdot \boldsymbol{v} = 1$$. This is going to be used to simplify the expressions below.

Now a point P with coordinates $$\boldsymbol{p} = \pmatrix{p_x & p_y & p_z}$$ is rotated into $$\boldsymbol{p}'$$ with

$$\pmatrix{ 0 \\ \boldsymbol{p}' } = \pmatrix{s \\ \boldsymbol{v} } \otimes \pmatrix{ 0 \\ \boldsymbol{p} } \otimes \pmatrix{s \\ \boldsymbol{v}}^{-1} \tag{3}$$

where $$\otimes$$ is the quaternion multiplication.

The above is expanded out to $$\boldsymbol{p}' = \boldsymbol{p} + 2 s (\boldsymbol{v} \times \boldsymbol{p}) + 2 ( \boldsymbol{v} \times ( \boldsymbol{v} \times \boldsymbol{p})) \tag{4}$$

considering the multiplication rule

$$\pmatrix{s_1 \\ \boldsymbol{v}_1 } \otimes \pmatrix{ s_2 \\ \boldsymbol{v}_2 } = \pmatrix{ s_1 s_2 - \boldsymbol{v}_1 \cdot \boldsymbol{v}_2 \\ s_1 \boldsymbol{v}_2 + s_2 \boldsymbol{v}_1 + \boldsymbol{v}_1 \times \boldsymbol{v}_2 } \tag{5}$$

Now the inverse operation is done with

$$\pmatrix{ 0 \\ \boldsymbol{p} } = \pmatrix{s \\ \boldsymbol{v} }^{-1} \otimes \pmatrix{ 0 \\ \boldsymbol{p}' } \otimes \pmatrix{s \\ \boldsymbol{v}} \tag{6}$$

which works out to

$$\boldsymbol{p} = \boldsymbol{p}' - 2 s (\boldsymbol{v} \times \boldsymbol{p}') + 2 ( \boldsymbol{v} \times ( \boldsymbol{v} \times \boldsymbol{p}')) \tag{7}$$

Note that the reverse rotation of $$\pmatrix{s \\ \boldsymbol{v}}$$ is $$\pmatrix{s \\ -\boldsymbol{v}}$$ given unit quaternion. This means that you go directly from (4) to (7) by changing the sign of the vector $$\boldsymbol{v}$$.

The last part of the puzzle is to show that (4) corresponds to the Rodrigues formula for rotation. Note that for a unit quaternion that is a rotation $$Q = \pmatrix{s \\ \boldsymbol{v}} = \pmatrix{ \cos \tfrac{\theta}{2} \\ \boldsymbol{\hat{z}} \sin \tfrac{\theta}{2} }$$ where $$\boldsymbol{\hat{z}}$$ is the rotation axis, and $$\theta$$ the rotation angle.

Use the scalar and vector part in (4) to get

\begin{aligned}\boldsymbol{p}' & =\boldsymbol{p}+2s\left(\boldsymbol{v}\times\boldsymbol{p}\right)+2\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}\right)\\ & =\boldsymbol{p}+2\cos\tfrac{\theta}{2}\sin\tfrac{\theta}{2}\left(\boldsymbol{\hat{z}}\times\boldsymbol{p}\right)+2\sin\tfrac{\theta}{2}\sin\tfrac{\theta}{2}\boldsymbol{\hat{z}}\times\left(\boldsymbol{\hat{z}}\times\boldsymbol{p}\right)\\ & =\boldsymbol{p}+\sin\theta\left(\boldsymbol{\hat{z}}\times\boldsymbol{p}\right)+\left(1-\cos\theta\right)\boldsymbol{\hat{z}}\times\left(\boldsymbol{\hat{z}}\times\boldsymbol{p}\right) \end{aligned} \;\;\checkmark

## Appendix

I will show how to go from (3) to (4)

\begin{aligned}\begin{pmatrix}0\\ \boldsymbol{p}' \end{pmatrix} & =\begin{pmatrix}s\\ \boldsymbol{v} \end{pmatrix}\otimes\begin{pmatrix}0\\ \boldsymbol{p} \end{pmatrix}\otimes\begin{pmatrix}s\\ \boldsymbol{v} \end{pmatrix}^{-1}\\ & =\begin{pmatrix}s\\ \boldsymbol{v} \end{pmatrix}\otimes\begin{pmatrix}0\\ \boldsymbol{p} \end{pmatrix}\otimes\begin{pmatrix}s\\ -\boldsymbol{v} \end{pmatrix}\\ & =\begin{pmatrix}s\\ \boldsymbol{v} \end{pmatrix}\otimes\begin{pmatrix}\boldsymbol{p}\cdot\boldsymbol{v}\\ s\boldsymbol{p}-\boldsymbol{p}\times\boldsymbol{v} \end{pmatrix}\\ & =\begin{pmatrix}s\left(\boldsymbol{p}\cdot\boldsymbol{v}\right)-\boldsymbol{v}\cdot\left(s\boldsymbol{p}-\boldsymbol{p}\times\boldsymbol{v}\right)\\ \boldsymbol{v}\left(\boldsymbol{p}\cdot\boldsymbol{v}\right)+s\left(s\boldsymbol{p}-\boldsymbol{p}\times\boldsymbol{v}\right)+\boldsymbol{v}\times\left(s\boldsymbol{p}-\boldsymbol{p}\times\boldsymbol{v}\right) \end{pmatrix}\\ & =\begin{pmatrix}s\left(\boldsymbol{p}\cdot\boldsymbol{v}\right)-s\left(\boldsymbol{v}\cdot\boldsymbol{p}\right)\\ \boldsymbol{v}\left(\boldsymbol{p}\cdot\boldsymbol{v}\right)+s^{2}\boldsymbol{p}-s\left(\boldsymbol{p}\times\boldsymbol{v}\right)+s\left(\boldsymbol{v}\times\boldsymbol{p}\right)-\boldsymbol{v}\times\left(\boldsymbol{p}\times\boldsymbol{v}\right) \end{pmatrix}\\ & =\begin{pmatrix}0\\ \boldsymbol{v}\left(\boldsymbol{p}\cdot\boldsymbol{v}\right)+\left(1-\boldsymbol{v}\cdot\boldsymbol{v}\right)\boldsymbol{p}+2s\left(\boldsymbol{v}\times\boldsymbol{p}\right)-\boldsymbol{v}\times\left(\boldsymbol{p}\times\boldsymbol{v}\right) \end{pmatrix}\\ & =\begin{pmatrix}0\\ \boldsymbol{p}+\underbrace{\boldsymbol{v}\left(\boldsymbol{v}\cdot\boldsymbol{p}\right)-\boldsymbol{p}\left(\boldsymbol{v}\cdot\boldsymbol{v}\right)}_{\text{triple product}}+2s\left(\boldsymbol{v}\times\boldsymbol{p}\right)+\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}\right) \end{pmatrix}\\ & =\begin{pmatrix}0\\ \boldsymbol{p}+\underbrace{\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}\right)}_{\text{triple product}}+2s\left(\boldsymbol{v}\times\boldsymbol{p}\right)+\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}\right) \end{pmatrix}\\ & =\begin{pmatrix}0\\ \boldsymbol{p}+2s\left(\boldsymbol{v}\times\boldsymbol{p}\right)+2\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}\right) \end{pmatrix} \end{aligned}

and from (6) to (7)

\begin{aligned}\begin{pmatrix}0\\ \boldsymbol{p} \end{pmatrix} & =\begin{pmatrix}s\\ \boldsymbol{v} \end{pmatrix}^{-1}\otimes\begin{pmatrix}0\\ \boldsymbol{p}' \end{pmatrix}\otimes\begin{pmatrix}s\\ \boldsymbol{v} \end{pmatrix}\\ & =\begin{pmatrix}s\\ -\boldsymbol{v} \end{pmatrix}\otimes\begin{pmatrix}0\\ \boldsymbol{p}' \end{pmatrix}\otimes\begin{pmatrix}s\\ \boldsymbol{v} \end{pmatrix}\\ & =\begin{pmatrix}s\\ -\boldsymbol{v} \end{pmatrix}\otimes\begin{pmatrix}-\boldsymbol{p}'\cdot\boldsymbol{v}\\ s\boldsymbol{p}'+\boldsymbol{p}'\times\boldsymbol{v} \end{pmatrix}\\ & =\begin{pmatrix}s\left(-\boldsymbol{p}'\cdot\boldsymbol{v}\right)-\left(-\boldsymbol{v}\right)\cdot\left(s\boldsymbol{p}'+\boldsymbol{p}'\times\boldsymbol{v}\right)\\ s\left(s\boldsymbol{p}'+\boldsymbol{p}'\times\boldsymbol{v}\right)+\left(-\boldsymbol{p}'\cdot\boldsymbol{v}\right)\left(-\boldsymbol{v}\right)+\left(-\boldsymbol{v}\right)\times\left(s\boldsymbol{p}'+\boldsymbol{p}'\times\boldsymbol{v}\right) \end{pmatrix}\\ & =\begin{pmatrix}-s\left(\boldsymbol{p}'\cdot\boldsymbol{v}\right)+s\left(\boldsymbol{v}\cdot\boldsymbol{p}'\right)\\ s^{2}\boldsymbol{p}'+s\left(\boldsymbol{p}'\times\boldsymbol{v}\right)+\boldsymbol{v}\left(\boldsymbol{p}'\cdot\boldsymbol{v}\right)-s\left(\boldsymbol{v}\times\boldsymbol{p}'\right)+\left(-\boldsymbol{v}\right)\times\left(\boldsymbol{p}'\times\boldsymbol{v}\right) \end{pmatrix}\\ & =\begin{pmatrix}0\\ \left(1-\boldsymbol{v}\cdot\boldsymbol{v}\right)\boldsymbol{p}'+\boldsymbol{v}\left(\boldsymbol{p}'\cdot\boldsymbol{v}\right)-2s\left(\boldsymbol{v}\times\boldsymbol{p}'\right)+\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}'\right) \end{pmatrix}\\ & =\begin{pmatrix}0\\ \boldsymbol{p}'+\underbrace{\boldsymbol{v}\left(\boldsymbol{p}'\cdot\boldsymbol{v}\right)-\boldsymbol{p}'\left(\boldsymbol{v}\cdot\boldsymbol{v}\right)}_{\text{triple product}}-2s\left(\boldsymbol{v}\times\boldsymbol{p}'\right)+\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}'\right) \end{pmatrix}\\ & =\begin{pmatrix}0\\ \boldsymbol{p}'+\underbrace{\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}'\right)}_{\text{triple product}}-2s\left(\boldsymbol{v}\times\boldsymbol{p}'\right)+\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}'\right) \end{pmatrix}\\ & =\begin{pmatrix}0\\ \boldsymbol{p}'-2s\left(\boldsymbol{v}\times\boldsymbol{p}'\right)+2\boldsymbol{v}\times\left(\boldsymbol{v}\times\boldsymbol{p}'\right) \end{pmatrix} \end{aligned}