Re: Newbie question: pairswise function application
- To: mathgroup at smc.vnet.net
- Subject: [mg23891] Re: Newbie question: pairswise function application
- From: Daniel Reeves <dreeves at eecs.umich.edu>
- Date: Thu, 15 Jun 2000 00:51:30 -0400 (EDT)
- References: <8i1t6n$jn9@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Needs["DiscreteMath`Combinatorica`"]; ?KSubsets "KSubsets[l, k] gives all subsets of set l containing exactly k elements, ordered lexicographically." pairs = KSubsets[{1, 5, 19, 100}, 2] {{1, 5}, {1, 19}, {1, 100}, {5, 19}, {5, 100}, {19, 100}} f @@@ pairs (* replace @@@ with @@#&/@ for version < 4 *) {f[1, 5], f[1, 19], f[1, 100], f[5, 19], f[5, 100], f[19, 100]} Note that KSubsets, in the case of pairs, works by prepending the first element to each of the Rest of the elements, and then recursively calling on the Rest of the list. -- -- -- -- -- -- -- -- -- -- -- -- Daniel Reeves http://ai.eecs.umich.edu/people/dreeves/ "Try identifying the problem and then solving it." -- suggestion from Dilbert's boss --- \/ FROM Sidney Cadot ON 00.06.12 01:41 (Yesterday) \/ --- > 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. > > Thanks in advance, > > Sidney Cadot > sidney at janis.pds.twi.tudelft.nl >