Re: How to apply a list of functions
- To: mathgroup at smc.vnet.net
- Subject: [mg112892] Re: How to apply a list of functions
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Tue, 5 Oct 2010 05:33:36 -0400 (EDT)
Vince, My feeling is that Inner is in principle a little faster, since it is more specialized. For example, here it is true (although the difference is not at all dramatic): In[67]:= Do[Inner[Rule,Range[100000],Range[100001,200000],List],{10}]//Timing Out[67]= {0.797,Null} In[68]:= Do[MapThread[Rule,{Range[100000],Range[100001,200000]}],{10}]//Timing Out[68]= {0.922,Null} But this seems to show up only in the most trivial cases like this one above. Perhaps, the overhead of Compose is sufficient to hide this effect. Besides, I just did a few benchmarks, and indeed using Hold instead of List slows Inner down somewhat, so all in all it is not worth the trouble, I agree. Regards, Leonid On Mon, Oct 4, 2010 at 10:02 PM, Vincent N. Virgilio <virgilio at ieee.org>wrote: > Leonid, > > Certainly. > > (The timing difference between MapThread and Inner/Hold appears to be in > the noise. I didn't look at Inner/List as closely.) > > Vince > > > On Mon, Oct 4, 2010 at 1:46 PM, Leonid Shifrin <lshifr at gmail.com> wrote: > >> Vince, >> >> How about this: >> >> In[51]:= >> Inner[Compose,Hold[f1,f2,f3],Hold[{{1,2},{3,4}},{{5,6},{7,8}},{{9,10},{11,12}}],List] >> >> Out[51]= {f1[{{1,2},{3,4}}],f2[{{5,6},{7,8}}],f3[{{9,10},{11,12}}]} >> >> Basically, wrap your arguments in Hold rather than List, and you don't >> have this problem. >> >> Regards, >> Leonid >> >> On Mon, Oct 4, 2010 at 9:37 PM, Vincent N. Virgilio <virgilio at ieee.org>wrote: >> >>> Leonid, >>> >>> I notice that Inner/Compose is a little fragile if x is, say, a list of >>> matrices, and the goal is to apply each f to each whole matrix. >>> MapThread/Compose handles that transparently. >>> >>> Thanks again, >>> >>> Vince >>> >>> On Mon, Oct 4, 2010 at 1:07 PM, Leonid Shifrin <lshifr at gmail.com> wrote: >>> >>>> Hi Vince, >>>> >>>> First, you could use Compose in your code - perhaps it is a little >>>> faster: >>>> >>>> In[2]:= MapThread[Compose,{{f1,f2,f3},{x1,x2,x3}}] >>>> >>>> Out[2]= {f1[x1],f2[x2],f3[x3]} >>>> >>>> Using Inner can be a bit faster still: >>>> >>>> In[4]:= Inner[Compose,{f1,f2,f3},{x1,x2,x3},List] >>>> >>>> Out[4]= {f1[x1],f2[x2],f3[x3]} >>>> >>>> In most cases, you probably won't see the difference in performance, >>>> unless your functions >>>> do very little and you have lots of them. >>>> >>>> Regards, >>>> Leonid >>>> >>>> >>>> >>>> >>>> On Mon, Oct 4, 2010 at 2:06 PM, Vince Virgilio <blueschi at gmail.com>wrote: >>>> >>>>> On Oct 3, 3:39 am, Sam Takoy <sam.ta... at yahoo.com> wrote: >>>>> > Hi, >>>>> > >>>>> > As a follow up to my question about apply {Sin, Cos} to x, I came up >>>>> > with the tasteless >>>>> > >>>>> > Map[Apply[#, {x}] &, {Sin, Cos}] >>>>> > >>>>> > but I expect that the pros in this ng will be able to improve upon >>>>> it. >>>>> > >>>>> > Thanks, >>>>> > >>>>> > Sam >>>>> >>>>> Tangentially, >>>>> >>>>> I've always wondered if there was a better way to apply a list of >>>>> functions to a list of arguments. Here's how I do it, where f and x >>>>> are the respective lists. >>>>> >>>>> MapThread[#1@#2 &, {f, x}] >>>>> >>>>> Seems performant. >>>>> >>>>> Anyone? >>>>> >>>>> Vince >>>>> >>>>> >>>> >>> >> >