4
$\begingroup$

I get intrigued about Gather behavior on this post. I thought that Gather was invariant under list order?

Here is the result I spect:

l1={{1, 1}, {0, 0}, {2, 2}, {5, 5}, {4, 4}, {6, 6}}
Gather[l1,ManhattanDistance[#1, #2] <= 2 &]

{{{1,1},{0,0},{2,2}},{{5,5},{4,4},{6,6}}}

When the list order is changed, I get this:

l2={{0, 0}, {1, 1}, {2, 2}, {4, 4}, {5, 5}, {6, 6}}
Gather[l2,ManhattanDistance[#1,#2]<= 2&]

{{{0,0},{1,1}},{{2,2}},{{4,4},{5,5}},{{6,6}}}

Someone know why?

$\endgroup$
  • $\begingroup$ Documentation is obscure about this. $\endgroup$ – Murta Sep 21 '13 at 0:09
  • $\begingroup$ Counter question: how can it determine the correct order? E.g. {4,5}>{5,4} or {4,5}<{5,4}? $\endgroup$ – ybeltukov Sep 21 '13 at 0:09
  • $\begingroup$ Well.. I got the point. I don't know if I should delete this question. $\endgroup$ – Murta Sep 21 '13 at 0:15
  • $\begingroup$ I don;t know where's @ssch 's comment but I think he's right. Relation has to be transitive. If it's not the result may vary with order. For example, for the case of l2 the result {{{0,0}}, {{1,1}, {2,2}}, {{4,4}}, {{5,5}, {6,6}}} is as good as the one you obtained. $\endgroup$ – Kuba Sep 21 '13 at 0:15
  • 4
    $\begingroup$ @Murta No, it starts again as soon as it meets False. Try Reap@Gather[l2, (Sow[{#, #2}]; ManhattanDistance[#1, #2] <= 2) &] $\endgroup$ – Kuba Sep 21 '13 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.