Matrix multiplication (best computational approach)

4 Ansichten (letzte 30 Tage)
Julián Francisco
Julián Francisco am 14 Mai 2012
I have to make a coordinates transformation between two reference systems (axes). For it, three matrices (3x3) have to be multiplied due to some intermediate axes are used. I have thought two approaches to resolve this:
method #1: Making the multiplication directly, i.e. v_f = R1*R2*R3*v_i
method #2: Split into steps.
step 1: v_3i = R3*v_i
step 2: v_23 = R2*v_3i
step 3: v_f = R1*v_23
where: R1, R2 and R3 are 3x3 matrices
v_f,v_i, v_3i, v_23 are 3x1 vectors
I would like to know what method is more efficient computationally (less time) to do the transformation (this will be made a lot of times).

Akzeptierte Antwort

Teja Muppirala
Teja Muppirala am 14 Mai 2012
I think you might be better off grouping your multiplications so that you maximize matrix-vector products instead of matrix-matrix products.
For example:
R1 = rand(3);
R2 = rand(3);
R3 = rand(3);
v = rand(3,1);
tic;
for n = 1:1e6, R1*R2*R3*v; end;
toc;
% Slightly faster
tic;
for n = 1:1e6, R1*(R2*(R3*v)); end;
toc;
This is especially true for larger matrices much bigger than 3x3.
R1 = rand(1000);
R2 = rand(1000);
R3 = rand(1000);
v = rand(1000,1);
tic;
R1*R2*R3*v;
toc;
% Much faster
tic;
R1*(R2*(R3*v));
toc;
Of course if the R's are all constant and you are reusing them, then you should just multiply them all together and store that result to begin with.
  3 Kommentare
Jan
Jan am 14 Mai 2012
R3*v replies a vector such that forcing the execution to start from the right is more efficient than the accumulating the rotation matrices at first.
Julián Francisco
Julián Francisco am 14 Mai 2012
@Jan Simon: Thank you again.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jan
Jan am 14 Mai 2012
There is no difference internally. The intermediate results have to be calculated in both cases. A TIC/TOC measurement allows you to find out, which one is faster.
Usually it is possible to calculate R1*R2*R3 explicitely without a matrix multiplication.
[EDITED, appended after your comment]:
function R = RotMatrix(N, A)
% N: [1 x 3], normalized vector to rotate around
% A: [1 x 1], angle
% Calculate trigonometric values once to save time:
cA = cos(A);
sA = sin(A);
cmA = 1 - cA;
x = N(:, 1);
y = N(:, 2);
z = N(:, 3);
ysA = y .* sA; % Some optimizations to save products
zsA = z .* sA;
xsA = x .* sA;
xcmA = x .* cmA;
ycmA = y .* cmA;
yzcmA = z .* ycmA;
xycmA = x .* ycmA;
xzcmA = z .* xcmA;
R = [cA + x .* xcmA, xycmA + zsA, xzcmA - ysA; ...
xycmA - zsA, cA + y .* ycmA, yzcmA + xsA; ...
xzcmA + ysA, xsA - yzcmA, cA + z .* z .* cmA];
For a real application it could be more efficient to accept [nFrame x 3] arrays as list of vectors and a [nFrame x 1] list of angles. Then the context decides, which output dimensions are most useful, perhaps a [3 x 3 x nFrame] array or three [3 x nFrame] matrices, which allow a DOT product directly to transform vectors.
  3 Kommentare
Jan
Jan am 14 Mai 2012
See: http://en.wikipedia.org/wiki/Rotation_matrix and http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions . Any rotation can be described by a 3x3 matrix, which represents a rotation around a specific axes in 3D, see "General rotation" and "Rotation from axes and angle".
Julián Francisco
Julián Francisco am 14 Mai 2012
Thank you back for your comments and answers.

Melden Sie sich an, um zu kommentieren.


James Tursa
James Tursa am 14 Mai 2012
If you are simply after speed, the fastest approach will be a mex routine to do the calculations inline (i.e., explicitly without calling a separate generic matrix multiply package ala BLAS). This avoids all of the forming of intermediate variables. How many is "... lots of times"? Which variable(s) are changing in each iteration of the loop?
  3 Kommentare
James Tursa
James Tursa am 14 Mai 2012
So *all* of the matrices are changing and are formed at each iteration? How do you form them? With trig functions of angles for each element of the matrix? If so, rather than forming the matrices at the m-file level it would also be a speed benefit to simply pass the angles into the mex routine directly and have the mex routine calculate the matrix elements on the fly as well as do the matrix multiplies on the fly. Can you give a short section of your code that gives the angle inputs and your matrix formation?
Julián Francisco
Julián Francisco am 14 Mai 2012
Yes, you are right. I use functions of angles for each matrix element. For the matrix rotation I have written the following code:
function [R] = rot(axis,theta)
% axis is refering to the axis around the rotation is made (use 1 for x-axis, 2 for y-axis and 3 for z-axis)
% theta is the rotation angle
S = sin(theta);
C = cos(theta);
R = eye(3);
switch axis
case 1
R(2,2) = C;
R(3,2) = -S;
R(2,3) = S;
R(3,3) = C;
case 2
R(1,1) = C;
R(3,1) = S;
R(1,3) = -S;
R(3,3) = C;
otherwise
R(1,1) = C;
R(2,1) = -S;
R(1,2) = S;
R(2,2) = C;
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by