Functions for cached matrix inverse adapted from mean example.