# Revision history [back]

You can use PolynomialRing in the following way.

sage: R.<d,e> = PolynomialRing(QQ)
sage: S.<b,c> = PolynomialRing(R)
sage: S
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: S(a)
(d^2 + e)*b + (2*d*e + e^2)*c


To work with variables b_i and x_i as you want, define multivariate polynomial rings Rb and Rx as in the code below, then inject variables as shown. You can use PolynomialRing then enter any polynomial expressions in the following way.b_i's and x_i's, and it will be understood as an element in Rb if no x_i's are involved, and otherwise in Rx, ie, as a polynomial in the x_i's with coefficients in polynomials in the b_i's.

sage: R.<d,e> Rb = PolynomialRing(QQ)
PolynomialRing(ZZ,10,name='b')
sage: S.<b,c> Rx = PolynomialRing(R)
PolynomialRing(Rb,10,name='x')

sage: S
Multivariate Polynomial Ring in b, c over Multivariate Polynomial Ring in d, e over Rational Field
Rb.inject_variables()
Defining b0, b1, b2, b3, b4, b5, b6, b7, b8, b9
sage: var('b c d e')
(b, c, d, e)
Rx.inject_variables()
Defining x0, x1, x2, x3, x4, x5, x6, x7, x8, x9

sage: a = b*d^2+c*e^2+e*(b+2*c*d)
sage: a
b*d^2 b1 * (x2 + c*e^2 x3) + (2*c*d b2 * (x3 + b)*e
sage: S(a)
(d^2 x8)
b1*x2 + e)*b (b1 + (2*d*e b2)*x3 + e^2)*c
b2*x8


To The best way to work with variables b_i and x_i as you want, want is to define multivariate polynomial rings Rb and Rx as in the code below, then inject variables as shown. You can then enter and inject variables. Then any polynomial expressions expression in the b_i's and x_i's, and it x_i's will be understood transformed as an element in Rb if no x_i's are involved, and otherwise in Rx, ie, as a polynomial 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.