What is the Canonical way to map a function to only the diagonal elements of a matrix?
For example, given
A = {{E, 0}, {0, E}}
I wanted to take the log of the diagonal elements only to obtain {{1, 0}, {0, 1}}
I came up with these
MapAt[Log[#] &, A, Table[{i, i}, {i, Length[A]}]]
result = Log[Diagonal[A]];
ReplacePart[A, {i_, i_} :> result[[i]]]
Since in Mathematica the rule of thumb is that there should be at least 10 different ways to do the same thing, I think there is room to find a better approach.
MapIndexed
rather thanMapAt
? $\endgroup$ReplacePart[A, {i_, i_} :> Log[A[[i, i]]]]
$\endgroup$