revised code to cache matrix inverse