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.
$\begingroup$
$\endgroup$
2
$\begingroup$
$\endgroup$
2
is this what you are asking?
exp=3Cos[x]+5Cos[2x]+12Cos[3x];
Coefficient[exp,Cos[x #]]&/@Range@3
{3, 5, 12}
Collect[exp /. Cos[x n_.] -> x^n, x]/. x^n_. -> Cos[n x]
. $\endgroup$ – Somos Apr 7 '19 at 2:29