Group Abstract Group Abstract

Message Boards Message Boards

Concatenating a function

GROUPS:
Hello everyone,

I need to generate many functions of the same family; A much simplified version of which is something like:

f"i"[x_] =: x+i.

So I want to name the different functions like

f1 [x_] = x+1
f2 [x_] = x+2
...
f50 [x_] = x+50
...
fi [x_] = x+i

Any suggestions on how to create them with the name I want to;
Answer
1 year ago
You can easity do this by either defining
f[x_,i_]:= x+i
orĀ 
f[i_][x_]:=x+i
The latter is an example of how to use Operate function.
POSTED BY: Shenghui Yang
Answer
1 year ago
It is possible to do this, but I strongly recommend against it. Most often, this kind of programming leads to bad design and is difficult to debug. Shenghui's suggestion is the best way to do this kind of thing.

To answer your question, you can define your functions all at once by evaluating:
Do[Symbol["f" <> ToString[i]][x_] := Evaluate[x + i], {i, 1, 50}]
POSTED BY: Sean Clarke
Answer
1 year ago
Thank you both. I used your suggestion Sean!
Answer
1 year ago