Add sparse matrix support for CachedFunction and improve plugin check

There may be a way to reduce code duplication for all the "assert(0)" methods.