/ concept-collection / seqlab
Sign in
concept-collection / seqlab
seqlab / src / engine / pulseq / +mr / +aux / +quat / multiply.m
24 lines · 925 BBlameHistoryRaw
1function qout = multiply(q1,q2)
2%MULTIPLY Calculate the product of two quaternions.
3% A single quaternion is represented as a 1 x 4 vector with the first
4% component being the real part and the 2nd to 4th components
5% corresponding to the complex vector part.Collections of N quaternions
6% can be stored as N x 4 matrices.
7
8% Calculate vector portion of quaternion product
9% vec = s1*v2 + s2*v1 + cross(v1,v2)
10vec = [q1(:,1).*q2(:,2) q1(:,1).*q2(:,3) q1(:,1).*q2(:,4)] + ...
11 [q2(:,1).*q1(:,2) q2(:,1).*q1(:,3) q2(:,1).*q1(:,4)]+...
12 [ q1(:,3).*q2(:,4)-q1(:,4).*q2(:,3) ...
13 q1(:,4).*q2(:,2)-q1(:,2).*q2(:,4) ...
14 q1(:,2).*q2(:,3)-q1(:,3).*q2(:,2)];
16% Calculate scalar portion of quaternion product
17% scalar = s1*s2 - dot(v1,v2)
18scalar = q1(:,1).*q2(:,1) - q1(:,2).*q2(:,2) - ...
19 q1(:,3).*q2(:,3) - q1(:,4).*q2(:,4);
21qout = [scalar vec];
23end
moveopenescclose