written two functions to cache inverse