Quaternions Computation Time too long

4 次查看(过去 30 天)
Ozan Anli
Ozan Anli 2022-11-22
编辑: Jan 2022-11-22
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 个评论
Jan
Jan 2022-11-22
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

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2022-11-22
编辑:Jan 2022-11-22
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.

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by