Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers 3

up vote 12 down vote accepted

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

share|improve this answer
    
Thanks, it's working now. –  AvatarOfChronos Oct 22 '12 at 0:37
add comment

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

share|improve this answer
add comment

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] *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.