From: Simon Pearce on 7 May 2010 06:22 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: David Park on 8 May 2010 07:04 functs = {Sin[#1] &, Cos[#1] &}; Through(a)functs[x] {Sin[x], Cos[x]} David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: Simon Pearce [mailto:Simon.Pearce(a)nottingham.ac.uk] 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: Simon Pearce on 8 May 2010 07:04 Thanks David, that's exactly what I wanted. I just couldn't find the command at all! Simon -----Original Message----- From: David Park [mailto:djmpark(a)comcast.net] Sent: 07 May 2010 12:28 To: 'Simon Pearce'; mathgroup(a)smc.vnet.net Subject: Re: Giving several functions the same argument functs == {Sin[#1] &, Cos[#1] &}; Through(a)functs[x] {Sin[x], Cos[x]} David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: Simon Pearce [mailto:Simon.Pearce(a)nottingham.ac.uk] 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 This message has been checked for viruses but the contents of an attachment= may still contain software viruses which could damage your computer system:= you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
From: cinnabar on 8 May 2010 07:05 On 7 =D0=BC=D0=B0=D0=B9, 12:22, 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 Hi, Simon The problem is you are applying a list to an argument, not a pure function Correct way is: In[1]:= {Sin[#], Cos[#]} &[x] Out[1]= {Sin[x], Cos[x]} i.e. making one pure function with a list of bodies Best Regards, Roman
From: Ray Koopman on 8 May 2010 07:05 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 In[1]:= funx = {Sin,Cos,Tan,Sinh,Cosh,Tanh,whatever} Out[1]= {Sin,Cos,Tan,Sinh,Cosh,Tanh,whatever} In[2]:= #[x]& /@ funx Out[2]= {Sin[x],Cos[x],Tan[x],Sinh[x],Cosh[x],Tanh[x],whatever[x]}
|
Next
|
Last
Pages: 1 2 3 Prev: FrontEnd and bash automatization Next: Easy question, please help to run a function n times |