wrote functions for cachematrix