7
$\begingroup$

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.

$\endgroup$

4 Answers 4

12
$\begingroup$

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}]] 

compositions

$\endgroup$
0
7
$\begingroup$

When f and g have been defined this way :

f[x_, y_] := {E^x + y, Sin[2 x]} g[x_, y_] := {2 x + Cos[y], E^(x + y)} 

the problem is that g and h being two-argument functions, then g is called as one-argument (i.e. a list returned by f) function h[x_, y_] := g[f[x, y]]. Instead of redefining f and g you can try another definitions of h as a composition of f and g, e.g. h1 using Sequence (and Apply) or h2 using Apply only :

h1[x_, y_] := g[ Sequence @@ f[x, y]] h2[x_, y_] := g @@ f[x, y] 

they both work well :

h1[x, y] == h2[x,y] h1[x, y] 
True 

enter image description here

$\endgroup$
5
$\begingroup$

A little late to the party, but here's a function I wrote to compose arbitrary $\mathbb{R}^M \to \mathbb{R}^P$ functions with $\mathbb{R}^P \to \mathbb{R}^N$ functions:

Clear[multiDimComposition] multiDimComposition[flst__]:= With[{fcns = Reverse@List[flst]}, Fold[#2[ Sequence @@ #1 ]&, First[fcns][##], Rest[fcns]]& ] 

Here's a few examples

Clear[f, g] f[x_, y_] := Sin[2 \[Pi] x y^2] g[s_] := {s, s^3} multiDimComposition[f, g][s] (* Sin[2 Pi s^7] *) 

The reason I created it was to specify arbitrary paths in multidimensional functions, as follows

GraphicsRow[{Show[ DensityPlot[f[x, y], {x, -1, 1}, {y, -1, 1}], ParametricPlot[g[s], {s, -1, 1}, PlotStyle -> Black] ], Plot[multiDimComposition[f, g][s], {s, -1, 1}]}] 

enter image description here

Here's the transformation to spherical coordinates:

Clear[f, g] f[x_, y_, z_] := Exp[Sqrt[x^2 + y^2 + z^2]] g[r_, t_, f_] := {r Sin[t] Cos[f], r Sin[t] Sin[f], r Cos[t]} multiDimComposition[f, g][Rho, Theta, Phi] // Simplify[#, Rho > 0] & (* E^Rho *) 

There is a flaw, though, as written the functions must have downvalues, or it won't work:

multiDimComposition[f, {s, s^2}][s] (* f[s] *) 
$\endgroup$
0
$\begingroup$

You can use Composition directly:

f[x_, y_] := {E^x + y, Sin[2 x]} g[x_, y_] := {2 x + Cos[y], E^(x + y)} h = Apply[g] @* f; 

Then:

h[x, y] h[2, 3] 

{2 (E^x + y) + Cos[Sin[2 x]], E^(E^x + y + Sin[2 x])}

{2 (3 + E^2) + Cos[Sin[4]], E^(3 + E^2 + Sin[4])}

in agreement with the other answers.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.