Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Q: Union and SameTest Option

  • To: mathgroup at
  • 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

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


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

  • Prev by Date: MathLink & Metrowerks CodeWarrior
  • Next by Date: Re: CompiledFunction for matrices ??
  • Previous by thread: Re: Q: Union and SameTest Option
  • Next by thread: Re: Q: Union and SameTest Option