Examples of non-continuous, non-piecewise-constant, idempotent map?
Pick an arbitrary $A \subset \mathbb R$. Pick an arbitrary function $g :\mathbb R \backslash A \to A$.
Define $$ f(x)= \begin{align} \begin{cases} x &\text{if $x\in A$}\,, \\ g(x) &\text{otherwise}\,. \end{cases} \end{align}$$
Then $f \circ f=f$.
Conversely, if $f \circ f=f$ then $A= f(\mathbb R)$ together with $g(x)=f(x) \forall x \notin A$ produce $f$.
How about \begin{align} f(x)=\begin{cases} 0 &\text{if $x\in\mathbb{Q}$}\,, \\ x &\text{otherwise}\,. \end{cases} \end{align} I'm trying to think of an injective example.
Edit: The only injective function of this kind is $f(x)=x$. If $f$ is injective and $f(x)=y\neq x$, then $f(y)=y$ but $f(y)\neq f(x)$, a contradiction.
This a “frame challenge” answer — it seems the restrictions in the question are partly motivated by a misconception, and so this doesn’t answer the question as written, but may give another answer to its original motivation. The question says “If $f : \newcommand{\R}{\mathbb{R}}\R \to \R$ and we require continuity, it seems that we basically get the identity function”, and it seems this is the motivation for including “non-continuous” in the question.
However, continuous idempotents don’t really have to be close to the identity function, except to the same extent that all idempotents do. The question links the claim to a nice answer which shows that:
- If $f:\R \to \R$ is continuous and idempotent then $I=f(\R)$ is a closed interval and $f(x)=x$ for all $x\in I$.
- If $f$ is also differentiable and nonconstant, then $I=\R$, i.e., $f(x)=x$ for all $x\in\mathbb R$.
which is true, but a little misleading — regrouping makes the dependency of the conditions clearer:
- Any idempotent map $f$ on any set $X$ acts as the identity on its image $f(X) \subseteq X$.
- The image of any continuous idempotent $f:\R \to \R$ is a closed interval.
- The image of any differentiable idempotent $f:\R \to \R$ is either a point (i.e. $f$ is constant), or the whole of $\R$ (in which case by (1), $f$ is the identity function).
That is, all idempotents are somewhat like the identity; and non-constant differentiable idempotents are precisely the identity; but continuity doesn’t force the idempotent to be any more identity-like than it already had to be. So if the original motivation was partly just “interesting, non-trivial examples of idempotents”, then there’s no need to exclude continuous ones.
A nice intuitive continuous example is the “absolute value” function, $x \mapsto \left|x\right|$.
Another nice one is the function $ x \mapsto \left\{ \begin{array}{lr}x & -1 \leq x \leq 1 \\ \frac{1}{x} & \text{otherwise}\end{array}\right. $
(Graph from WolframAlpha)
N.S.’s answer gives a general explanation: an idempotent function on $\mathbb{R}$ can have an arbitrary subset $S$ of $\mathbb{R}$ as its image (and on $S$ it must act as the identity), and then on the rest of $\mathbb{R}$, it can be an arbitrary function $\mathbb{R} \setminus S \to S$. So for continuous examples, take $S$ to be any closed interval, and then $f$ on $\R \setminus S$ (which must be the union of 0, 1, or 2 half-infinite open intervals) can be any continuous function into $S$ that fixes the endpoints of $S$.