completed cachematrix function