How to calculate the divergence efficiently? I’m not talking about a GAN divergence, but the actual divergence which is the sum of the partial derivative of all elements of a vector (https://en.wikipedia.org/wiki/Divergence 9).

Assume f(x): R^d-> R^d. I could use autograd to get the derivative matrix (of size d x d) and then simply take the sum of the diagonals. But this is seems terribly inefficient and wasteful. There has to be a better way!