generalize the Diag() function