Re: Newbie question: pairswise function application

• To: mathgroup at smc.vnet.net
• Subject: [mg23865] Re: Newbie question: pairswise function application
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Thu, 15 Jun 2000 00:51:05 -0400 (EDT)
• References: <8i1t6n\$jn9@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Sidney:

pairs[data_] :=
Transpose[Apply[Join,
NestList[#, #[data], Length[data] - 2]] & /@ {Drop[#, -1] &,
Drop[#, 1] &}]

Is a quick way of getting all the pairs that you need, but not in the order
that you gave.

prs = pairs[L]

{{1, 5}, {5, 19}, {19, 100}, {100, 101}, {1, 19}, {5, 100}, {19,
101}, {1,
100}, {5, 101}, {1, 101}}

f @@@ prs

{f[1, 5], f[5, 19], f[19, 100], f[100, 101], f[1, 19], f[5, 100],
f[19, 101],
f[1, 100], f[5, 101], f[1, 101]}

If you need the pairs to be ordered then:

f @@@ Sort[prs]

{f[1, 5], f[1, 19], f[1, 100], f[1, 101], f[5, 19], f[5, 100], f[5,
101],
f[19, 100], f[19, 101], f[100, 101]}

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Sidney Cadot" <sidney at janis.pds.tudelft.nl> wrote in message
news:8i1t6n\$jn9 at smc.vnet.net...
> Hi,
>
> Wandering through the 1500-odd pages of the Mathematica book, I can't
> find the solution to the following rather silly problem:
>
> I have a list L consisting of (a lot of) integers, and I want to apply a
> function f to all pairs of numbers; furthermore, I don't want to
> calculate both f[a,b] and f[b,a]:
>
>   L = {1,5,19,100};
>
> ... and I want:
>
>   { f[1,5], f[1,19], f[1,100], f[5,19], f[5,100], f[19,100] }
>
> Can anyone please tell me how to do this? I'd prefer a smart solution
> (i.e., not just generating all pairs, then throwing away a lot of them)
> since my lists tend to get rather large.
>