AW: Converting a mapping into a well-defined function

*To*: mathgroup at smc.vnet.net*Subject*: [mg56523] AW: [mg56499] Converting a mapping into a well-defined function*From*: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>*Date*: Thu, 28 Apr 2005 02:40:10 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

-----Ursprüngliche Nachricht----- Von: Gilmar [mailto:gilmar.rodriguez at nwfwmd.state.fl.us] Gesendet: Mittwoch, 27. April 2005 03:54 An: mathgroup at smc.vnet.net Betreff: [mg56499] Converting a mapping into a well-defined function Dear Mathematica User Friends: I need to build a module that takes a non-empty set A (whose elements are 2-tuples), and appends (or not) those 2-tuples to a set B according to the following criteria: (1.) if the abscissa of a 2-tuple in A is unique (among the abscissas of all the other 2-tuples in A), then the module appends that 2-tuple to the set B. (2.) if there are n 2-tuples in A that share the same abscissa, then the module appends the first of those 2-tuples to B ("first" here means the first of such n 2-tuple encountered, as you read A from left to right),but does not append the rest to B. (3.) the module does not attempt to sort the set B, after the appending process is completed. Example: If A = {{a,5},{b,27},{a,14},{c,4},{e,94},{b,6},{d,9},{e,4}} then B=Module[A]={{a,5},{b,27},{c,4},{d,9},{e,94}}. Another way to re-phrase the above example; if A defines a mapping: A(a)=5, A(b)=27, A(a)=14, A(c)=4, A(e)=94, A(b)=6, A(d)=9, A(e)=4 them the module converts the mapping A into a well-defined function: A(a)=5, A(b)=27, A(c)=4, A(d)=9, A(e)=94. Thank you for your help! Gilmar, this certainly is a case for Carl Woll's OrderedUnion (which deservedly re-occurs at least twice a year in this group), and similar solutions. As your solution has a property you didn't state explicitely, but which may be exploited, namely that your result is (canonically) ordered by the "abszissa", I will use this opportunity to explain the Sort-Split metaphor, which is simple and effective: In[23]:= Sort[A, OrderedQ[{#1, #2}[[{1, 2}, 1]]] &] Out[23]= {{a, 5}, {a, 14}, {b, 27}, {b, 6}, {c, 4}, {d, 9}, {e, 94}, {e, 4}} In[24]:= Split[%, #1[[1]] === #2[[1]] &] Out[24]= {{{a, 5}, {a, 14}}, {{b, 27}, {b, 6}}, {{c, 4}}, {{d, 9}}, {{e, 94}, {e, 4}}} In[25]:= First /@ % Out[25]= {{a, 5}, {b, 27}, {c, 4}, {d, 9}, {e, 94}} In[23] sorts A according to the ordering of the abszissa, and this is a stable sort, i.e. with same abszissa the elements occur in their original order. Split and First then picks out the first elements with equal abzissa, and this is what you wanted. The same idea can be persued with Ordering (which is faster than Sort). But Ordering is not stable, however its has the property -- which I called weakly stable -- that the first element of "equals" in A comes first in ordering, and the last as last. Other elemnts in between may be permuted. But this is good enough for us: In[26]:= absz = A[[All, 1]]; orda = Ordering[absz]; takes = Split[orda, absz[[#1]] === absz[[#2]] &][[All, 1]]; A[[takes]] Out[29]= {{a, 5}, {b, 27}, {c, 4}, {d, 9}, {e, 94}} -- Hartmut Wolf

**Follow-Ups**:**Re: AW: Converting a mapping into a well-defined function***From:*DrBob <drbob@bigfoot.com>