Can someone explain what exactly what the last part $P^{-1}$ in the formula $PQP^{-1}$ does. I heard it rotates halfway but I'm not understanding that. If it is the inverse, it goes back the exact same as it was?
[Math] Quaternion Rotation formula
quaternions
Related Solutions
Right off the bat, a reality check shows something amiss here.
Given ANY axis U = (xi, yj, zk) and a rotational angle alpha, the w (i.e., real) value of the resulting quaternion should be cos(alpha/2). But if alpha is 45 degrees, then w = cos(pi/8.0) should be around 0.924... and not somewhere about 0.963..., as you seem to expect when 'correctly' calculating ccc=quat([.3 .1 .6], 45).
Your error is that the axis rotation quaternion given axis U and rotation angle alpha assumes that U is already normalized (has length 1.0). So your formula should (in an ideal world, without any round-off error) be:
norm(U)*sin(alpha/2) + cos(alpha/2).
where norm(U) is U, 'normalized' to have length 1. What you have coded up is instead
norm(U*sin(alpha/2) + cos(alpha/2)).
As a test, try using your existing code to evaluate ccc = quat([300, 100, 600], 45). It will be much more 'off' than your example (where U is 'relatively' close to being normalized).
That should make sense: the quaternion that rotates 45 degrees around the axis (.3, .1, .6) should be exactly the same quaternion that rotates 45 degrees around the axis (300, 100, 600). In both caes, it's really the same axis: the latter is just a 'longer version' of the former.
Since we are not in an idealized world lacking round-off error, what you want to do is first, normalize the axis of rotation U; then construct the quaternion, and then normalize the quaternion:
norm(norm(U)*sin(alpha/2) + cos(alpha/2))
BTW, while it's quite readable, I don't recognize the syntax of the language your are working in. What language is it?
If you have unit quaternion $q$ then formula $u \to qu\bar q$ define rotation of imaginary quaternion $u$ over axis $Im(q)$ and angle having cosine of half equal to $Re(q)$. The oposite rotation is obtained by conjugate quaternion $\bar q$ giving formula $u \to \bar quq$. If you combine both transformations you obtain $\bar q qu\bar q q$ which is equal to $u$ because $q$ is unit quaternion and multiplication of quaternions is associative.
If you do not obtain the same point after applying rotation $q$ and $\bar q$ then you must have done some mistake. It is hard to know where might be the mistake without seeing your code.
Best Answer
In the product $PQP^{-1}$, $Q=q_1\mathbf i +q_2 \mathbf j +q_3 \mathbf k$ is a pure imaginary quaternion that represents the vector $\vec q=[q_1,q_2,q_3]^T$ in $\mathbb{R}^3$ and $P$ is a unitary quaternion, i.e. a quaternion $ p_0+ p_1\mathbf i +p_2 \mathbf j +p_3 \mathbf k=p_0+\mathbf p $ such that $||P||=\sqrt{p_0^2+p_1^2+p_2^2+p_3^2}=1$ so that $P$ can be represented in polar form as $P=e^{\theta \mathbf p}$, with $ \cos \theta =\frac{p_0}{||P||}$.
Whan we multiply $PQ$ the result is a quaternion, but not a pure imaginary quaternion, and this means that we cannot identify such product with a vector in $\mathbb{R}^3$ but, if we calculate the product $PQP^{-1}=e^{\theta \mathbf p}Qe^{-\theta \mathbf p}$ than this gives a pure imaginary quaternion, and we can see that it corresponds to a rotation of $\vec q$ by an angle $2 \theta$ around the axis oriented by the vector $\mathbf p$.
So, to give a direct answer to your question; the part $P^{-1}$ has no independent role, but is required, together with P to obtain as a result a vector.