Definition function with variable in name

Click For Summary

Discussion Overview

The discussion revolves around creating functions in a loop with variable names in a programming context, specifically focusing on how to define these functions with a numerical suffix based on the loop index. The conversation includes technical solutions and hypothetical considerations regarding function variables.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant seeks advice on defining functions with names that include a variable index, specifically using a loop to create multiple functions.
  • Another participant suggests using a notation like f[1]=Interpolation[data1] as a solution for defining functions without variable names.
  • A later reply mentions the use of a delayed set for defining functions with a variable, proposing the syntax f[i][x_]:= ...
  • There is a suggestion that using immediate set on the function name might be a better approach, although this is not universally recommended.

Areas of Agreement / Disagreement

Participants express differing views on the best method for defining functions with variable names, with some advocating for immediate set and others for delayed set. The discussion does not reach a consensus on the optimal approach.

Contextual Notes

Some participants note that using delayed set may not be the best practice, but the discussion does not clarify the implications of this choice or any potential limitations.

Vrbic
Messages
400
Reaction score
18
Hello,
I would like to create few functions in loop by Table (not ordinary change a parameter, but different numerical solution of problem). The names should differ by "number"of the step, let's say "i", in the name. I.e. I would like to get set of function f"i"={f1=Interpolation[data], f2=Interpolation[data1],...}
Can you advise me how to write the definition of function with variable "i" in name?

Thank you for a help.
 
Physics news on Phys.org
Just use f[1]=Interpolation[data1] etc.
 
Dale said:
Just use f[1]=Interpolation[data1] etc.
Thank you, very clever solution :-)
And just hypothetically, if I would like to specify a variable of the function. I tried f[x_]=..., but it doesn't work.
 
Vrbic said:
Thank you, very clever solution :-)
And just hypothetically, if I would like to specify a variable of the function. I tried f[x_]=..., but it doesn't work.
You would need to use delayed set for that
Code:
 f[i][x_]:= ...

But I wouldn't recommend that. I would use immediate set on the function name only
 
Last edited:
Dale said:
You would need to use delayed set for that
Code:
 f[i][x_]:= ...

But I wouldn't recommend that. I would use immediate set on the function name only
Haaa it works :-)
Thank you very much.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K