finished the cachematrix.R function by Ruiyuan Li