Compute trace of an ndarray