First version. Function for creating a matrix with associated cache for expensive computations