implemented caching for invertible matrices