added code to calculate inverse of matrix and caching'