MATHEMATICS

Sabtu, 21 Mei 2011

Equivalence classes in Mathematica

An equivalence relation like 'has the same remainder after division by 5' partitions a set in equivalence classes.

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}}

Tidak ada komentar:

Posting Komentar