Consider the following piece of code:
DistanceMatrix[Range[10], DistanceFunction -> Function[{x, y}, x - y]]
I would have expected it to return a skew-symmetric matrix, but it seems that MMA forces my custom distance function to return positive values (no this is not because MMA is just computing one triangular half and copying it over; even though that maybe how it is operating). For example,
DistanceMatrix[Range[10],
DistanceFunction -> Function[{x, y}, If[EvenQ[x - y], True, False]]]
reveals that each element is run through an Abs. Why is this happening? Specifically, can I prevent Mathematica from doing that?
I do understand the mathematical definition of a distance function (positivity, symmetry, triangle inequality etc.) so I am not looking for an answer that says "Oh that is how the math is defined." On the other hand, it is conceivable that you want to perform a some operation on each element of Subsets[someList, {2}] and put it into a square matrix and this was my first thought. I found this which I could probably use, but the DistanceFunction approach seemed most natural.
It is possible that I am missing a more obvious/simpler way to do it; in which case I apologize - please enlighten me on the more idiomatic way.
Outer[f, l1, l2]; so this question becomes moot, and I don't mind if you vote to close. But if someone does have a way to prevent that call toAbsin custom distance function, I would be very curious to see how the hack works. – ITA Mar 08 '21 at 20:09Unprotect[Abs];Abs[x_]:=xcomes to mind. I'm not 100% sure how to clean up properly after this hack. Restarting the kernel is my goto... – Adam Mar 08 '21 at 22:42DistanceMatrixenforces symmetry. Some amount of internal code might be relying on that. – Daniel Lichtblau Mar 09 '21 at 14:11