I have a piece of code in Fortran90 in which I have to solve both a non-linear (for which I have to invert the Jacobian matrix) and a linear system of equations. When I say very small I mean n unknowns for both operations, with n<=4. Unfortunately, n is not known a priori. What do you think is the fastest option? I thought of writing explicit formulas for cases with n=1,2 and using other methods for n=3,4 (e.g. some functions of the Intel MKL libraries), for the sake of performance. Is this sensible or should I write explicit formulas for the inverse matrix also for n=3,4?
The code is going to be called very many times in a Finite Element Method analysis, for this reason I was looking for the fastest solution. I was also looking for a comparison chart of MKL matrix inversion routines for very small matrices versus explicit methods.
We know about such problem, but currently we don't provide optimization for such tiny cases and may be would worth for you to try implement the direct methods to make the inversion.
If you only need to solve simultaneous equations, you do not need to compute the inverse. It is more efficient to do an LU, LLT or LDU decomposition and then solve. In your case, it may even be the case that you only need to do the decomposition once and can subsequently use the factors repeatedly to obtain solutions.
If you have, or can obtain and install, the Intel IPP library, there is a section called "Small Matrices and Realistic Rendering" that you can look up and see if there are routines there that suit your needs. This part of the IPP is "deprecated", but since you have an older version of MKL that may not be an obstacle.