Re: How to apply a list of functions
- To: mathgroup at smc.vnet.net
- Subject: [mg112886] Re: How to apply a list of functions
- From: "Vincent N. Virgilio" <virgilio at ieee.org>
- Date: Tue, 5 Oct 2010 05:32:29 -0400 (EDT)
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 >> >> >