implemented matrix caching