Re: Applying a list of functions to a single number
- To: mathgroup at smc.vnet.net
- Subject: [mg74538] Re: Applying a list of functions to a single number
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 24 Mar 2007 05:25:39 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <eu1peu$eu1$1@smc.vnet.net>
siewsk at bp.com wrote:
> Applying a list of functions to a single number
> =================================
>
> I have
>
> Square[x_]:=x^2
> Cube[x_]:=x^3
>
> I have tried
>
> In[2]:={Square,Cube} @ 5
>
> Out[2]:= {Square,Cube}[5]
>
> But what I wanted is a list containing
>
> {25,125}
>
> Obviously I'm doing this the wrong way. What is the correct way?
>
>
What you need is the function *Through* [1, 2]. For instance,
In[1]:=
square[x_] := x^2
cube[x_] := x^3
Through[{square, cube}[5]]
Out[3]=
{25, 125}
Note that Square (with a capital S) is already defined in Mathematica.
Indeed, every Mathematica function starts with an uppercase letter (this
is a convention established ages ago by Wolfram Research). To avoid any
clash, it is better to use user-defined names starting with a lowercase
letter.
Regards,
Jean-Marc
[1] http://documents.wolfram.com/mathematica/functions/Through
[2] "2.2.9 Advanced Topic: Working with Operators",
http://documents.wolfram.com/mathematica/book/section-2.2.9