cached matrix implementation