### How to determine the coefficients of a polynomial when they depend on variables?

Suppose that EXP2 is a polynomial expression in the variables X_1,X_2,...,X_n with coefficients that depend on the variables B_1,B_2,...,B_n. Suppose EXP1 is an expression equal to EXP2, but scrambled.

Given EXP1, how can I automatically obtain EXP2?

For example, if I have

~~[code]
a=var('a')
b=var('b')
c=var('c')
d=var('d')
e=var('e')
~~a=var('a')

b=var('b')

c=var('c')

d=var('d')

e=var('e')

eqn=a==b*d^2+c*e^2+e*(b+2*~~c*d),
[\code]~~c*d),

how can I automatically rewrite a as a polynomial in b and c, so that I get an equation that looks like

a==b*(d^2+e)+c*(e^2+2*e*d)?