# 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_m. Suppose EXP1 is an expression equal to EXP2, but scrambled.

Given EXP1, how can I automatically obtain EXP2?

For example, if I have

a=var('a')

b=var('b')

c=var('c')

d=var('d')

e=var('e')

eqn=a==bd^2+ce^2+e(b+2c*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+2ed)?

edit retag close merge delete

I'm not sure how to display the asterisks as literal asterisks...

To display code inline, put it in between backquotes. To display blocks of code, indent code by 4 spaces.

Try it: you can edit your question and make it look nicer!

Sort by » oldest newest most voted

The best way to work with variables b_i and x_i as you want is to define multivariate polynomial rings Rb and Rx and inject variables. Then any polynomial expression in the b_i's and x_i's will be transformed as a polynomial in the x_i's with coefficients in polynomials in the b_i's.

sage: Rb = PolynomialRing(ZZ,10,name='b')
sage: Rx = PolynomialRing(Rb,10,name='x')

sage: Rb.inject_variables()
Defining b0, b1, b2, b3, b4, b5, b6, b7, b8, b9
sage: Rx.inject_variables()
Defining x0, x1, x2, x3, x4, x5, x6, x7, x8, x9

sage: b1 * (x2 + x3) + b2 * (x3 + x8)
b1*x2 + (b1 + b2)*x3 + b2*x8


For your baby example, you could work in the symbolic ring and just ask coefficients,

sage: var('b c d e')
(b, c, d, e)
sage: a = b*d^2+c*e^2+e*(b+2*c*d)
sage: a
b*d^2 + c*e^2 + (2*c*d + b)*e
sage: a.coefficient(b)
d^2 + e
sage: a.coefficient(c)
2*d*e + e^2


or you could mix the two in the following way.

sage: S.<d,e> = PolynomialRing(QQ)
sage: R.<b,c> = PolynomialRing(S)
sage: R
Multivariate Polynomial Ring in b, c over Multivariate Polynomial Ring in d, e over Rational Field
sage: var('b c d e')
(b, c, d, e)
sage: a = b*d^2+c*e^2+e*(b+2*c*d)
sage: a
b*d^2 + c*e^2 + (2*c*d + b)*e
sage: R(a)
(d^2 + e)*b + (2*d*e + e^2)*c


But as a rule, it is best to avoid the symbolic ring altogether, whenever possible. And the way your main question is formulated, the most sensible approach is the polynomial rings one.

more

Thank you very much! This answer provides a good solution both to my general question and a simple solution to my specific problem (since in my case I'm really just dealing with a scalar linear function of one set of variables with coefficients depending on another set of variables).