Working cachematrix function