MathGroup Archive 2006

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

Search the Archive

Re: How to extract functions from a list and call them with any argument?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65950] Re: How to extract functions from a list and call them with any argument?
  • From: albert <awnl at arcor.de>
  • Date: Mon, 24 Apr 2006 06:02:05 -0400 (EDT)
  • References: <e2fkto$bp7$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

> I have a list of functions, i.e.,
> 
> funclist = {Sin[x], x^3, Cos[x]^2}.
> 

actually I would call this a list of expressions...

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

This is because these are expressions, not functions, so one way to get what
you want would be:

In[6]:= funclist[[1]]/.x->t

Out[6]= Sin[t]

It extracts the first expression and replaces x by t. In such cases it is
often a better idea to really work with functions, which can be defined in
various ways:

funclist2 = {Sin,#^3&,Function[{x},Cos[x]^2]}

then>

In[9]:= funclist2[[2]][t]

         3
Out[9]= t

does what you probably want to achieve, note that you first extract the part
you want and then append the argument. If for some reason you want it the
other way round you could define funclist as a function that returns a list
of expressions:

funclist3 = Function[{x},{Sin[x], x^3, Cos[x]^2}]

In[11]:= funclist3[t][[3]]

               2
Out[11]= Cos[t]

Anyway, for almost any application I can think of I would recommend
funclist2...

Note that for any function that has a name, as Sin, it is good enough to
just use that name without an argument, for functions with no names
mathematica supplies pure functions in the long form Function[{args},body],
in short notation #n are the arguments and the function expression is
terminated with a &. Look it up in the online help to learn more, which is
a good idea anyway...

hth

albert


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


  • Prev by Date: Re: matrix operations -- shared data vs copied
  • Next by Date: Re: Where do I put my own add-on packages?
  • Previous by thread: Re: How to extract functions from a list and call them with any argument?
  • Next by thread: matrix operations -- shared data vs copied