added implementation of cached matrix inversion