Re: How to apply a list of functions
- To: mathgroup at smc.vnet.net
- Subject: [mg112883] Re: How to apply a list of functions
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Tue, 5 Oct 2010 05:31:56 -0400 (EDT)
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 > >