If I type:

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

I get zero.

But I would like Mathematica to apply:

cos(x+b)=cosxcosb−sinxsinb\cos(x+b)=\cos x\cos b-\sin x\sin b

Therefore the coefficient of cosx\cos x should be cosb\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)\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 sinx\sin x is not a constant, and for what I am concerned with, it cannot be the coefficient of cosx\cos x. Therefore, for all the expressions of the type cos(nx+a)\cos(n x+ a) I am expecting to find zero, except when n=1n=1.

=================

This is not exactly what I think would work, I will explain it in the edit 🙂

– usumdelphini

Dec 8 ’15 at 10:31

@Kuba It would be zero.

– usumdelphini

Dec 8 ’15 at 10:46

@Kuba for all the expressions of the type cos(nx+a)\cos(nx+a) I am expecting to find zero, except when n=1n=1

– usumdelphini

Dec 8 ’15 at 10:49

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.

– Daniel Lichtblau

Dec 8 ’15 at 14:58

=================

3 Answers

3

=================

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π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.

findCoeff[Sin[2 x] 2 + 1 Cos[x]] gives 0

– usumdelphini

Dec 8 ’15 at 13:09

Also, I cannot choose Sin[2 x] as a termToIsolate

– usumdelphini

Dec 8 ’15 at 13:11

@usumdelphini see the edit

– glS

Dec 8 ’15 at 14:02

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

This does not seem to work for Cases[TrigExpand@(Sin[a + x] + Sin[2 x]), Cos[x] coeff__ :> coeff /; FreeQ[coeff, x]] /. {} -> 0

– usumdelphini

Dec 8 ’15 at 12:00

@usumdelphini it gives Sin[a] which is expected since there is Cos[x] Sin[a], if not define what is coefficient.

– Kuba

Dec 8 ’15 at 12:07

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

– usumdelphini

Dec 8 ’15 at 12:22

@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

– glS

Dec 8 ’15 at 12:29

1

@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

– glS

Dec 8 ’15 at 13:08

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]

Thanks so much. Is there a way to extend it to the use of sin(2x)\sin(2 x) as var for example?

– usumdelphini

Dec 8 ’15 at 11:48

Now it does not work for coeff[Cos[a + x] + Sin[2 x], Cos[x]]

– usumdelphini

Dec 8 ’15 at 11:58