# Collect Fourier series terms

Is there a way to make mathematica automatically collect terms like $$\mathrm{Cos}[n\ x]$$ with $$n=1,2,3...$$? I have a long expression coming out of a Fourier series where I see a lot of $$Cos[x],Cos[2 x],Cos[3 x]$$ and so on, but Simplify is very inefficient. I would like mathematica to automatically collect all coefficients of the various Cos[n x] without having to do it manually for each one of them.

• can you show your initial code, so we try to help! – Alrubaie Apr 6 '19 at 17:00
• Try Collect[exp /. Cos[x n_.] -> x^n, x]/. x^n_. -> Cos[n x]. – Somos Apr 7 '19 at 2:29

exp=3Cos[x]+5Cos[2x]+12Cos[3x];