amended cachematrix.R