Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
Documentation is obscure about this. –  Murta Sep 21 '13 at 0:09
    
Counter question: how can it determine the correct order? E.g. {4,5}>{5,4} or {4,5}<{5,4}? –  ybeltukov Sep 21 '13 at 0:09
    
Well.. I got the point. I don't know if I should delete this question. –  Murta Sep 21 '13 at 0:15
    
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. –  Kuba Sep 21 '13 at 0:15
2  
@Murta No, it starts again as soon as it meets False. Try Reap@Gather[l2, (Sow[{#, #2}]; ManhattanDistance[#1, #2] <= 2) &] –  Kuba Sep 21 '13 at 0:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.