# Quaternion estimator algorithm

The quaternion estimator algorithm (QUEST) is an algorithm designed to solve Wahba's problem, that consists of finding a rotation matrix between two coordinate systems from two sets of observations sampled in each system respectively. The key idea behind the algorithm is to find an expression of the loss function for the Wahba's problem as a quadratic form, using the Cayley–Hamilton theorem and the Newton–Raphson method to efficiently solve the eigenvalue problem and construct a numerically stable representation of the solution.
The algorithm was introduced by Malcolm D. Shuster in 1981, while working at Computer Sciences Corporation. While being in principle less robust than other methods such as Davenport's q method or singular value decomposition, the algorithm is significantly faster and reliable in practical applications, and it is used for attitude determination problem in fields such as robotics and avionics.

## Formulation of the problem

Wahba's problem consists of finding a rotation matrix $\mathbf {A} ^{*}$ that minimises the loss function
    $l\left(\mathbf {A} \right)={\frac {1}{2}}\sum _{i=1}^{n}a_{i}\left\|\mathbf {w} _{i}-\mathbf {A} \mathbf {v} _{i}\right\|^{2}$
where $\mathbf {w} _{i}$ are the vector observations in the reference frame, $\mathbf {v} _{i}$ are the vector observations in the body frame, $\mathbf {A}$ is a rotation matrix between the two frames, and $a_{i}$ are a set of weights such that $\textstyle \sum _{i}a_{i}=1$. It is possible to rewrite this as a maximisation problem of a gain function $g$
    $g\left(\mathbf {A} \right)=1-l\left(\mathbf {A} \right)=\sum _{i}a_{i}\mathbf {w} _{i}^{\top }\mathbf {A} \mathbf {v} _{i}$
defined in such a way that the loss $l$ attains a minimum when $g$ is maximised. The gain $g$ can in turn be rewritten as
    $g\left(\mathbf {A} \right)=\operatorname {tr} \left(\mathbf {A} \mathbf {B} ^{\top }\right)$
where $\mathbf {B} =\textstyle \sum _{i}a_{i}\mathbf {w} _{i}\mathbf {v} _{i}^{\top }$ is known as the attitude profile matrix.
In order to reduce the number of variables, the problem can be reformulated by parametrising the rotation as a unit quaternion $\mathbf {q} =\left(v_{1},v_{2},v_{3},q\right)$ with vector part $\mathbf {v} =\left(v_{1},v_{2},v_{3}\right)$ and scalar part $q$, representing the rotation of angle $\theta =2\cos ^{-1}q$ around an axis whose direction is described by the vector $\textstyle {\frac {1}{\sin {\frac {\theta }{2}}}}\mathbf {v}$, subject to the unity constraint $\mathbf {q} ^{\top }\mathbf {q} =1$. It is now possible to express $\mathbf {A}$ in terms of the quaternion parametrisation as
    $\mathbf {A} =\left(q^{2}-\mathbf {v} \cdot \mathbf {v} \right)\mathbf {I} +2\mathbf {v} \mathbf {v} ^{\top }+2q\mathbf {V} _{\times }$
where $\mathbf {V} _{\times }$ is the skew-symmetric matrix
    $\mathbf {V} _{\times }={\begin{pmatrix}0&v_{3}&-v_{2}\\-v_{3}&0&v_{1}\\v_{2}&-v_{1}&0\\\end{pmatrix}}$.
Substituting $\mathbf {A}$ with the quaternion representation and simplifying the resulting expression, the gain function can be written as a quadratic form in $\mathbf {q}$
    $g(\mathbf {q} )=\mathbf {q} ^{\top }\mathbf {K} \mathbf {q}$
where the $4\times 4$ matrix
    $\mathbf {K} ={\begin{pmatrix}\mathbf {S} -\sigma \mathbf {I} &\mathbf {z} \\\mathbf {z} ^{\top }&\sigma \end{pmatrix}}$
is defined from the quantities
    ${\begin{aligned}\mathbf {S} &=\mathbf {B} +\mathbf {B} ^{\top }\\\mathbf {z} &=\sum _{i}a_{i}\left(\mathbf {w} _{i}\times \mathbf {v} _{i}\right)\\\sigma &=\operatorname {tr} \mathbf {B} .\end{aligned}}$
This quadratic form can be optimised under the unity constraint by adding a Lagrange multiplier $-\lambda \mathbf {q} ^{\top }\mathbf {q}$, obtaining an unconstrained gain function
    ${\hat {g}}\left(\mathbf {q} \right)=\mathbf {q} ^{\top }\mathbf {K} \mathbf {q} -\lambda \mathbf {q} ^{\top }\mathbf {q}$
that attains a maximum when
    $\mathbf {K} \mathbf {q} =\lambda \mathbf {q}$.
This implies that the optimal rotation is parametrised by the quaternion $\mathbf {q} ^{*}$ that is the eigenvector associated to the largest eigenvalue $\lambda _{\text{max}}$ of $\mathbf {K}$.

## Solution of the characteristic equation

The optimal quaternion can be determined by solving the characteristic equation of $\mathbf {K}$ and constructing the eigenvector for the largest eigenvalue. From the definition of $\mathbf {K}$, it is possible to rewrite
    $\mathbf {K} \mathbf {q} =\lambda \mathbf {q}$
as a system of two equations
    ${\begin{aligned}\mathbf {y} &=\left((\lambda +\sigma )\mathbf {I} -\mathbf {S} \right)^{-1}\mathbf {z} \\\lambda &=\sigma +\mathbf {z} \mathbf {y} \end{aligned}}$
where $\mathbf {y} =\textstyle {\frac {1}{q}}\mathbf {v}$ is the Rodrigues vector. Substituting $\mathbf {y}$ in the second equation with the first, it is possible to derive an expression of the characteristic equation
    $\lambda =\sigma +\mathbf {z} ^{\top }\left((\lambda +\sigma )\mathbf {I} -\mathbf {S} \right)^{-1}\mathbf {z}$.
Since $\lambda _{\text{max}}=\max g\left(\mathbf {A} \right)$, it follows that $\lambda _{\text{max}}=1-\min l\left(\mathbf {A} \right)$ and therefore $\lambda _{\text{max}}\approx 1$ for an optimal solution (when the loss $l$ is small). This permits to construct the optimal quaternion $\mathbf {q} ^{*}$ by replacing $\lambda _{\text{max}}$ in the Rodrigues vector $\mathbf {y}$
    $\mathbf {q} ^{*}={\frac {1}{\sqrt {1+\left|\mathbf {y} _{\lambda _{\text{max}}}\right|^{2}}}}(\mathbf {y} ,1)^{\top }$.
The $\mathbf {y}$ vector is however singular for $\theta =\pi$. An alternative expression of the solution that does not involve the Rodrigues vector can be constructed using the Cayley–Hamilton theorem. The characteristic equation of a $3\times 3$ matrix $\mathbf {S}$ is
    $\det \left[\mathbf {S} -\xi \mathbf {I} \right]=-\xi ^{3}+2\sigma \xi ^{2}-k\xi +\Delta =0$
where
    ${\begin{aligned}\sigma &={\frac {1}{2}}\operatorname {tr} {\mathbf {S} }\\k&=\operatorname {tr} \left(\operatorname {adj} \mathbf {S} \right)\\\Delta &=\det \mathbf {S} \end{aligned}}$
The Cayley–Hamilton theorem states that any square matrix over a commutative ring satisfies its own characteristic equation, therefore
    $-\mathbf {S} ^{3}+2\sigma \mathbf {S} ^{2}-k\mathbf {S} +\Delta =0$
allowing to write
    $\left((\omega +\sigma )\mathbf {I} -\mathbf {S} \right)^{-1}={\frac {\alpha \mathbf {I} +\beta \mathbf {S} +\mathbf {S} ^{2}}{\gamma }}$
where
    ${\begin{aligned}\alpha &=\omega ^{2}-\sigma ^{2}+k\\\beta &=\omega -\sigma \\\gamma &=(\omega +\sigma )\alpha -\Delta \end{aligned}}$
and for $\omega =\lambda _{\text{max}}$ this provides a new construction of the optimal vector
    ${\begin{aligned}\mathbf {y} ^{*}&=\left((\lambda +\sigma )\mathbf {I} -\mathbf {S} \right)^{-1}\mathbf {z} \\&={\frac {\alpha \mathbf {I} +\beta \mathbf {S} +\mathbf {S} ^{2}}{\gamma }}\mathbf {z} \end{aligned}}$
that gives the conjugate quaternion representation of the optimal rotation as
    $\mathbf {q} ^{*}={\frac {1}{\sqrt {\gamma ^{2}+\left|\mathbf {x} \right|^{2}}}}(\mathbf {x} ,\gamma )^{\top }$
where
    $\mathbf {x} =\left(\alpha \mathbf {I} +\beta \mathbf {S} +\mathbf {S} ^{2}\right)\mathbf {z}$.
The value of $\lambda _{\text{max}}$ can be determined as a numerical solution of the characteristic equation. Replacing $\left((\omega +\sigma )\mathbf {I} -\mathbf {S} \right)^{-1}$ inside the previously obtained characteristic equation
    $\lambda =\sigma +\mathbf {z} ^{\top }\left((\lambda +\sigma )\mathbf {I} -\mathbf {S} \right)^{-1}\mathbf {z}$.
gives
    $\lambda ^{4}-(a+b)\lambda ^{2}-c\lambda +(ab+c\sigma -d)=0$
where
    ${\begin{aligned}a&=\sigma ^{2}-k\\b&=\sigma ^{2}+\mathbf {z} ^{\top }\mathbf {z} \\c&=\Delta +\mathbf {z} ^{\top }\mathbf {S} \mathbf {z} \\d&=\mathbf {z} ^{\top }\mathbf {S} ^{2}\mathbf {z} \end{aligned}}$
whose root can be efficiently approximated with the Newton–Raphson method, taking 1 as initial guess of the solution in order to converge to the highest eigenvalue (using the fact, shown above, that $\lambda _{\text{max}}\approx 1$ when the quaternion is close to the optimal solution).