I am looking for the most efficient way to do matrix * matrix
and matrix * vector
operations for 3x3 rotation and 4x4 transformation matrices in C#.
I currently store my matrices in multidimensional arrays (new double[3,3]
, new double[4,4]
). I am not totally adverse to changing that but if possible I would like to keep the syntax. My current multiplication using the 3 standard nested for loops works fine but can be a bottleneck.
My thoughts so far:
- Optimized algorithms like Strassen are not practical for these sizes
- Parallelisation does not make much sense either at the level of a single 4x4 multiplication; better done at a higher level.
- multidimensional arrays are (were?) slower in c# due to less efficient boundary checks, however this can be overcome with unsafe pointer arithmetic. (I am not sure how current this information is)
- rotation matrices are symmetric, there might be a way to exploit that?
- the biggest gains can probably be achieved by using cache-locality, making sure that values that are close together in memory are accessed together; but I am unsure how to do this.
So before I hack together my own solution using unsafe, fixed and 3 for loops, is there already a tested and optimized solution for this standard problem out there?
Or are there other optimizations that I have overlooked?