Quaternions Computation Time too long

6 vues (au cours des 30 derniers jours)
Ozan Anli
Ozan Anli le 22 Nov 2022
Modifié(e) : Jan le 22 Nov 2022
Hello,
we have written a script to illustrate the rotation of objects in space. This is calculated once using rotation matrices and once using quaternions. We have also tracked the computation time and noticed that the computation for the quaternions take longer than for the rotation matrices. According to the theory, the calculation of quaternions should work faster than for rotation matrices. Can you explain why this is not the case here.
Thank you very much in advance.
  1 commentaire
Jan
Jan le 22 Nov 2022
The relevant part of the code:
x = [0;1;0]; % Vector to rotate
theta = 33; % Angle
n = 999; % Number of rotations
Rx = rotx(theta);
tic;
for i = 1:n
y = Rx * x;
x = y;
end
toc;
Elapsed time is 0.005929 seconds.
y.'
ans = 1×3
0 -0.8910 -0.4540
% Quaternion
x = [0,1,0];
v = [1,0,0];
v_dach = quaternion(0, v(1), v(2), v(3));
theta = deg2rad(theta);
q = cos(theta/2) + sin(theta/2)*v_dach;
tic;
for i = 1:n
y = rotatepoint(q,x);
x = y;
end
toc;
Elapsed time is 0.071161 seconds.
y
y = 1×3
0 -0.8910 -0.4540

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 22 Nov 2022
Modifié(e) : Jan le 22 Nov 2022
While the multiplication with the rotation matrix calls an optimzed BLAS library directly, rotatepoint is a function, which calls the functions prepRotate for a normalization and compact after the multiplication. Calling functions have a certain overhead. Look into prepRotate to see, that there are further checks of the inputs and function calls.
The normalization matters, if the vectors have different scalings, e.g. x = [0, 1e180, 0]. Of course considering such exceptions costs runtime. The profiler helps you to examine, where the time is spent:
profile on
% run your code
profile report
"Faster in theory" is not really meaningful, if a function is applied to tiny input data. Maybe the theoretical advantage matters, if your input data has millions of points.

Catégories

En savoir plus sur Specialized Messages dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by