Given a list of coordinates r
(r[[i]]!=r[[j]]
), I'd like to know the reciprocals of distances of all pairs in the list, and for the convenience subsequent operations, the trace of the resulting matrix should be all zero. I feel that this should be a frequent need, but I can't do it optimally.
My code:
R = Outer[Norm, r, r, 1];
rR = Quiet[1/R] /. {ComplexInfinity -> 0.}
But this is not such a good idea as ReplaceAll
is significantly slower than the other calculations in this code. Is it a good idea to use For
or Table
and loop over all indices, or is there a better way to do this?
r
values are different, why not just set the diagonal to 0 afterward? DoUpperTriangularize[#, 1] + LowerTriangularize[#, -1] &@Quiet[1/R]
. See here. $\endgroup$Outer[Norm, r, r, 1]
should beOuter[EuclideanDistance, r, r, 1]
? $\endgroup$