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

is this what you are asking?


Coefficient[exp,Cos[x #]]&/@Range@3

{3, 5, 12}

  • $\begingroup$ Yes, thanks this is it! $\endgroup$ – Riccardo Apr 6 '19 at 17:28
  • $\begingroup$ you are welcome! $\endgroup$ – J42161217 Apr 6 '19 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.