Implementation of cached matrix