Perform quaternion multiplication.
q3 = quatmult(q1,q2)
Perform lefthand quaternion multiplication of q2
by q1
, i.e. "q1
times q2
". The input quaternions(s) are given as 4xN arrays. The resulting quaternion(s) are returned as 4xN arrays.
Just as matrix multiplication between rotation matrices combines sequential rotations, quaternion multiplication between unit quaternions also combines sequential rotations.
Note that quatmult
is vectorized and will accept arrays of unit quaternions of size 4xNxMx... with an arbitrary number of dimensions, in which case the resulting quaternions will be returned as an array of size 4xNxMx... .
Taking two arbitrary quaternions and normalizing the results, we perform quaternion multiplication
q1 = [0.1; 0.438; -0.879; 0.699]; q1 = q1./sqrt(sum(q1.^2)); q2 = [0.46; -0.984; -0.324; 0.167]; q2 = q2./sqrt(sum(q2.^2)); q3 = quatmult(q1,q2)
q3 = 0.0545 0.1319 -0.8642 -0.4824
To show that quaternion multiplication between unit quaternions equates to combining sequential rotations, we first rotate an arbitrary unit vector vec
using q3
. Then, we rotate vec
first using q2
and second using q1
. Finally, we compare the results of the two methods.
vec = [0.3; 0.9; -0.4]; vec = vec./sqrt(sum(vec.^2)); quatvecmult(q3,vec) quatvecmult(q1, quatvecmult(q2,vec))
ans = -0.3468 0.0367 0.9372 ans = -0.3468 0.0367 0.9372
rotmat2quat, quat2euler, rotmat2quat, euler2quat, quatvecmult, quatinv