cache a matrix function