Re: Q: Union and SameTest Option
- To: mathgroup at smc.vnet.net
- Subject: [mg16113] Re: [mg16016] Q: Union and SameTest Option
- From: David Withoff <withoff>
- Date: Thu, 25 Feb 1999 08:25:04 -0500
- Sender: owner-wri-mathgroup at wolfram.com
> Hi Mathematica-Groupers! > > Can anybody explain to me what's going on here ? > > In[270]:= > Union[ {{a,b,1},{a,c,1},{x,y,2},{a,b,2}}, > SameTest->(#1[[2]]===#2[[2]]&) ] > Out[270]= > {{a,b,1},{a,c,1},{x,y,2}} > > is ok since the first and the last list element have the same second > component. But what is wrong here now ? > > In[272]:= > Union[ {{a,b,1},{a,c,1},{x,y,2},{a,b,2}}, > SameTest->(#1[[3]]===#2[[3]]&) ] > Out[272]= > {{a,b,1},{a,b,2},{a,c,1},{x,y,2}} > > Thanks in advance > Clemens > > ------------------------------------------------------------ > Clemens Frey > Doctoral Student at the > Department of Mathematics/BITOEK > University of Bayreuth(Germany) > > clemens.frey at uni-bayreuth.de > http://www.bitoek.uni-bayreuth.de/~Clemens.Frey > ------------------------------------------------------------ See http://www.wolfram.com/support/Kernel/Symbols/System/Union.html in the Wolfram Research web site for a discussion of this design. The Union function sorts the elements before comparing them, and compares only adjacent elements after sorting. This is done for reasons of efficiency. This also causes the behavior in your example, since elements that match your SameTest aren't sorted into adjacent positions. The web page mentioned above contains additional discussion and alternate definitions for Union that compare all pairs of elements. Dave Withoff Wolfram Research