# Maths - Expansion of q * P1 * q'

## Rotation - Expanding out all the terms

So why do we use an equation like: Pout = q * Pin * conj(q) to rotate a point?

We can demonstrate it works by expanding out the parts of the complex number using:

• Pout = i Pout.x + j Pout.y + k Pout.z
• q = qw + i qx + j qy + k qz
• Pin = i x + j y + k z

We can expand out all the terms of the rotation as follows:

P2=q * P1 * q'

gives the following, first substitute in P1 and q':

P2=q * (0 + i x + j y + k z) * (qw - i qx - j qy - k qz)

 P2=q * 0*qw +x*qx +y*qy +z*qz + i (x*qw - 0*qx - y*qz+z*qy) + j (-0*qy +x*qz+ y*qw - z*qx) + k (0*-qz - x*qy +y*qx + z*qw)

now substitute terms in q and multiply out terms:

 P2= qw*(0*qw +x*qx +y*qy +z*qz) - qx*(x*qw - 0*qx - y*qz+z*qy) - qy*(-0*qy +x*qz+ y*qw - z*qx) - qz* (0*-qz - x*qy +y*qx + z*qw) + i (qx*(0*qw +x*qx +y*qy +z*qz) + qw*(x*qw - 0*qx - y*qz+z*qy) + qy* (0*-qz - x*qy +y*qx + z*qw)- qz*(-0*qy +x*qz+ y*qw - z*qx)) + j (qw*(-0*qy +x*qz+ y*qw - z*qx) - qx* (0*-qz - x*qy +y*qx + z*qw)+ qy*(0*qw +x*qx +y*qy +z*qz) + qz*(x*qw - 0*qx - y*qz+z*qy)) + k (qw* (0*-qz - x*qy +y*qx + z*qw) + qx*(-0*qy +x*qz+ y*qw - z*qx) - qy*(x*qw - 0*qx - y*qz+z*qy) + qz*(0*qw +x*qx +y*qy +z*qz))

so expanding out gives (padding with 0):

real part = qw*(0*qw +x*qx +y*qy +z*qz) - qx*(x*qw - 0*qx - y*qz+z*qy) - qy*(-0*qy +x*qz+ y*qw - z*qx) - qz* (0*-qz - x*qy +y*qx + z*qw)
real part = qw(x*qx+y*qy +z*qz) - qx*(x*qw - y*qz+z*qy) - qy*(x*qz+ y*qw - z*qx) - qz* (- x*qy +y*qx + z*qw)
real part = x*(qw*qx - qx*qw- qy*qz + qz*qy) + y*(qw*qy+qx*qz- qy*qw- qz*qx) + z*(qw*qz - qx*qy+ qy*qx- qz*qw)
real part = 0

i part = qx*(0*qw +x*qx +y*qy +z*qz) + qw*(x*qw - 0*qx - y*qz+z*qy) + qy* (0*-qz - x*qy +y*qx + z*qw)- qz*(-0*qy +x*qz+ y*qw - z*qx)
i part = qx*(x*qx +y*qy +z*qz) + qw*(x*qw - y*qz+z*qy) + qy* (- x*qy +y*qx + z*qw)- qz*(x*qz+ y*qw - z*qx)
i part = x*(qx*qx+qw*qw-qy*qy- qz*qz) + y*(qx*qy- qw*qz+ qy*qx- qz*qw) + z*(qx*qz+ qw*qy+ qy*qw+ qz*qx)
i part = x*(qx*qx+qw*qw-qy*qy- qz*qz) + y*(2*qx*qy- 2*qw*qz) + z*(2*qx*qz+ 2*qw*qy)

j part = qw*(-0*qy +x*qz+ y*qw - z*qx) - qx* (0*-qz - x*qy +y*qx + z*qw)+ qy*(0*qw +x*qx +y*qy +z*qz) + qz*(x*qw - 0*qx - y*qz+z*qy)
j part = qw*(x*qz+ y*qw - z*qx) + qx* (x*qy - y*qx - z*qw)+ qy*(x*qx +y*qy +z*qz) + qz*(x*qw - y*qz+z*qy)
j part = x*(qw*qz + qx*qy+ qy*qx+ qz*qw) + y*(qw*qw - qx*qx+ qy*qy - qz*qz)+ z*(-qw*qx- qx*qw+ qy*qz+ qz*qy)
j part = x*(2*qw*qz + 2*qx*qy) + y*(qw*qw - qx*qx+ qy*qy - qz*qz)+ z*(-2*qw*qx+ 2*qy*qz)

k part = qw* (0*-qz - x*qy +y*qx + z*qw) + qx*(-0*qy +x*qz+ y*qw - z*qx) - qy*(x*qw - 0*qx - y*qz+z*qy) + qz*(0*qw +x*qx +y*qy +z*qz)
k part = qw* (-x*qy +y*qx + z*qw) + qx*(x*qz+ y*qw - z*qx) - qy*(x*qw -y*qz+z*qy) + qz*(x*qx +y*qy +z*qz)
k part = x*(-qw*qy+ qx*qz- qy*qw+ qz*qx) + y*(qw*qx+ qx*qw+ qy*qz+ qz*qy)+ z*(qw*qw - qx*qx- qy*qy+ qz*qz)
k part = x*(-2*qw*qy+ 2*qx*qz) + y*(2*qw*qx+ 2*qy*qz)+ z*(qw*qw - qx*qx- qy*qy+ qz*qz)

So combining these gives:

P2.x = x*(qx*qx+qw*qw-qy*qy- qz*qz) + y*(2*qx*qy- 2*qw*qz) + z*(2*qx*qz+ 2*qw*qy)
P2.y = x*(2*qw*qz + 2*qx*qy) + y*(qw*qw - qx*qx+ qy*qy - qz*qz)+ z*(-2*qw*qx+ 2*qy*qz)
P2.z = x*(-2*qw*qy+ 2*qx*qz) + y*(2*qw*qx+ 2*qy*qz)+ z*(qw*qw - qx*qx- qy*qy+ qz*qz)

This can be written in the form of a matrix:

 P2.x P2.y P2.z
=
 qx*qx+qw*qw-qy*qy- qz*qz 2*qx*qy- 2*qw*qz 2*qx*qz+ 2*qw*qy 2*qw*qz + 2*qx*qy qw*qw - qx*qx+ qy*qy - qz*qz -2*qw*qx+ 2*qy*qz -2*qw*qy+ 2*qx*qz 2*qw*qx+ 2*qy*qz qw*qw - qx*qx- qy*qy+ qz*qz
 P1.x P1.y P1.z

## Reflection - expansion of result

We can expand out all the terms of the rotation as follows:

P2=q * P1 * q

gives the following, first substitute in P1 and q:

P2=q * (0 + i x + j y + k z) * (qw + i qx + j qy + k qz)

 P2=q * - x*qx - y*qy -z*qz + i (x*qw + y*qz - z*qy) + j ( - x*qz+ y*qw + z*qx) + k ( x*qy - y*qx + z*qw)

now substitute terms in q and multiply out terms:

 P2= qw*(- x*qx - y*qy -z*qz) - qx*(x*qw + y*qz - z*qy) - qy*( - x*qz+ y*qw + z*qx)- qz*( x*qy - y*qx + z*qw) + i (qx*(- x*qx - y*qy -z*qz) + qw*(x*qw + y*qz - z*qy) + qy*( x*qy - y*qx + z*qw) - qz*( - x*qz+ y*qw + z*qx)) + j (qw*( - x*qz+ y*qw + z*qx) - qx*( x*qy - y*qx + z*qw) + qy*(- x*qx - y*qy -z*qz) + qz*(x*qw + y*qz - z*qy)) + k (qw*( x*qy - y*qx + z*qw) + qx*( - x*qz+ y*qw + z*qx) - qy*(x*qw + y*qz - z*qy) + qz*(- x*qx - y*qy -z*qz))

For reflections qw = 0 so we can simplify to:

 P2= - qx*(y*qz - z*qy) - qy*( - x*qz + z*qx)- qz*( x*qy - y*qx) + i (qx*(- x*qx - y*qy -z*qz) + qy*( x*qy - y*qx ) - qz*( - x*qz + z*qx)) + j (- qx*( x*qy - y*qx ) + qy*(- x*qx - y*qy -z*qz) + qz*(y*qz - z*qy)) + k (qx*( - x*qz + z*qx) - qy*( + y*qz - z*qy) + qz*(- x*qx - y*qy -z*qz))

combining terms gives:

 P2= - qx*y*qz + qx*z*qy + qy*x*qz - qy*z*qx -qz*x*qy + qz*y*qx) + i (-qx*x*qx - qx*y*qy -qx*z*qz + qy*x*qy - qy*y*qx + qz*x*qz - qz*z*qx) + j (-qx*x*qy + qx*y*qx - qy*x*qx - qy*y*qy -qy*z*qz + qz*y*qz - qz*z*qy) + k (- qx*x*qz + qx*z*qx - qy*y*qz + qy*z*qy - qz*x*qx - qz*y*qy -qz*z*qz)

cancelling gives:

 P2= 0+ i (-qx*x*qx - qx*y*qy -qx*z*qz + qy*x*qy - qy*y*qx + qz*x*qz - qz*z*qx) + j (-qx*x*qy + qx*y*qx - qy*x*qx - qy*y*qy -qy*z*qz + qz*y*qz - qz*z*qy) + k (- qx*x*qz + qx*z*qx - qy*y*qz + qy*z*qy - qz*x*qx - qz*y*qy -qz*z*qz)

grouping together into matrix form:

Refl = 1 / (Px2 + Py2 + Pz2)*
 -qx2 + qy2+ qz2 -2*qx*qy -2*qx*qz -2*qx*qy qx2 - qy2+ qz2 -2*qy*qz -2*qx*qz -2*qy*qz qx2 + qy2- qz2
[P1]