I need to make a function that multiplies an arbitrary number of matrices. My current attempt is the following:
@inline function compose_rotation(D1::Matrix, Ds::Matrix...) for Di in Ds D1 = Di*D1 end D1 end
The problem is that the performance is much worse than only the multiplication:
D1 = rand(3,3) D2 = rand(3,3) @time for i=1:10000000 compose_rotation(D1,D2,D1,D2) end 2.406881 seconds (30.00 M allocations: 4.470 GiB, 1.90% gc time) @time for i=1:10000000 D1*D2*D1*D2 end 2.004193 seconds (30.00 M allocations: 4.470 GiB, 2.44% gc time)
I am wondering: since I will always know how many matrices it is necessary to multiply at compile time, is there any way to “unroll” to loop so that I can improve the performance?
P.S.: This seems a little silly, but this function will be used to compute a composed rotation regardless the choice of the rotation description (matrix, quaternion, Euler axis/angle, etc.).