Re: How to extract functions from a list and call them with any argument?
- To: mathgroup at smc.vnet.net
- Subject: [mg65929] Re: [mg65919] How to extract functions from a list and call them with any argument?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Mon, 24 Apr 2006 06:01:23 -0400 (EDT)
- References: <200604231017.GAA11838@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 23 Apr 2006, at 19:17, dmp55 at sympatico.ca wrote: > I have a list of functions, i.e., > > funclist = {Sin[x], x^3, Cos[x]^2}. > > I can extract each of the functions, like > > In[1]:=funclist[[1]] > Out[1]=Sin[x] > > , but I don't know how to call these functions with a general > argument. > If I type > > In[2]:=funclist[t][[1]] > > , I get > > Out[2]=t > > instead of Sin[t] (or t^3 or Cos[t]^2) > > What I would like is to extract a function from the list and to > call it > with any argument, as it was defined in a standard way: > f[x_]:=Sin[x]. > > Dom > It would be much better if your list was really a list of functions, i.e. funcs = {Sin, #1^3 & , Cos}; funcs[[1]][Pi] 0 funcs[[2]][3] 27 This suggests a solution to your problem: just convert your list of expressions into a list of functions. exprs = {Sin[x], Cos[x], x^2}; funcs = Function /@ (exprs /. x -> #1) {Sin[#1] & , Cos[#1] & , #1^2 & } now you can proceed as above, e.g. funcs[[1]][Pi] 0 funcs[[3]][4] 16 Andrzej Kozlowski Tokyo, Japan
- References:
- How to extract functions from a list and call them with any argument?
- From: dmp55@sympatico.ca
- How to extract functions from a list and call them with any argument?