What steps are taken to make this complex expression equal this? How would you show that $$\sum_{n=1}^{\infty}p^n\cos(nx)=\frac{1}{2}\left(\frac{1-p^2}{1-2p\cos(x)+p^2}-1\right)$$ when $p$ is positive, real, and $p<1$?

Since $0<p<1$, we have
\begin{eqnarray}
\sum_{n=1}^\infty p^n\cos(nx)&=&\Re\sum_{n=1}^\infty p^ne^{inx}=\Re\sum_{n=1}^\infty(pe^{ix})^n=\Re\frac{pe^{ix}}{1-pe^{ix}}=\Re\frac{pe^{ix}(1-pe^{-ix})}{|1-pe^{ix}|^2}\\
&=&p\Re\frac{-p+\cos x+i\sin x}{|1-p\cos x-ip\sin x|^2}=p\frac{-p+\cos x}{(1-p\cos x)^2+p^2\sin^2x}\\
&=&p\frac{\cos x-p}{1-2p\cos x+p^2\cos^2x+p^2\sin^2x}=\frac{p(\cos x-p)}{1+p^2-2p\cos x}\\
&=&\frac12\left(\frac{1-p^2}{1-2p\cos x+p^2}-1\right).
\end{eqnarray}
