Cached matrix implementation