MathGroup Archive 2010

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

Search the Archive

Re: How to apply a list of functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112887] Re: How to apply a list of functions
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Tue, 5 Oct 2010 05:32:40 -0400 (EDT)

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
>>>
>>>
>>
>



  • Prev by Date: Re: Plotting a function dynamically in a loop
  • Next by Date: Re: Mathematica crash when exporting to HTML? is it me?
  • Previous by thread: Re: How to apply a list of functions
  • Next by thread: Re: How to apply a list of functions