cachematrix, adapted from cachesolve