We can avoid that computation for vectors and matrices easily by writing special case functions, which will probably handle most use cases. I couldn't figure out how to express this computation for higher-order tensors using direct linear indexing.