1
$\begingroup$

If I type:

Coefficient[Cos[x + b], Cos[x]]

I get zero.

But I would like Mathematica to apply:

$$\cos(x+b)=\cos x\cos b-\sin x\sin b$$

Therefore the coefficient of $\cos x$ should be $\cos b$.

How do I fix this?

Edit:

The solution Coefficient[TrigExpand@f[x],Cos[x]] has been proposed, but this wouldn't work for expressions like $\sin(a+3x)$, in fact:

In[84]:= TrigExpand@Sin[a + 3 x]

Out[84]= Cos[x]^3 Sin[a] + 3 Cos[a] Cos[x]^2 Sin[x] - 
         3 Cos[x] Sin[a] Sin[x]^2 - Cos[a] Sin[x]^3

And therefore this would give me a wrong coefficient:

In[86]:= Coefficient[TrigExpand@Sin[a + 3 x], Cos[x]]

Out[86]= -3 Sin[a] Sin[x]^2

Because $\sin x$ is not a constant, and for what I am concerned with, it cannot be the coefficient of $\cos x$. Therefore, for all the expressions of the type $\cos(n x+ a)$ I am expecting to find zero, except when $n=1$.

$\endgroup$
4
  • $\begingroup$ This is not exactly what I think would work, I will explain it in the edit :) $\endgroup$ Commented Dec 8, 2015 at 10:31
  • $\begingroup$ @Kuba It would be zero. $\endgroup$ Commented Dec 8, 2015 at 10:46
  • $\begingroup$ @Kuba for all the expressions of the type $\cos(nx+a)$ I am expecting to find zero, except when $n=1$ $\endgroup$ Commented Dec 8, 2015 at 10:49
  • $\begingroup$ These expressions do not have unique representations as polynomials in {sin(x),cos(x)}. This of course is due to the fundamental trig identity. A possible way around this would be to obtain coefficients in terms of both trig terms. $\endgroup$ Commented Dec 8, 2015 at 14:58

3 Answers 3

2
$\begingroup$

Ok, here is another approach:

ClearAll[findCoeff];
findCoeff[expr_, termToIsolate_] := Module[{findCoeffIn},
  findCoeffIn[termToIsolate] = 1;
  findCoeffIn[exprIn_Plus, OptionsPattern[]] := findCoeffIn /@ exprIn;
  findCoeffIn[Times[lterms___, termToIsolate, rterms___]] := 
   If[FreeQ[#, x], #, 0] &@Times[lterms, rterms];
  findCoeffIn[exprIn : (Cos[_] | Sin[_] | Tan[_] | Cot[_]), 
    OptionsPattern[]] := findCoeffIn[TrigExpand[exprIn]];
  findCoeffIn[_] := 0;
  findCoeffIn[expr]
  ]

This seems to work on all the cases that I tried. Use it like this:

In[1]:= findCoeff[Cos[2 x] + Cos[x + a] + 2, Cos[x]]
Out[1]= Cos[a]

In[2]:= findCoeff[2 Cos[x], Cos[x]]
Out[2]= 2

In[3]:= findCoeff[Cos[x + a] + Sin[2 x] + Cos[x], Cos[x]]
Out[3]= 1 + Cos[a]

In[4]:= findCoeff[Cos[2 x], Cos[2 x]]
Out[4]= 1

Something like findCoeff[Sin[2 x+a],Cos[2 x]] will still not work as you want it to, though. One probably has to fiddle with FourierCosCoefficient to do that. In fact, I'm not even sure that the problem is well stated: consider for example Sin[2x]. How should it expand? FourierCosCoefficient[Sin[2 x],x ,1] gives the answer $8/3\pi$, while findCoeff[Sin[2 x],Cos[x]] gives 0. While the former result answers a well defined question, what exactly are the rules according to which you want the latter result?

Old

Here is a slightly improved version of Kuba's idea:

Options[findCoeff] = {termToIsolate -> Cos[x], 
   independentVariable -> x};
findCoeff[expr_, OptionsPattern[]] := Total@Cases[
    TrigExpand[expr],
    cl___ OptionValue@termToIsolate cr___ :> 
     cl cr /; FreeQ[cl cr, OptionValue@independentVariable]
    ] /. {} -> 0

Examples:

In[1]:= findCoeff[Sin[2 x] + 2 Cos[x] + Cos[x + a]]

Out[1]= 2 + Cos[a]

In[2]:= findCoeff[Cos[y + x] + Sin[x], termToIsolate -> Cos[y], 
 independentVariable -> y]

Out[2]= Cos[x]

I'm not sure it would work on more complicated cases though.

$\endgroup$
3
  • $\begingroup$ findCoeff[Sin[2 x] 2 + 1 Cos[x]] gives 0 $\endgroup$ Commented Dec 8, 2015 at 13:09
  • $\begingroup$ Also, I cannot choose Sin[2 x] as a termToIsolate $\endgroup$ Commented Dec 8, 2015 at 13:11
  • $\begingroup$ @usumdelphini see the edit $\endgroup$
    – glS
    Commented Dec 8, 2015 at 14:02
2
$\begingroup$

Does it fit your needs?

Cases[
  TrigExpand@Sin[a + x],
  Cos[x] coeff__ :> coeff /; FreeQ[coeff, x]
  ] /. {} -> 0

Old:

Not sure if I got your point in general but this should do: (no it shouldn't really)

FourierCosCoefficient[Cos[x + b], x, 1]

FourierCosCoefficient[Sin[a + 3 x], x, 1]
Cos[b]

0
$\endgroup$
7
  • $\begingroup$ This does not seem to work for Cases[TrigExpand@(Sin[a + x] + Sin[2 x]), Cos[x] coeff__ :> coeff /; FreeQ[coeff, x]] /. {} -> 0 $\endgroup$ Commented Dec 8, 2015 at 12:00
  • $\begingroup$ @usumdelphini it gives Sin[a] which is expected since there is Cos[x] Sin[a], if not define what is coefficient. $\endgroup$
    – Kuba
    Commented Dec 8, 2015 at 12:07
  • $\begingroup$ That's true, sorry. But it does not work for Cases[TrigExpand@(((-C1 + C2) Sin[x])/a), Sin[x] coeff__ :> coeff /; FreeQ[coeff, x]] /. {} -> 0 $\endgroup$ Commented Dec 8, 2015 at 12:22
  • $\begingroup$ @usumdelphini modifying the code like this Cases[ TrigExpand[Sin[a + x] + Sin[2 x]], cl___ Cos[x] cr___ :> cl cr /; FreeQ[cl cr, x] ] /. {} -> 0 works in your cases. It would probably fail in more complicated ones, though $\endgroup$
    – glS
    Commented Dec 8, 2015 at 12:29
  • 1
    $\begingroup$ @usumdelphini if you expand Cos[2x] you cannot then ask to isolate Cos[2x]. You should probably explain better what exactly do you need this function for $\endgroup$
    – glS
    Commented Dec 8, 2015 at 13:08
1
$\begingroup$

EDIT: Ok, now this answer is almost the same as Kuba's...

ORIGINAL POST:

I think Kuba's answer is preferable, but you could also do this:

coeff[expr_, var_] := Boole[FreeQ[#,x]]*#&@
  Coefficient[TrigExpand@expr, var]

Then,

coeff[Sin[a + 3 x], Cos[x]]

0

and

coeff[Cos[a+x],Cos[x]]

Cos[a]

$\endgroup$
2
  • $\begingroup$ Thanks so much. Is there a way to extend it to the use of $\sin(2 x)$ as var for example? $\endgroup$ Commented Dec 8, 2015 at 11:48
  • $\begingroup$ Now it does not work for coeff[Cos[a + x] + Sin[2 x], Cos[x]] $\endgroup$ Commented Dec 8, 2015 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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