       Re: AW: Converting a mapping into a well-defined function

• To: mathgroup at smc.vnet.net
• Subject: [mg56571] Re: [mg56523] AW: [mg56499] Converting a mapping into a well-defined function
• From: DrBob <drbob at bigfoot.com>
• Date: Fri, 29 Apr 2005 03:20:38 -0400 (EDT)
• References: <200504280640.CAA24593@smc.vnet.net>
• Reply-to: drbob at bigfoot.com
• Sender: owner-wri-mathgroup at wolfram.com

```>> then 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.

Here's a variation that constructs the function itself.

teach[tuples_List, f_Symbol] := Module[{h},
Clear@f; h[z : {a_, _}] := (h[{a, _}] = Sequence[]; f[a] = Last@z;
z); h /@ tuples
]

data={{a,5},{b,27},{a,14},{c,4},{e,94},{b,6},{d,9},{e,4}};
teach[data,f]

{{a,5},{b,27},{c,4},{e,94},{d,9}}

f/@{a,b,c,d,e}

{5,27,4,9,94}

??f

(suppressed)

I prefer the function rather than the list version, myself, but this gives both.

Bobby

On Thu, 28 Apr 2005 02:40:10 -0400 (EDT), Wolf, Hartmut <Hartmut.Wolf at t-systems.com> wrote:

>
> -----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:= Sort[A, OrderedQ[{#1, #2}[[{1, 2}, 1]]] &]
> Out=
> {{a, 5}, {a, 14}, {b, 27}, {b, 6}, {c, 4}, {d, 9}, {e, 94}, {e, 4}}
>
> In:= Split[%, #1[] === #2[] &]
> Out=
> {{{a, 5}, {a, 14}}, {{b, 27}, {b, 6}}, {{c, 4}}, {{d, 9}}, {{e, 94}, {e, 4}}}
>
> In:= First /@ %
> Out=
> {{a, 5}, {b, 27}, {c, 4}, {d, 9}, {e, 94}}
>
>
> In 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:= absz = A[[All, 1]];
>          orda = Ordering[absz];
>          takes = Split[orda, absz[[#1]] === absz[[#2]] &][[All, 1]];
>          A[[takes]]
> Out=
> {{a, 5}, {b, 27}, {c, 4}, {d, 9}, {e, 94}}
>
>
> --
> Hartmut Wolf
>
>
>
>

--
DrBob at bigfoot.com

```

• Prev by Date: Re: Working with huge text files with mathematica
• Next by Date: books on writing packages
• Previous by thread: AW: Converting a mapping into a well-defined function
• Next by thread: gridlines