Re: Intersection @ Complement

*To*: mathgroup at smc.vnet.net*Subject*: [mg58141] Re: Intersection @ Complement*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>*Date*: Mon, 20 Jun 2005 05:21:17 -0400 (EDT)*Organization*: The Open University, Milton Keynes, England*References*: <d90tcg$99f$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Zak Seidov wrote: > Some crazy thing Mmca does with > Intersection/Complement: > > s1={1,2,5,7,4,4,1};s2={1,6,3,5,7,7,1,6}; > Intersection[s1,s2] > {1,5,7} (* not {1,1,5,7} ?!*) > Complement[s1,s2] > {2,4} (* not {2,4,4} ?!*) > Complement[s2,s1] > {3,6} (* not {3,6,6,7} ?!*) > > Interestingly, in both cases Mmca Help > says that the result is SORTED list, > while actually it's UNION not SORT that works?! > And note that in the last case 7 is absent at all ?! > what d'u think about this? thanks a lot, zak > Hi Zak, I am afraid that there is nothing crazy at all in Mathematica behavior! Mathematica works just has expected with *set* operators such as intersection or complement. We do not have a specific data type for *sets* built-in Mathematica. So the only way I am aware of to represent a *set* is to use a list (here I might be wrong but the Mathematica gurus in this forum will correct me if needed). So whenever you feed Mathematica with some lists ordered in a specific way and containing repeated elements and then use *set* operations such as intersection or complement on these lists, Mathematica first reorganizes internally these lists as equivalent of *sets*: it orders them in lexicographic order (here too I might be wrong about the most general cases) and keeps only one occurrence of each elements. Then the *set* operation is applied and a list that represents a *set* is returned. For example, first we create two lists In[1]:= lst1 = {1, 2, 5, 7, 4, 4, 1} lst2 = {1, 6, 3, 5, 7, 7, 1, 6} Out[1]= {1, 2, 5, 7, 4, 4, 1} Out[2]= {1, 6, 3, 5, 7, 7, 1, 6} Now we create the "associated" *sets* In[3]:= s1 = Union[lst1] s2 = Union[lst2] Out[3]= {1, 2, 4, 5, 7} Out[4]= {1, 3, 5, 6, 7} In[5]:= Intersection[s1, s2] Out[5]= {1, 5, 7} In[6]:= Intersection[lst1, lst2] Out[6]= {1, 5, 7} And we can see that both operations yield the same result. Best regards, /J.M.