I have two functions $f,g:\mathbb{R}^2 \to \mathbb{R}^2$ and I define a third one $h:\mathbb{R}^2 \to \mathbb{R}^2$ as the composition $$h(x,y) = g(f(x,y))$$ I'm trying to get this function into Mathematica
as a composition because I still want the functions $f,g$ and I don't want to have to manually update $h$ when I change $f$ or $g$.
So here is what I'm currently doing :
1. Define f[x_, y_] := {E^x + y, Sin[2 x]}
2. Define g[x_, y_] := {2 x + Cos[y], E^(x + y)}
3. Try the obvious definition of h[x_, y_] := g[f[x, y]].
This doesn't seem to work. I'll just get things like h[0, 0]
returning g[{1, 0}]
. I could write $h$ using the projection of $f$ onto the standard basis for $\mathbb{R}^2$ with the Projection function but this seems needlessly cumbersome and adds conjugate statements everywhere. Can anybody point out a better direction for defining $h$ as a composition?
Also I have tried the Mathematica
function Composition
and it behaved the same way.
Answer
Your functions take two arguments as input. Yet they return one argument as output. So the output of f cannot serve as the input of g and vice versa. You can't compose the functions with until the outputs of one function can serve as the input for the other function. Reduce your arguments in f
, g
by placing them in a list.
This appears to work:
f[{x_, y_}] := {E^x + y, Sin[2 x]}
g[{x_, y_}] := {2 x + Cos[y], E^(x + y)}
h[{x_, y_}] := g[f[{x, y}]]
Example
g[f[{2, 3}]]
f[g[{2, 3}]]
Comments
Post a Comment