Writing a function as a sum of its odd and even parts

I have the following question and the solution along with it but I can't get my head around what's been done.

The aim is to write the following function as a sum of even and odd functions:

$h(x) = \begin{cases} 1, & \text{if$x<0$} \\ e^x, & \text{if$ x\geq 0$} \end{cases}$

I am aware that any function $f(x)$ can be written as:

$f(x) = \underbrace{\frac{1}{2}(h(x)+h(-x))}_{f_{even}(x)}+ \underbrace{\frac{1}{2}(h(x)-h(-x))}_{f_{odd}(x)}$

I attempted to do it myself but it was incorrect.

The correct method was to find the following:

We have

$h(-x) = \begin{cases} 1, & \text{if$x>0$} \\ e^{-x}, & \text{if$ x\leq 0$} \end{cases}$

Hence:

$h_{even}(x) = \begin{cases} \frac{1}{2}(1+e^{-x}), & \text{if$x<0$} \\ \frac{1}{2}(1+e^{x}), & \text{if$ x> 0$} \\ \ 1, & \text{ if$x=0$}\end{cases}$

$h_{odd}(x) = \begin{cases} \frac{1}{2}(1-e^{-x}), & \text{if$x<0$} \\ \frac{1}{2}(e^{x}-1), & \text{if$ x> 0$} \\ \ 0, & \text{ if$x=0$}\end{cases}$

My problem is I have no idea how these functions were found.. I'm sure it's as simple as applying the formula I stated above but I think perhaps because they are piece wise functions I am not entirely sure how this happened.

Any explanation is greatly appreciated.

Further Info:

I think I understand the formula but applying it to examples particularly is where I struggle, as in the actual calculation is what I don't understand.

For example, to calculate $h_{even}(x)$ we should do the addition of the following functions, if I am correct:

$\frac{1}{2}h(x) = \begin{cases} \frac{1}{2}, & \text{if$x<0$} \\ \frac{1}{2}e^x, & \text{if$ x\geq 0$} \end{cases}$

$\frac{1}{2}h(-x) = \begin{cases} \frac{1}{2}, & \text{if$x>0$} \\ \frac{1}{2}e^{-x}, & \text{if$ x\leq 0$} \end{cases}$

But my issue is, how do you combine these? How can you deduce that for example $h_{even}(x)=1$, if $x=0$ purely from this addition.

I guess my question is quite simple really as in just how to add piece wise functions when the conditions are different.

Note that in your expression $$f(x) = \underbrace{\frac{1}{2}(h(x)+h(-x))}_{f_{even}(x)}+ \underbrace{\frac{1}{2}(h(x)-h(-x))}_{f_{odd}(x)}$$ $$f(x) = \frac{1}{2}h(x) + \frac{1}{2}h(x) + \frac{1}{2}h(-x)-\frac{1}{2}h(-x)$$ $$f(x) = h(x)$$ This derivation should help you out, as finding $f_{even}$ and $f_{odd}$ is now just a matter of calculation.
• As for your question on how to add piecewise functions. You can split up the bounds further so that you have $x>0$, $x<0$, and $x=0$. Then you can go about adding up the similar parts of the functions. Does that help? – Isaac Browne May 18 '17 at 12:34
$$f(x)=\frac12 \Big(2f(x)\Big)=\frac12 \Big(f(x)+f(x)\Big)=\frac12 \Big(f(x)-f(-x)+f(x)+f(-x)\Big)=\underbrace{\frac12 (f(x)+f(-x))}_{\text{Even function}}+\underbrace{\frac12 (f(x)-f(-x))}_{\text{Odd function}}$$
In your solution $h(x)=f(x)$