       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:=funclist[]
> Out=Sin[x]
>
> , but I don't know how to call these functions with a general argument.
> If I type
>
> In:=funclist[t][]
>

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

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

Out= 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:= funclist2[][t]

3
Out= 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:= funclist3[t][]

2
Out= 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
a good idea anyway...

hth

albert

> , I get
>
> Out=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