If $(f ∘ f)$ is differentiable, is $f$ also differentiable?

Question:

If $(f \circ f)$ is differentiable on $\mathbb R$, then $f$ is differentiable on $\mathbb R$.

Is this statement true or false and why?

I have had a look at this question and really can't get my head around it.

I have thought that it is False, because if we let $f(x) = 2$, $(f\circ f)$ can't be defined as $(f(f(2))$ doesn't exist. So the statement would be false as we can't define $(f\circ f)$ so it can't be differentiable on $\mathbb R$.

Is this way of looking at it right or not?


There is a very simple counterexample. Consider $$ f(x)=\begin{cases}2 & x\geq 0\\1 & x<0\end{cases} $$ which is not differentiable (not even continuous) at $0$ but $f\circ f\equiv 2$ is constant, so differentiable on $\mathbb R$.


We can do even better than Mundron Schmidt's counterexample. For example, the Dirichlet function: $$f(x) = \begin{cases}1 & \text{if } x \in \mathbb Q \\ 0 & \text{if } x \notin \mathbb Q\end{cases}$$ is discontinuous (and thus non-differentiable) everywhere on $\mathbb R$, but $f \circ f$ is constant.


A slight modification will also provide a counterexample such that $(f \circ f)' \ne 0$. For example, the function: $$g(x) = \begin{cases}\phantom{+}x & \text{if } x \in \mathbb Q \\ -x & \text{if } x \notin \mathbb Q\end{cases}$$ is discontinuous almost everywhere on $\mathbb R$ (except at the origin), while $g \circ g$ is the identity function (and thus $(g \circ g)' = 1$). Or, for a counterexample that's truly discontinuous everywhere, we could instead pick, say: $$h(x) = \begin{cases}-x & \text{if } x \in \mathbb Q \\ 1/x & \text{if } x \notin \mathbb Q\end{cases}$$ which also satisfies $(h \circ h)(x) = x$.


Your assumption that the statement is false is correct, but your argument is wrong. With $f(x)=2$ you certainly can form $f\circ f$: $$(f\circ f)(x) = f(f(x)) = f(2) = 2$$ Indeed, whenever the range if $f$ is a subset of its domain, $f\circ f$ can be formed.

Also, besides your wrong argument, $f(x)=2$ isn't a valid counterexample, as it is a differentiable function, with $f'(x)=0$. Any valid counterexample to the claim involves a non-differentiable function.

A list of valid counterexamples has already been given by Mundron Schmidt and Ilmari Karonen.