Mathematica What does f[a] mean in mathematica?

  • Thread starter Thread starter tgt
  • Start date Start date
  • Tags Tags
    Mathematica Mean
AI Thread Summary
In Mathematica, f[a][b] represents the application of a to the function f, followed by the application of b to the resulting function. For instance, if f[a_] is defined as (a + #)&, then f[3] produces a function that adds 3 to its argument. The discussion also covers how to index functions, suggesting the use of Subscript to create indexed functions like Subscript[g, i][x]. An alternative method involves defining a pattern-matching function that returns specific functions based on the index. Overall, the conversation emphasizes the flexibility of function definitions and indexing in Mathematica.
tgt
Messages
519
Reaction score
2
What does f[a] mean in mathematica?

What does f[a] mean in mathematica?
 
Physics news on Phys.org


It applies a to the function f, and then b to the resulting function.
For example, you could do something like
f[a_] := (a + #)&
then
f[3] would give the function (3 + #)& and applying that to 2 would produce 5.

In more extended notation,
f[a_] := Function[y, a + y]
f[3] => Function[y, 3 + y]
f[3][2] = Function[y, 3 + y][2] = 3 + 2 = 5

Or, equivalently
f[a_, b_] := a + b
f[3, 2]
 


So F[a]=F[a,b]?

What happens if I'd like F[x] to be indexed by i=1,2,3,...

how to do that?
 


No, F[a] is F[a] applied to b, which is (f applied to a) applied to b.
It depends on what F is.

How do you mean "indexed" by i = 1, 2, 3? You would like three functions F[x], G[x], H[x] but call them Fi[x] (i = 1, 2, 3) instead? Or ...
 


CompuChip said:
How do you mean "indexed" by i = 1, 2, 3? You would like three functions F[x], G[x], H[x] but call them Fi[x] (i = 1, 2, 3) instead?


That's right. How to do it?
 
Last edited:


So the simplest form is
Code:
Subscript[g, 1][x_] := x
Subscript[g, 2][x_] := x^2
Subscript[g, 3][x_] := Sin[x]

Plot[{Subscript[g, 1][x], Subscript[g, 2][x], 
  Subscript[g, 3][x]}, {x, -1, 1}]
where you can type g2 using Control + hyphen (-).

An alternative would be
Code:
Subscript[f, i_?(Function[IntegerQ[#] && 1 <= # <= 3])][x_] := 
 Which[i == 1, F[x], i == 2, G[x], i == 3, H[x], True, "This should not occur!"]
Then you can call fi[x] (type with Control + - (hyphen)) for i = 1, 2, 3 and it will call F[x], G[x] or H[x], respectively. The complicated-looking pattern matching is to have it return just fi[x] if i is not 1, 2 or 3.
 


CompuChip said:
So the simplest form is
Code:
Subscript[g, 1][x_] := x
Subscript[g, 2][x_] := x^2
Subscript[g, 3][x_] := Sin[x]

[/QUOTE]

why g?
 


Why not?
I had already used f in the other example. You can call it f if you want, or Apples, or GqoFKdsJF or LoremIpsumDolorem. Just don't use any reserved names (E, N, Sin, ...) or - preferably - things you already defined.
 

Similar threads

Replies
4
Views
3K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
3
Views
2K
Replies
1
Views
2K
Back
Top