# Revision history [back]

Here is an ad-hoc quick solution, hope it works for the special purpose needed:

def get_trigonometric_coefficients( A, x ):
"""A is the given trigonometric expression in a variable x.
We extract the corresponding coefficients in sin(x)^k * cos(x)^n
for possible k, n.
"""
data_DIC = {}
for op in A.operands():
k, n = 0, 0
# identify the type of the trigonometric expression appeared
factors = op.factor_list()
coeff   = 1
for f, mul in factors:
if f == sin(x):
k += mul
elif f == cos(x):
n += mul
else:
coeff *= f^mul
if (k,n) in data_DIC:
data_DIC[ (k,n) ] += coeff
else:
data_DIC[ (k,n) ]  = coeff
# print op, coeff, f, mul, '\n'
keys = data_DIC.keys()
keys . sort()
return [ ( data_DIC[key], key ) for key in keys ]

# test
var( 'b1 b2 q x b3 b4 b5' );
A = b4*cos(x)^4 + (b2 + q)*cos(x)*sin(x)^2 + (b3/b1 + 6)*cos(x)*sin(x) + (b1 + 2)*sin(x) + b5
print get_trigonometric_coefficients( A, x )
print get_trigonometric_coefficients( A.expand(), x )


Results:

[(b5, (0, 0)), (b4, (0, 4)), (b1 + 2, (1, 0)), ((6*b1 + b3)/b1, (1, 1)), (b2 + q, (2, 1))]
[(b5, (0, 0)), (b4, (0, 4)), (b1 + 2, (1, 0)), (b3/b1 + 6, (1, 1)), (b2 + q, (2, 1))]