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

For example


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

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

  • $\begingroup$ try: f[[1]] = #^2 &(you need a pure function for that, see documentation). Also: check out: ConstantArray[0, 4]:) (fwiw: alternatively: f[[1]] /. x -> 2, but I wouldnt recommend that) $\endgroup$ – Pinguin Dirk Jan 27 '14 at 16:43
  • $\begingroup$ @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? $\endgroup$ – Pygmalion Jan 27 '14 at 17:02
  • $\begingroup$ 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 $\endgroup$ – Pinguin Dirk Jan 27 '14 at 17:18
  • $\begingroup$ @PinguinDirk I've rephrased the question, can you have a second look? $\endgroup$ – Pygmalion Jan 27 '14 at 17:22
  • 1
    $\begingroup$ 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. $\endgroup$ – 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:


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[[3]] = Composition[ff[[1]], ff[[2]]];
ff[[4]] = ff[[1]][ff[[2]][#]] &;

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

And then for example:






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!

| improve this answer | |
  • $\begingroup$ I accept the first solution as an answer. The second solution does not work for my needs. E.g. ff[[3]] = 2*ff[[1]] + 3*ff[[2]] and ff[[3]][2] wouldn't work. $\endgroup$ – 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:


Or you can define a real function:


Now you can ask for g[3] 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
| improve this answer | |
  • $\begingroup$ I want to have list of functions which I want to combine together. E.g. f[[1]]=x^2, f[[2]]=x^3 and af[[1]]+bf[[2]]=ax^2+bx^3 $\endgroup$ – Pygmalion Jan 27 '14 at 17:03
  • $\begingroup$ See if the update comes closer to what you are looking to do. $\endgroup$ – bill s Jan 27 '14 at 17:48
  • $\begingroup$ +1 This is a nice inventive solution. However I like PinguinDirk's solution better, as I want to stick to a real list. Thanks $\endgroup$ – 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


which produces


| improve this answer | |

Not sure if this is what you want:

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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