# General method for composition of piecewise defined functions

There is a similarity in questions about composition of functions piecewise defined (see e.g. here, here and here). In these questions the goal is always the same:

Given $f,g$ piecewise defined, compute $f \circ g$.

The aim of this question is to express the mechanism behind these exercises and give a systematic method which apply in a general setting for computing the definition, domaine and range of a composition of functions. For example with $f,g$ defined through respectively $m$ and $n$ different conditions or for compositions of more than two functions.

One more example:

Consider $$f:[-1/2,1] \to ]1,3[:x\mapsto \left\{\begin{array}{l l} e^x & \text{if } 0 < x \leq 1 \\ 4x^2+2 & \text{if } -1/2 < x \leq 0\end{array}\right.$$ and $$g:[-1,2[ \to [0,1]:x\mapsto \left\{\begin{array}{l l}\frac{x}{2} & \text{if } 0 \leq x < 2 \\ x^{32} & \text{if } -1 \leq x <0\end{array}\right. .$$ Let us compute $f \circ g$. We have $0 < g(x) \leq 1$ for every $x \in [-1,2[\setminus\{0\}$ and $g(0)=0$. It follows that $f\circ g(x) = e^{g(x)}$ for every $x \in [-1,2[\setminus\{0\}$ and $f(g(0))=4g(0)^2+2$. So $$f\circ g:[-1,2[ \to ]1,e]:x\mapsto \left\{\begin{array}{l l}e^\frac{x}{2} & \text{if } 0 < x < 2 \\ 2 & \text{if }x = 0\\ e^{x^{32}} & \text{if } -1 \leq x <0 \end{array}\right.$$

Given $$f:Y \to Z:x\mapsto \left\{\begin{array}{l l} f_1(x) & \text{if } x \in S_1 \\ f_2(x) & \text{otherwise} \end{array}\right.$$
$$g:X \to Y:x\mapsto \left\{\begin{array}{l l}g_1(x) & \text{if } x \in S_2 \\ g_2(x) & \text{otherwise} \end{array}\right.$$
We get $$(f \circ g):X \to Z:x\mapsto \left\{\begin{array}{1 1} (f_1 \circ g_1)(x) & \text{if } x \in g_1^{-1}(S_1) \cap S_2 \\ (f_1 \circ g_2)(x) & \text{if } x \in g_2^{-1}(S_1) \setminus S_2 \\ (f_2 \circ g_1)(x) & \text{if } x \in g_1^{-1}(S_1^C) \cap S_2 \\ (f_2 \circ g_2)(x) & \text{otherwise} \end{array}\right.$$