From: Bob Hanlon on

#[x] & /@ {Sin, Cos}

{sin(x),cos(x)}

{Sin[#], Cos[#]} &[x]

{sin(x),cos(x)}

Through[{Sin[#] &, Cos[#] &}[x]]

{sin(x),cos(x)}

% == %% == %%%

True


Bob Hanlon

---- Simon Pearce <Simon.Pearce(a)nottingham.ac.uk> wrote:

=============
Hi Mathgroup,



I have a list of pure functions, which I wish to apply to the same
argument. I can't seem to be able to thread the list of functions over
the argument in a simple way, all the commands like Apply and Map etc
take a single function. I can do it using Table and Part, but it seemed
like there should be a simple command that I'm missing.



For example:



In[1]:= functs = {Sin[#1] &, Cos[#1] &};



In[2]:= functs[x]



Out[2]= {Sin[#1] &, Cos[#1] &}[x]



I want to get the result {Sin[x],Cos[x]} out, without having to use
something of the form:



In[3]:= Table[functs[[i]][x], {i, 1, 2}]



Out[3]= {Sin[x], Cos[x]}



Which seems like it'd be less efficient when I try and use it on long
lists of functions.



Thanks,

Simon


From: Bill Rowe on
On 5/7/10 at 6:28 AM, Simon.Pearce(a)nottingham.ac.uk (Simon Pearce)
wrote:

>I have a list of pure functions, which I wish to apply to the same
>argument. I can't seem to be able to thread the list of functions
>over the argument in a simple way, all the commands like Apply and
>Map etc take a single function. I can do it using Table and Part,
>but it seemed like there should be a simple command that I'm
>missing.

>For example:

>In[1]:= functs = {Sin[#1] &, Cos[#1] &};
>In[2]:= functs[x]
>Out[2]= {Sin[#1] &, Cos[#1] &}[x]

>I want to get the result {Sin[x],Cos[x]} out,

Use Map, i.e.

In[4]:= #[x]&/@ functs

Out[4]= {Sin[x], Cos[x]}


From: Leonid Shifrin on
Hi Simon,

You are right, such a function does exist. Its name is Through:

In[9]:= functs = {Sin[#1] &, Cos[#1] &};

In[10]:= Through[functs[x]]


Out[10]= {Sin[x], Cos[x]}

Here is one more way to do this:

In[11]:= Map[#[x] &, functs]

Out[11]= {Sin[x], Cos[x]}

Regards,
Leonid


On Fri, May 7, 2010 at 2:28 PM, Simon Pearce
<Simon.Pearce(a)nottingham.ac.uk>wrote:

> Hi Mathgroup,
>
>
>
> I have a list of pure functions, which I wish to apply to the same
> argument. I can't seem to be able to thread the list of functions over
> the argument in a simple way, all the commands like Apply and Map etc
> take a single function. I can do it using Table and Part, but it seemed
> like there should be a simple command that I'm missing.
>
>
>
> For example:
>
>
>
> In[1]:= functs = {Sin[#1] &, Cos[#1] &};
>
>
>
> In[2]:= functs[x]
>
>
>
> Out[2]= {Sin[#1] &, Cos[#1] &}[x]
>
>
>
> I want to get the result {Sin[x],Cos[x]} out, without having to use
> something of the form:
>
>
>
> In[3]:= Table[functs[[i]][x], {i, 1, 2}]
>
>
>
> Out[3]= {Sin[x], Cos[x]}
>
>
>
> Which seems like it'd be less efficient when I try and use it on long
> lists of functions.
>
>
>
> Thanks,
>
> Simon
>
>


From: Raffy on
On May 7, 3:22 am, Simon Pearce <Simon.Pea...(a)nottingham.ac.uk> wrote:
> Hi Mathgroup,
>
> I have a list of pure functions, which I wish to apply to the same
> argument. I can't seem to be able to thread the list of functions over
> the argument in a simple way, all the commands like Apply and Map etc
> take a single function. I can do it using Table and Part, but it seemed
> like there should be a simple command that I'm missing.
>
> For example:
>
> In[1]:= functs = {Sin[#1] &, Cos[#1] &};
>
> In[2]:= functs[x]
>
> Out[2]= {Sin[#1] &, Cos[#1] &}[x]
>
> I want to get the result {Sin[x],Cos[x]} out, without having to use
> something of the form:
>
> In[3]:= Table[functs[[i]][x], {i, 1, 2}]
>
> Out[3]= {Sin[x], Cos[x]}
>
> Which seems like it'd be less efficient when I try and use it on long
> lists of functions.
>
> Thanks,
>
> Simon

Through[{Sin, Cos}[x]] === {Sin[x], Cos[x]}

I'd imagine this is more efficient however: Function[x, #[x] & /@
{Sin, Cos}][x]

Or more generalized:

gen = Function[v,Function[x,#[x]&/@v]
functs = gen@{Sin,Cos}
functs[x]

From: E. Martin-Serrano on
If I understood your question, it seems this will do

In[1]:= Through[{Sin[#] &, Cos[#] &}[x]]

Out[1]= {Sin[x], Cos[x]}


-----Original Message-----
From: Simon Pearce [mailto:Simon.Pearce(a)nottingham.ac.uk]
Sent: Friday, May 07, 2010 12:28 PM
Subject: Giving several functions the same argument

Hi Mathgroup,



I have a list of pure functions, which I wish to apply to the same
argument. I can't seem to be able to thread the list of functions over
the argument in a simple way, all the commands like Apply and Map etc
take a single function. I can do it using Table and Part, but it seemed
like there should be a simple command that I'm missing.



For example:



In[1]:= functs = {Sin[#1] &, Cos[#1] &};



In[2]:= functs[x]



Out[2]= {Sin[#1] &, Cos[#1] &}[x]



I want to get the result {Sin[x],Cos[x]} out, without having to use
something of the form:



In[3]:= Table[functs[[i]][x], {i, 1, 2}]



Out[3]= {Sin[x], Cos[x]}



Which seems like it'd be less efficient when I try and use it on long
lists of functions.



Thanks,

Simon