Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

Re: Newbie question: pairswise function application

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23895] Re: Newbie question: pairswise function application
  • From: Hartmut Wolf <hwolf at debis.com>
  • Date: Thu, 15 Jun 2000 00:51:35 -0400 (EDT)
  • Organization: debis Systemhaus
  • References: <8i1t6n$jn9@smc.vnet.net> <Pine.GSO.4.21.0006131745370.18990-100000@flip.eecs.umich.edu>
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Daniel,

short code, but the package doesn't do well (for k=2);
alternatively I'd like to propose:

In[19]:= l = {1, 5, 19, 100};
In[20]:=
MapThread[Function[y, f[#1, y]] /@ #2 &, 
    Through[{First, Rest}[NestList[Rest, list, Length[list]]]]] // Flatten
Out[20]=
{f[1, 5], f[1, 19], f[1, 100], f[5, 19], f[5, 100], f[19, 100]}

to compare performance:

In[40]:= $RecursionLimit = 1000  (* !!! *)

In[52]:=
Print[Timing[MapThread[Function[y, Times[#1, y]] /@ #2 &, 
                Through[{First, Rest}[NestList[Rest, #, Length[#]]]]] // 
              Flatten][[1]]] & /@ (Range[100 #] & /@ Range[8]);
>From In[52]:= 0.391 Second
>From In[52]:= 0.641 Second
>From In[52]:= 0.941 Second
>From In[52]:= 1.482 Second
>From In[52]:= 2.103 Second
>From In[52]:= 2.924 Second
>From In[52]:= 3.776 Second
>From In[52]:= 4.857 Second

whereas

In[53]:=
Print[Timing[Times @@@ KSubsets[#, 2]][[1]]] & /@ (Range[100 #] & /@ 
        Range[8]);
>From In[53]:= 0.961 Second
>From In[53]:= 2.373 Second
>From In[53]:= 5.008 Second
>From In[53]:= 8.983 Second
>From In[53]:= 14.25 Second
>From In[53]:= 21.371 Second
>From In[53]:= 30.333 Second
>From In[53]:= 41.349 Second


Kind regards,
	Hartmut Wolf



Daniel Reeves schrieb:
> 
> 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
> >



  • Prev by Date: RE: Functional Expression Meaning (was:A Functional Expression Trick)
  • Next by Date: RE: Functional Expression Meaning (was:A Functional Expression Trick)
  • Previous by thread: Re: Newbie question: pairswise function application
  • Next by thread: Re: Newbie question: pairswise function application