ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 15 Jul 2014 11:25:12 -0500How to determine the coefficients of a polynomial when they depend on variables?http://ask.sagemath.org/question/23409/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==b*d^2+c*e^2+e*(b+2*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)?Sun, 13 Jul 2014 22:58:46 -0500http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/Comment by slelievre for <p>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.</p>
<p>Given EXP1, how can I automatically obtain EXP2?</p>
<p>For example, if I have</p>
<p>a=var('a')</p>
<p>b=var('b')</p>
<p>c=var('c')</p>
<p>d=var('d')</p>
<p>e=var('e')</p>
<p>eqn=a==b<em>d^2+c</em>e^2+e<em>(b+2</em>c*d),</p>
<p>how can I automatically rewrite a as a polynomial in b and c, so that I get an equation that looks like</p>
<p>a==b<em>(d^2+e)+c</em>(e^2+2<em>e</em>d)?</p>
http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23423#post-id-23423Try it: you can edit your question and make it look nicer!Tue, 15 Jul 2014 11:24:36 -0500http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23423#post-id-23423Comment by Zorgoth for <p>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.</p>
<p>Given EXP1, how can I automatically obtain EXP2?</p>
<p>For example, if I have</p>
<p>a=var('a')</p>
<p>b=var('b')</p>
<p>c=var('c')</p>
<p>d=var('d')</p>
<p>e=var('e')</p>
<p>eqn=a==b<em>d^2+c</em>e^2+e<em>(b+2</em>c*d),</p>
<p>how can I automatically rewrite a as a polynomial in b and c, so that I get an equation that looks like</p>
<p>a==b<em>(d^2+e)+c</em>(e^2+2<em>e</em>d)?</p>
http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23422#post-id-23422Thank you!Tue, 15 Jul 2014 11:22:54 -0500http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23422#post-id-23422Comment by slelievre for <p>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.</p>
<p>Given EXP1, how can I automatically obtain EXP2?</p>
<p>For example, if I have</p>
<p>a=var('a')</p>
<p>b=var('b')</p>
<p>c=var('c')</p>
<p>d=var('d')</p>
<p>e=var('e')</p>
<p>eqn=a==b<em>d^2+c</em>e^2+e<em>(b+2</em>c*d),</p>
<p>how can I automatically rewrite a as a polynomial in b and c, so that I get an equation that looks like</p>
<p>a==b<em>(d^2+e)+c</em>(e^2+2<em>e</em>d)?</p>
http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23412#post-id-23412To display code inline, put it in between backquotes. To display blocks of code, indent code by 4 spaces.Mon, 14 Jul 2014 00:15:11 -0500http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23412#post-id-23412Comment by Zorgoth for <p>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.</p>
<p>Given EXP1, how can I automatically obtain EXP2?</p>
<p>For example, if I have</p>
<p>a=var('a')</p>
<p>b=var('b')</p>
<p>c=var('c')</p>
<p>d=var('d')</p>
<p>e=var('e')</p>
<p>eqn=a==b<em>d^2+c</em>e^2+e<em>(b+2</em>c*d),</p>
<p>how can I automatically rewrite a as a polynomial in b and c, so that I get an equation that looks like</p>
<p>a==b<em>(d^2+e)+c</em>(e^2+2<em>e</em>d)?</p>
http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23410#post-id-23410I'm not sure how to display the asterisks as literal asterisks...Sun, 13 Jul 2014 23:02:42 -0500http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23410#post-id-23410Answer by slelievre for <p>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.</p>
<p>Given EXP1, how can I automatically obtain EXP2?</p>
<p>For example, if I have</p>
<p>a=var('a')</p>
<p>b=var('b')</p>
<p>c=var('c')</p>
<p>d=var('d')</p>
<p>e=var('e')</p>
<p>eqn=a==b<em>d^2+c</em>e^2+e<em>(b+2</em>c*d),</p>
<p>how can I automatically rewrite a as a polynomial in b and c, so that I get an equation that looks like</p>
<p>a==b<em>(d^2+e)+c</em>(e^2+2<em>e</em>d)?</p>
http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?answer=23413#post-id-23413
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.
Mon, 14 Jul 2014 00:24:50 -0500http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?answer=23413#post-id-23413Comment by Zorgoth for <p>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.</p>
<pre><code>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
</code></pre>
<p>For your baby example, you could work in the symbolic ring and just ask coefficients,</p>
<pre><code>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
</code></pre>
<p>or you could mix the two in the following way.</p>
<pre><code>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
</code></pre>
<p>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.</p>
http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23424#post-id-23424Thank 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).Tue, 15 Jul 2014 11:25:12 -0500http://ask.sagemath.org/question/23409/how-to-determine-the-coefficients-of-a-polynomial-when-they-depend-on-variables/?comment=23424#post-id-23424