A generic solution to cache the results of operations on matrices