The relevant Mathematica function is GatherBy[ set, equivalence relation] -> {equivalence classes}:
In[5]:= GatherBy[{1, 2, 3, 4, 5}, Mod[#, 5] &] Out[5]= {{1}, {2}, {3}, {4}, {5}} In[6]:= GatherBy[{49, 33, 11, 1, 2, 3, 4, 5}, Mod[#, 5] &] Out[6]= {{49, 4}, {33, 3}, {11, 1}, {2}, {5}}
Or when using the equivalence relation 'Is Odd?' on the same set:
In[7]:= GatherBy[{49, 33, 11, 1, 2, 3, 4, 5},OddQ] Out[7]= {{49, 33, 11, 1, 3, 5}, {2, 4}}
No comments:
Post a Comment