1
f2[x_, y_] := x^2 + y^3;
g[t_] := {t^2, 3*t+1}

$g$ returns a list, but $f_2$ is a two variable function.
So, we cannot compose these functions:

f2[g[1]]

f1[v_] := v[[1]]^2 + v[[2]]^3
g[t_] := {t^2, 3*t+1}

We can compose these functions like the following:

f1[g[1]]

But I don't want to use a vector variable function like $f_1$.


Any good idea?

tchappy ha
  • 463
  • 2
  • 7

3 Answers3

3

Does this work for you?

ClearAll[f2, g]

f2[{x_, y_}] := f2[x, y]
f2[x_, y_] := x^2 + y^3;
g[t_] := {t^2, 3*t + 1}

f2[g[1]]
65
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
3

It looks like you've already got several good answers, but I thought I would mention Apply.

f2[x_, y_] := x^2 + y^3
g[t_] := {t^2, 3 t + 1}
f2@@g[1]

65

The head of the result of evaluating g[1] is List, and Apply replaces this with f2. Essentially, List[1, 4] --> f2[1, 4].

MassDefect
  • 10,081
  • 20
  • 30
1

And along the lines of @MassDefect,

f = {#1^2 + #2^3} &
g = {#1^2, 3 #1 + 1} &
f @@ g[1]

Pure functions can be a solution too. Result is {65} as expected.

Coti
  • 31
  • 5