Solution (source code)

= Solution

If $Av=\lambda v$ for a nonzero <eigenvector>, then $A^2v=\lambda^2v$. Since $A^2=A$, $\lambda(\lambda-1)=0$: every <eigenvalue> is zero or one. Let $m$ be the number of ones, counted with <algebraic multiplicity>. The <trace> is $m$ and the <determinant> is the product of these <eigenvalues>. Therefore
$$
\boxed{m=n:\quad\det A=1,\ \operatorname{tr}A=n;\qquad0\leq m<n:\quad\det A=0,\ \operatorname{tr}A=m.}
$$
Every output $y=Ax$ is fixed by a second application:
$$
\boxed{Ay=A^2x=Ax=y.}
$$
The <image of a linear map> is thus the <eigenspace> for the <eigenvalue> one. To establish its dimension without assuming <diagonalizability>, decompose every input as
$$
x=Ax+(I-A)x.
$$
The first term lies in the <image of a linear map> $A$ and the second in its <kernel>. Their intersection is zero, because a vector in the image is fixed by $A$ whereas a vector in the kernel is killed by $A$. Thus the space is their <direct sum>, and an adapted <basis> puts $A$ into the form $\operatorname{diag}(I_r,0)$, where $r=\operatorname{rank}A$. Comparing <traces> gives $r=m$:
$$
\boxed{\dim\operatorname{im}A=n\text{ in case (i)},\qquad\dim\operatorname{im}A=m\text{ in case (ii)}.}
$$
This is an <idempotent linear map>, and <trace of an idempotent equals its rank>. If $m=0$, no nonzero output exists. The dimension in the question refers to the image subspace, including its zero vector.