Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Can anyone tell me, how to create a list of functions? I've tried the following code, but it doesn't work.

For[i = 0, i < 5, i++,
{
  functionList[[i]][t_]:=(t*i*i)
}]

functionList[[3]][10]

Of course, this construction is just an example.

share|improve this question
    
Something like this? Through[{f, g, h}[x]] –  David Carraher May 17 '13 at 14:27
add comment

4 Answers

up vote 5 down vote accepted

Perhaps this is what you want?

functionList = Table[With[{i = i}, #*i*i &], {i, 0, 4}]

functionList[[3]][10]
{#1 0 0 &, #1 1 1 &, #1 2 2 &, #1 3 3 &, #1 4 4 &}

40

The With is used to get the values inside Function, which has the HoldAll attribute, as described in: Using pure functions in Table


The method above is more general and likely easier to read but for completeness here is another method, in this specific case notably more concise:

Array[# /. i_ :> (#*i*i &) &, 4]
{#1 1 1 &, #1 2 2 &, #1 3 3 &, #1 4 4 &}

Note that there are two different Functions in this code. The replacement rule is used in an inverted way that I have been calling the "injector pattern," and although destructuring is not used here this is similar in form to what I proposed early in my answer to Mathematica Destructuring.

share|improve this answer
add comment

Something like this?

functionList1 = Table[Evaluate[#*i*i] &, {i, 0, 4}]
functionList2 = Function[x, x^2 # &] /@ Range[0, 4]

functionList1[[3]]@10
functionList2[[3]]@10

(*
{0 &, #1 &, 4 #1 &, 9 #1 &, 16 #1 &}
{0^2 #1 &, 1^2 #1 &, 2^2 #1 &, 3^2 #1 &, 4^2 #1 &}
40
40
*)
share|improve this answer
add comment

Here's an even simpler way to create a "list" of functions, just define the function to have two arguments. To follow your example:

f[i_, t_] := t i^2

So now if you want the 5th function, it's

 f[5,t]

which gives 25 t. Or you can evaluate it at any point:

f[5,7]

to get 175. If you wanted f to behave more like a proper list so as to accept only integer-valued $i$, then you can force this by restricting the domain of $f$. For example:

f[i_?IntegerQ, t_] := t i^2

This $f$ will remain unevaluated for noninteger $i$.

share|improve this answer
1  
Of course, one could also do f[i_][t_] := t i^2, which allows for nice constructions like f[2] /@ Range[5]. –  J. M. May 18 '13 at 5:01
    
Yes... it is kind of hard to tell what the OP was looking for. –  bill s May 18 '13 at 5:07
add comment

Not sure exactly what you're looking for, but perhaps one of these steps will help.

(1) Create a list of functions you wish to apply:

fcnlist = {Sin, Cos, Tan}

(2) Now define myfunc using a pure function which applies the selected function from fcnlist to an argument x of your choice:

myfunc[f_, x_] := fcnlist[[#1]] /@ {#2} &[f, x]

(3) To generate a list of results with Table:

Table[myfunc[i, π/6], {i, 1, 3}]
(* {{1/2}, {Sqrt[3]/2}, {1/Sqrt[3]}} *)

If you specifically want the function to be t i^2, then you simply generate a list of functions as long as you like:

fcnlist = Table[# i^2 &, {i, 1, 5}]
(* {#1 i^2 &, #1 i^2 &, #1 i^2 &, #1 i^2 &, #1 i^2 &} *)

When you apply this fcnlist as above with myfunc, you can generate a list of results as long as you wish (in this case 3 results again):

Table[myfunc[i, t], {i, 1, 3}]
(* {{t}, {4 t}, {9 t}} *)

Table[myfunc[i, 5], {i, 1, 3}]
(* {{5}, {20}, {45}} *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.