Let $A$ be a diagonalizable real matrix such as $A^3=A$. Prove that $\mbox{rank}(A) = \mbox{tr}(A^2)$ 
Let $A$ be a diagonalizable real matrix such as $A^3=A$. Prove that $\mbox{rank}(A) = \mbox{tr}(A^2)$.

My way so far:
I know that if $B$ is simillar to $A$ then $B^3=B$. 
$A$ is diagonalizable so there is a diagonal matrix $D=P^{-1}AP$
$rank(A)=rank(D)$ and $trace(A)=trace(D)$
$rank(A^2)=rank(D^2)$ and $trace(A^2)=trace(D^2)$
$rank(A^2)\le rank(A)$
$D^3=D$
But how can I continue from here?
 A: Since $D^3=D$ so the eigenvalues are either $0$ or $-1$ or $1$. If we denote the number of nonzero eigenvalues by $p$ and the dimension of matrix by $n$ then since $A^2$ is equivalent to $D^2$ it has $p$ eigenvalues with value $1$ and $n-p$ zeros. therefore $$tr(A^2)=\sum{\lambda_i}=p$$
also the rank of a matrix is number of nonzero eigenvalues since the zero ones make linear dependence between columns or rows. Therefore $$rank(A)=p$$which leads us to $$rank(A)=tr(A^2)$$
A: One may use the fact that the trace of a projection of a finite-dimensional vector space over a field of characteristic $0$ equals its rank.  Observe that $A^2$ is a projection (or an idempotent linear transformation).  Thus, $\text{rank}(A^2)=\text{trace}(A^2)$.  It is an easy exercise to prove that $\text{rank}(A)=\text{rank}(A^2)$. 
In general, let $n$ and $k$ be nonnegative integers such that $n\geq k$.  For a linear transformation $A$ on a finite-dimensional vector space $V$ over a field of characteristic $0$, the condition $A^{n+k}=A^k$ implies that $\text{rank}(A^k)=\text{trace}(A^n)$.
