# Revision history [back]

### Should I always use .expand() before using .coefficient(x,n)?

I used the following code in SageMath (Windows binary, version 8.1):

q,x,t,bb_1_1_0,bb_1_1_1,aa_1_1_1,cc_1,eps=var('q x t bb_1_1_0 bb_1_1_1 aa_1_1_1 cc_1 eps')

const1=-(bb_1_1_1*eps*cos(t)*cos(x) + bb_1_1_0*eps*cos(x) + q)*aa_1_1_1*eps*cos(x)*sin(t) - bb_1_1_1*cc_1*eps*cos(x)*sin(t)


Obtaining the coefficients of polynomial with respect to eps: const1.coefficient(eps,1) gives

-(bb_1_1_1*cos(t)*cos(x) + bb_1_1_0*cos(x))*aa_1_1_1*cos(x)*sin(t) - bb_1_1_1*cc_1*cos(x)*sin(t)


but using const1.expand().coefficient(eps,1) gives

-bb_1_1_1*cc_1*cos(x)*sin(t) - aa_1_1_1*q*cos(x)*sin(t)


Clearly the second one is correct. Does this mean that I should always use .expand() before using .coefficient()? In another instance I was forced to use simplify_full() before using coefficient().