If $A^2=A$ then prove that $\textrm{tr}(A)=\textrm{rank}(A)$. Let $A\not=I_n$ be an $n\times n$ matrix such that $A^2=A$ , where $I_n$ is the identity matrix of order $n$.  Then prove that ,
(A) $\textrm{tr}(A)=\textrm{rank}(A)$.
(B) $\textrm{rank}(A)+\textrm{rank}(I_n-A)=n$
I found by example that these hold, but I am unable to prove them.
 A: Every vector $x$ can be written as
$$
                    x = (I-A)x + Ax
$$
The vector $x_0 = (I-A)x$ satisfies $Ax_0=0$, while $x_1 = Ax$ satisfies $Ax_1=x_1$. So you can choose a basis of elements
$$
               \{ x_{0,1},x_{0,2},\cdots,x_{0,k}\}\cup\{ x_{1,1},x_{1,2},\cdots,x_{1,n-k} \}
$$
where $A=0$ on the subspace spanned by $\{ x_{0,1},x_{0,2},\cdots,x_{0,k} \}$ and where $A=I$ on the subspace spanned by $\{ x_{1,1},x_{1,2},\cdots,x_{1,n-k}\}$. In this basis, the matrix representation of $A$ has $0$'s in the first $k$ diagonal entries and has $1$'s in the next $n-k$ diagonal positions; all other matrix entries are $0$'s. Once you understand this representation, $(A)$ and $(B)$ become more-or-less obvious.
A: Clearly, the eigenvalues of $A$ are $0$ and $1$. Ultimately, however, you will also need the fact that $A$ is diagonalizable, or something equivalent to this.  What we really need is any way to see that $A$ is similar to the block matrix
$$
D = \pmatrix{I_{r\times r} & 0\\0 & 0}
$$
We can then directly prove the theorem by computing the rank/trace of $D$ and $I - D$.
A: Hint: the $1$-eigenvectors and $0$-eigenvectors form a basis for $A$ (see here), so in particular, $A$ is similar to a diagonal matrix with $0$s and $1$s on the diagonal. Since rank and trace are invariant under change of basis, it suffices to prove the two statements for the diagonal matrix.
A: Since $A^2=A$, one can create the isomorphism
$$V \cong \text{Im } A \oplus \ker A$$
$$ x \mapsto (Ax,(I-A)x).$$
To show that this is an isomorphism is simple. Hence, we have your item $2$.
The decomposition above also shows that the unique eigenvalues are $0$ or $1$. The multiplicity of the eigenvalue $1$ will give the rank of the operator. Since the trace is the sum of the eigenvalues (counted with multiplicity), you have your item $1$.

EDIT: Since this answer was downvoted but not explained, I will assume it is due to the need of clarification with respect to the fact that I am conflating the algebraic multiplicity and the geometric multiplicity:
Both coincide, since $A$ is diagonalizable: it is clear that $A$ restricts to the identity on its image on the decomposition above, and thus any basis $\{v_1,\cdots,v_j\}$ of $\mathrm{Im}~A$ is composed of eigenvectors. It is obvious that any basis $\{v_{j+1},\cdots,v_n\}$ of $\ker A$ is also of eigenvectors, thus $\{v_1,\cdots,v_n\}$ is a basis of $V$ composed of eigenvectors for $A$.
If anything else needs clarification, please feel free to point it out.
A: The given condition $A^{2}=A$ says that the matrix is idempotent and so diagonalizable and hence rank is same as the number of eigen values $1$(with repetition ) which is the same as trace of $A.$ For second part use it $$|rank(A)-rank(B)|\leq rank(A+B)\leq n.$$
