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.

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[[1]]=x^2
f[[2]]=x^3
f[[3]]=f[[1]]*f[[2]]

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.

share|improve this question
    
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) –  Pinguin Dirk Jan 27 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 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 at 17:18
    
@PinguinDirk I've rephrased the question, can you have a second look? –  Pygmalion Jan 27 at 17:22
1  
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 at 17:46

4 Answers 4

up vote 2 down vote accepted

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

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

And then for example:

ff[[1]][2]
ff[[2]][2]
ff[[3]][2]
ff[[4]][2]

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!

share|improve this answer
    
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. –  Pygmalion Jan 29 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[[3]]
9

Or you can define a real function:

g[x_]:=x^2

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
share|improve this answer
    
I want to have list of functions which I want to combine together. E.g. f[[1]]=x^2, f[[2]]=x^3 and a*f[[1]]+b*f[[2]]=a*x^2+b*x^3 –  Pygmalion Jan 27 at 17:03
    
See if the update comes closer to what you are looking to do. –  bill s Jan 27 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 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[[3]][π/6]

which produces

π^2/36

share|improve this answer

Not sure if this is what you want:

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

f[2][x]
x^3
f[2][2]
8
share|improve this answer

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.