Example $g\circ f=id_A$ but $f\circ g\neq id_B$

Two functions $f:A\rightarrow B$ and $g:B\rightarrow A$. Can someone give me an example where $g\circ f=id_A$ but $f\circ g\neq id_B$?

Solutions Collecting From Web of "Example $g\circ f=id_A$ but $f\circ g\neq id_B$"

Since $g\circ f=\mathit{id}_A$, we see that $g$ is surjective and $f$ is injective.

Since we want $f\circ g\ne\mathit{id}_B$, $f$ has not to be surjective: if it’s also surjective, then it’s bijective and so $g=f^{-1}$ follows from $g\circ f=\mathit{id}_A$ and then $f\circ g=\mathit{id}_B$.

Thus we can try $A=\{0\}$, $B=\{1,2\}$ and $f(0)=1$. Since we need that $g$ is surjective, we must set $g(1)=g(2)=0$.

Now $f\circ g(2)=f(0)=1$.

If you need also $A=B$, then the set must be infinite. Just consider $A=B=\mathbb{N}$ and
n-1 & \text{if $n>0$}\\
0 & \text{if $n=0$}
for all $n$ and $g\circ f$ is the identity. On the other hand
and so $f\circ g$ is not the identity.

Take $A=\mathbb{R}$, $B=\mathbb{R}^2$ and
$$f:\mathbb{R}\to\mathbb{R}^2,\quad f(x)=(x,0)$$
$$g:\mathbb{R}^2\to\mathbb{R},\quad g(x,y)=x.$$

My favorite example has $A=B=\Bbb R$, with $f=\exp$ and $g$ the natural log for positive values of the argument, but $g(x)=17$ for $x\le0$.

Let $A \subset B$, $f$ be inclusion, and $g$ be the identity on $A$ and act in any way on $B \setminus A$. Then $g \circ f$ is the identity but $f \circ g$ cannot map to $B \setminus A$.

A minimal example is $A = \{1\}$, $B = \{1, 2\}$, $f(1) = 1$, $g(1) = 1$, $g(2) = 1$. Then $(g \circ f)(1) = 1$, $(f \circ g)(1) = 1$, $(f \circ g)(2) = 1$.

Take $A=B=l^2$ (this is the set of infinite sequences such that the sum of the squares of the elements is finite).

Let $x=(x_1,x_2,\dots)$, $f(x)=(0,x_1,x_2,\dots)$ and $g(x)=(x_2,x_3,x_4,\dots)$. Then we have $g(f(x))=x$, but $f(g(x))=(0,x_2,x_3,\dots)$.