Evaluate function from a table

I want to define list of functions and also use them for evaluation. But this just doesn't work.

For example

f=Table[0,{i,3}]
f[]=x^2
f[]=x^3
f[]=f[]*f[]

So far, fine, because f[] is in fact x^5.

Let's say I also need value of second function at $x=2$. However f[] does not return 8.

• try: f[] = #^2 &(you need a pure function for that, see documentation). Also: check out: ConstantArray[0, 4]:) (fwiw: alternatively: f[] /. x -> 2, but I wouldnt recommend that) – Pinguin Dirk Jan 27 '14 at 16:43
• @PinguinDirk OK, this is a step forward. How can I combine two pure functions together, e.g. f=Function[{x},x^2] and then g=f*f, I would like to get g=x^4? – Pygmalion Jan 27 '14 at 17:02
• Not sure I understand (also looking at @bills answer). I try: either explicitly compose like for any "normal function", like ff = #^2 & and then ff[ff[x]] respectively: g=ff[ff[#]]& or use Composition explicitly – Pinguin Dirk Jan 27 '14 at 17:18
• @PinguinDirk I've rephrased the question, can you have a second look? – Pygmalion Jan 27 '14 at 17:22
• it has no way of knowing that the function is in x. Afaik, you have to either use Composition or else Replace (a.k.a. /.), as shown above. Sorry don't have the time to write full answer. – Pinguin Dirk Jan 27 '14 at 17:46

As written in the comment, the "functions" you define have to way of knowing that they are in x. So if you want to stick to your approach and have functions in x in the list, all you need to do if you wanted to execute the function is to Replace x by the value, like so:

f[]/.x->5

Simply speaking, this is pretty similar to what a normal function does in Mathematica, see for example at this excellent ressource: (go to Chapter 1.2.3) (thanks to @LeonidShifrin), or see DownValues.

Alternatively, you could use pure functions, as @zentient describes in his answer. To compose to pure functions, you might want to use Composition or explicitly link the functions, like in the following example:

ff = {#^2 &, #^3 &, 0, 0};
ff[] = Composition[ff[], ff[]];
ff[] = ff[][ff[][#]] &;
ff

`{#1^2 &, #1^3 &, Composition[#1^2 &, #1^3 &], ff[ff[][#1]] &}

And then for example:

ff[]
ff[]
ff[]
ff[]

4

8

64

64

By the way: using some prefix/postfix/@-Notation might help clean up the mess with all the square brackets...

I hope this helps and gets you started!

• I accept the first solution as an answer. The second solution does not work for my needs. E.g. ff[] = 2*ff[] + 3*ff[] and ff[] wouldn't work. – Pygmalion Jan 29 '14 at 15:47

In f, you have not defined a function, you have defined a list. You can work with lists:

f = Table[i^2, {i, 4}]

so now you can ask:

f[]
9

Or you can define a real function:

g[x_]:=x^2

Now you can ask for g and get 9. To form a "list" of functions, you can define them as a function of two variables, for instance:

f[1, x_] := x^2;
f[2, x_] := x^3;
f[3, x_] := f[1, x] f[2, x];

So for instance:

a f[1, x] + b f[2, x]
a x^2 + b x^3

If there is a general form, then you can use that to define a complete collection. For example:

g[n_, x_] := x^(n + 1)

Now you have defined g[n,x] for every n. For example:

a g[1, x] + b g[2, x]
a x^2 + b x^3
• I want to have list of functions which I want to combine together. E.g. f[]=x^2, f[]=x^3 and af[]+bf[]=ax^2+bx^3 – Pygmalion Jan 27 '14 at 17:03
• See if the update comes closer to what you are looking to do. – bill s Jan 27 '14 at 17:48
• +1 This is a nice inventive solution. However I like PinguinDirk's solution better, as I want to stick to a real list. Thanks – Pygmalion Jan 29 '14 at 15:42

Create a List of pure functions, for example

myfunctions = {Sin[#] &, Cos[#] &, #^2 &}

Then grab one of your functions, which is a Part of that List, and feed it an argument in the usual way

myfunctions[][π/6]

which produces

π^2/36

Not sure if this is what you want:

f[x_] = x^2
f[x_] = x^3
x^2
x^3
f[i_][x_] := f[i][x] = f[i - 1][x]*f[i - 2][x]

f[x]
x^3
f
8