Quaternion to matrix
A unit quaternion \(\mathbf{q}=\group{q_w, \vec{q}}\) represents a rotation by an angle \(\alpha) around an axis \(\hat{r}\) with
\[\begin{aligned}
&\alpha = 2\acos{q_w}
\end{aligned}\]
\[\begin{aligned}
&\hat{r} = \frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}
\end{aligned}\]
Proof
Step 1
A unit quaternion \(\mathbf{q}=\group{q_w, \vec{q}}\) represents a rotation around a rotation by an angle \(\alpha\) around an axis \(\hat{r}\) with
\[\begin{aligned}
&\alpha = 2\acos{q_w}
\end{aligned}\]
\[\begin{aligned}
&\hat{r} = \frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}
\end{aligned}\]
Substitute these values into the matrix representing such a transformation we obtain
\[\begin{align*} R =& \left[\begin{matrix} t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x^2 + c & t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y - s \group{\frac{q_x}{\sqrt{1-\vec{q}_w^2}}}_z & t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z + s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y \\ t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y + s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z & t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y^2 + c & t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z - s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x \\ t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z - s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y & t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z + s \group{\frac{q_x}{\sqrt{1-\vec{q}_w^2}}}_x & t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z^2 + c \end{matrix}\right]\\ t =& 1 - c^2\\ c =& \cos\group{2\cos^{-1}\group{\vec{q}_w}}\\ s =& \sin\group{2\cos^{-1}\group{\vec{q}_w}} \end{align*}\]There is potential for simplification
Step 1
First we simplify the terms \(s\), \(c\), and \(1 - c\).
\[\begin{align*} c &= \cos\group{2\cos^{-1}\group{q_w}} \\ &= \cos^2\group{\cos^{-1}\group{q_w}} - \sin^2\group{\cos^{-1}\group{q_w}} \\ &= q_w^2 - \sin^2\group{\cos^{-1}(q_w)} \\ &= q_w^2 - 1 + \cos^2\group{\cos^{-1}\group{q_w}} \\ &= q_w^2 - 1 + q_w^2\\ &= 2 q_w^2 - 1 \end{align*}\] \[\begin{align*} t &= \group{1 - c}\\ &= 1 - 2 \vec{q}_w^2 + 1\\ &= 2 - 2 \vec{q}_w^2 \\ &= 2\group{1 - \vec{q}_w^2} \\ \end{align*}\] \[\begin{align*} s &= \sin\group{2\cos^{-1}\group{\vec{q}_w}}\\ &= 2\cos\group{\cos^{-1}\group{\vec{q}_w}}\sin\group{\cos^{-1}\group{\vec{q}_w}}\\ &= 2 \vec{q}_w \sin \group{\cos^{-1}\group{\vec{q}_w}}\\ &= 2 \vec{q}_w \sqrt{1-\vec{q}_w^2} \\ \end{align*}\]Step 2
We simplify the elements along the main diagonal \(R_{0,0}\), \(R_{1,1}\), and \(R_{2,2}\).
\[\begin{align*} R_{0,0} =& 2 \group{1 - \vec{q}_w^2} \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x^2 + 2 \vec{q}_w^2 - 1\\ R_{0,0} =& 2 \group{1 - \vec{q}_w^2} \frac{\vec{q}_x}{\sqrt{1-\vec{q}_w^2}}^2 + 2 \vec{q}_w^2 - 1\\ R_{0,0} =& 2 \group{1 - \vec{q}_w^2} \frac{\vec{q}_x^2}{1-\vec{q}_w^2} + 2 \vec{q}_w^2 - 1\\ R_{0,0} =& 2 \vec{q}_x^2 + 2 \vec{q}_w^2 - 1\\ R_{0,0} =& 2 \group{\vec{q}_x^2 + \vec{q}_w^2} - 1\\ R_{0,0} =& 2 \group{1 - \vec{q}_y^2 - \vec{q}_z^2} - 1\\ R_{0,0} =& 2 - 2\vec{q}_y^2 - 2\vec{q}_z^2 - 1\\ R_{0,0} =& 1 - 2\vec{q}_y^2 - 2\vec{q}_z^2\\ \end{align*}\]The approach for the elements \(R_{1}{1}\) and \(R_{2,2}\) is similar. Hence we obtain
\[\begin{align*} R =& \left[\begin{matrix} 1 - 2\vec{q}_y^2 - 2\vec{q}_z^2 & t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y - s \group{\frac{\vec{q}_x}{\sqrt{1-\vec{q}_w^2}}}_z & t \group{\frac{\vec{q}}{\sqrt{1 - \vec{q}_w^2}}}_x \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z + s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y \\ t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y + s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z & 1 - 2\vec{q}_x^2 - 2\vec{q}_z^2 & t \group{\frac{\vec{q}}{\sqrt{1 - \vec{q}_w^2}}}_y \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z - s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x \\ t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_x \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z - s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y & t \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_y \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_z + s \group{\frac{\vec{q}_x}{\sqrt{1-\vec{q}_w^2}}}_x & 1 - 2\vec{q}_x^2 - 2\vec{q}_y^2 \end{matrix}\right]\\ t =& 2\group{1 - q_w^2}\\ c =& 2 q_w^2 - 1\\ s =& 2 q_w \sqrt{1-\vec{q}_w^2} \end{align*}\]Step 3
We simplify the subterm
\[ t\group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_i \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_j, i, k \in \{x,y,z\}, i \neq k \]as follows
\[\begin{align*} &t\group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_i \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_j\\ = &2\group{1 - \vec{q}_w^2}\frac{\vec{q}_i}{\sqrt{1-\vec{q}_w^2}} \frac{\vec{q}_j}{\sqrt{1-\vec{q}_w^2}}\\ = &2 \vec{q}_i \vec{q}_j \end{align*}\]Step 4
We simplify the subterm
\[ s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_i, i \in \{x,y,z\} \]as follows
\[\begin{align*} &s \group{\frac{\vec{q}}{\sqrt{1-\vec{q}_w^2}}}_i\\ = &2 \vec{q}_w \sqrt{1-\vec{q}_w^2} \frac{\vec{q}_i}{\sqrt{1-\vec{q}_w^2}}\\ = &2 \vec{q}_w \vec{q}_i \end{align*}\]Final result
As the final result we obtain
\[\begin{align*} R =& \left[\begin{matrix} 1 - 2\vec{q}_y^2 - 2\vec{q}_z^2 & 2 \vec{q}_x \vec{q}_y - 2 \vec{q}_w \vec{q}_z & 2 \vec{q}_x \vec{q}_z + 2 \vec{q}_w \vec{q}_y \\ 2 \vec{q}_x \vec{q}_y + 2 \vec{q}_w \vec{q}_z & 1 - 2\vec{q}_x^2 - 2\vec{q}_z^2 & 2 \vec{q}_y \vec{q}_z - 2 \vec{q}_w \vec{q}_x \\ 2 \vec{q}_x \vec{q}_z - 2 \vec{q}_w \vec{q}_y & 2 \vec{q}_y \vec{q}_z + 2 \vec{q}_w \vec{q}_x & 1 - 2\vec{q}_x^2 - 2\vec{q}_y^2 \end{matrix}\right] \end{align*}\]