Final commit

Made funtions to cache inv matrix and to calculate inv if needed