From: Ingolf Dahl on 8 May 2010 07:08 Hi Simon. Define backmap[funclist_, x__] := Apply[#1, {x}] & /@ funclist Then backmap[{f, g}, x] gives {f[x], g[x]} and backmap[{Sin[#1] &, Cos[#1] &}, x] gives {Sin[x], Cos[x]} and backmap[{Sin[#1] &, Cos[#1] &}, Pi/6] gives {1/2,Sqrt[3]/2} You might also do backmap[{f, g}, x, y] which gives {f[x, y], g[x, y]} Best regards Ingolf Dahl Sweden -----Original Message----- From: Simon Pearce [mailto:Simon.Pearce(a)nottingham.ac.uk] Sent: den 7 maj 2010 12:28 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
From: Murray Eisenberg on 8 May 2010 07:08 Use Through. E.g.: Through[{Sin, Cos}[Pi]] {0, -1} f = (2 # &); g = (#^2 &); Through[{f, g}[3]] {6, 9} On 5/7/2010 6:28 AM, Simon Pearce 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 > -- Murray Eisenberg murray(a)math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305
First
|
Prev
|
Pages: 1 2 3 Prev: FrontEnd and bash automatization Next: Easy question, please help to run a function n times |