Zeroing the diagonal of a square matrix is an operation I need frequently, but somehow I still haven't managed to find an elegant solution that satisfies all these requirements:
- works with both numeric and symbolic matrices
- doesn't get tripped up by
- doesn't unpack packed arrays
- performs reasonably
So what do you use to zero out your diagonals? Is there any concise implementation for this at all?
Notes: Arithmetic with infinities tends to give indeterminates and
DiagonalMatrix strangely doesn't work with inifinities at all (even though it works with symbols). Writing an integer
0 into a packed array of reals unpacks it (or generally, trying to write an un-matching type of integer, real, complex).
UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1]? $\endgroup$