# Quaternions: Magnitude of rotation about an arbitrary axis

Given a unit quaternion in hamiltonian notiation $q$ which describes some arbitrary rotation of $\alpha$ about axis $\beta$ as

$$q =\begin{bmatrix} \cos(\alpha/2) \\ \sin(\alpha/2)\beta_x \\ \sin(\alpha/2)\beta_y \\ \sin(\alpha/2)\beta_z \\ \end{bmatrix}$$

how would I calculate the magnitude of rotation (in radians) that quaternion gives me about some arbitrary axis $v$?

As further clarification, what I mean by "magnitude of rotation about an arbitrary axis" would be the same as finding the angle $\alpha^\star$ which minimizes the angle $\tilde{\alpha}$ encoded by the "error" between the two quaternions $$\tilde{q} = q^{-1} \otimes q^\star$$ where $$q^\star =\begin{bmatrix} \cos(\alpha^\star/2) \\ \sin(\alpha^\star/2)v_x \\ \sin(\alpha^\star/2)v_y \\ \sin(\alpha^\star/2)v_z \\ \end{bmatrix}$$ and $$\tilde q =\begin{bmatrix} \cos(\tilde\alpha /2) \\ \sin(\tilde\alpha /2)\tilde\beta_x \\ \sin(\tilde\alpha /2)\tilde\beta_y \\ \sin(\tilde\alpha /2)\tilde\beta_z \\ \end{bmatrix}$$

and $v=\begin{bmatrix} v_x & v_y & v_z \end{bmatrix}^\top$ is the arbitrary axis of rotation.

I believe that this calculation is similar to calculating euler angles from a quaternion, i.e. "yaw" is typically the amount of rotation about the $z$ axis, and is can be calculated with (link)

$$\psi = \textrm{atan}\frac{2(q_w q_z + q_x q_y)}{ 1-2(q_y^2+q_z^2)}$$

and you can form a quaternion with rotation only about $z$ through an angle $\psi$ with

$$q_\psi = \begin{bmatrix} \cos\left(\frac{\psi}{2}\right) & 0 & 0 & \sin\left(\frac{\psi}{2}\right) \end{bmatrix}^\top.$$

• How are you defining the rotation about an arbitrary axis? For instance, take a rotation of say $\frac\pi4$ radians about the $x$ axis. What is the magnitude of this rotation about the $z$ axis? Mar 13, 2018 at 17:20
• In that case, there would be no rotation about $z$, because it was purely about $x$. I believe that the calculation is somehow related to the dot product between the axis of rotation of the quaternion and the arbitrary axis in question. Whether it's a direct scaling or not is what I'm a little confused about. Mar 13, 2018 at 17:23
• The catch is that one usually thinks of a rotation based on its affect on points, and while it's true that there are points where the rotation about the $x$ axis has no '$z$ axis component', there are other points where that's not true; for instance, the transform of the point $(1,0,1)$ by that $\frac\pi4$ rotation would be $(1,\sqrt{2}/2, \sqrt{2}/2)$ (modulo some minus signs) and it's clear that some rotation around the $z$ axis has occurred here. I don't think there's any way of getting what you're after in a frame-independent fashion. Mar 13, 2018 at 17:36
• @superjax: your comment above suggests that you not only don't know the answer to your question, you don't actually have a completely clear idea of what your question itself means. That makes it very tough for us. Where did you get the notion of "magnitude of a rotation about some axis other than the axis of rotation"? If you can show us a clear presentation of that notion, we can probably help you compute it. Without that, we're all just shooting in the dark. Mar 13, 2018 at 17:45
• I think this is more clear, though it also makes the question relatively straightforward, I think - you want to maximize the real part of $\widetilde{q}$ (in your notation), and that's relatively easily expressed as a function of $\alpha$. Can you talk about what you're trying to use this for, by any chance? This smells a little bit like an XY problem... Mar 13, 2018 at 18:50

First of all, I want to note that your formulas are slightly wrong: you should have, e.g., $q=(\cos(\alpha/2), \sin(\alpha/2)\beta_x, \sin(\alpha/2)\beta_y, \sin(\alpha/2)\beta_z)$, where ${\bf \beta}=(\beta_x, \beta_y, \beta_z)$ is the unit vector representing the rotation axis; instead you write $\cos(\beta_x/2)$, etc., which doesn't really make mathematical sense. I'll assume that all of the quaternions are in this corrected form in what follows.

Let's break this down a little bit. For clarity's sake, I'll use $\bf{q}$ for the imaginary (i.e. vector) part of $q$, and similarly $\bf{\widetilde{q}}$, etc; $\mathbf{v}$ here is the unit vector that you're trying to find a 'comparable' rotation around. (Note that by this notation, ${\bf q^\star}=\sin(\alpha^\star/2){\bf v}$).

By your own formula, we have $\widetilde{q} = q^{-1}q^\star$, and we're interested in finding a $q^\star$ that maximizes the real part of $\widetilde{q}$. (This is equivalent to minimizing the angle $\tilde{\alpha}$, since $\cos$ is a decreasing function in the domain we're interested in.) But that part is $\widetilde{q}_w=(q^{-1})_wq^\star_w-{\bf q^{-1}}\cdot{\bf q^\star}$ $= \cos(\alpha/2)\cos(\alpha^\star/2) + \sin(\alpha/2)\sin(\alpha^\star/2){\bf \beta}\cdot{\bf v}$. (Sanity check: when ${\bf \beta\cdot v}=1$ — i.e., when they're the same vector — this expression is $\cos((\alpha-\alpha^\star)/2)$ and we maximize it by taking $\alpha^\star=\alpha$, which is what you'd expect.)

Now, in general, to maximize an expression of the form $a\cos(\theta)+b\sin(\theta)$, we can view it as the (2d) dot product of $(a,b)$ with $(\cos(\theta), \sin(\theta))$; seen this way, it's clear that the maximum is achieved when these two vectors are aligned — that is, when $\theta=\arctan(\frac ba)$. Written in this form, you can see that the optimal $\alpha^\star$ is the one with $\tan(\alpha^\star/2)={\bf \beta\cdot v}\tan(\alpha/2)$.

Another way is to find the rotation which is closest to $$q$$ but which has no component in the direction of $$v$$, then remove that intermediate quaternion, $$\hat{q}$$ from the original, $$q$$.

Notation: \begin{aligned} R(q):& \, \textrm{rotation matrix formed from q (performs a passive rotation)}\\ (\cdot)^\times:& \, \textrm{skew-symmetric matrix} \\ \exp(\cdot):& \, \textrm{Quaternion exponential map} \\ \otimes:& \, \textrm{quaternion multiplication operator} \end{aligned}

First, find a the quaternion closest to $$q$$ with no rotation about $$v$$.

• Rotate $$v$$ by $$q$$ to get $$w$$ $$w = R\left(q\right)^\top v$$
• Then, use the exponential map of the quaternion to find the shortest rotation $$\hat{q}$$ between $$w$$ and $$v$$. Being the shortest rotation, $$\hat{q}$$ by definition has no rotation about either $$v$$ or $$w$$. Let $$\theta=v^\top w$$ be the angle of rotation and $$\delta=(v)^\times w$$ be the axis of rotation between $$v$$ and $$w$$.

$$\hat{q} = \exp\left(\theta \delta\right)$$

Now remove this part of the rotation from $$q$$

• At this point, it's pretty straight-forward to find $$q^\star$$

$$q^\star = q \otimes \hat{q}^{-1}$$

The nice thing about this approach is that it is easy to find on-manifold jacobians across this operation using Lie algebra techniques.

Compute the magnitude of rotation in $$q^\star$$ $$\phi = 2\arccos(q_w)$$