Cache matrix function