Optimise calculation with NxNxMxM matrix

You can always make an interpolation for g that allows it to be evaluated at in-between points.