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.Mon, 19 Mar 2018 12:20:51 -0500get the coefficients of polynomial of several variables?http://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/ Consider that I have a polynomial with n variables x1,x2,...,xn and want to get the coefficient of that polynomial. For example I can have:
x1+2*x1*x5^2+3*x1^2*x4-5*x2*x5^2+1/2*x2*x3+x6^2*x7*x8+9*x3*x9^3
I would like to ask Sage to give coefficient for the polynomial with variables x1..x9 and the result should be something like this:
coefficient,(exponent of x1..x9);
1,(1,0,0,0,0,0,0,0,0);
2,(1,0,0,0,2,0,0,0,0);
3,(2,0,0,1,0,0,0,0,0);
-5,(0,1,0,0,2,0,0,0,0);
1/2,(0,1,1,0,0,0,0,0,0);
1,(0,0,0,0,0,2,1,1);
9,(0,0,1,0,0,0,0,0,3)
How can I achieve this?
Tue, 13 Mar 2018 21:12:52 -0500http://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/Answer by kcrisman for <p>Consider that I have a polynomial with n variables x1,x2,...,xn and want to get the coefficient of that polynomial. For example I can have:
x1+2<em>x1</em>x5^2+3<em>x1^2</em>x4-5<em>x2</em>x5^2+1/2<em>x2</em>x3+x6^2<em>x7</em>x8+9<em>x3</em>x9^3</p>
<p>I would like to ask Sage to give coefficient for the polynomial with variables x1..x9 and the result should be something like this:
coefficient,(exponent of x1..x9);
1,(1,0,0,0,0,0,0,0,0);
2,(1,0,0,0,2,0,0,0,0);
3,(2,0,0,1,0,0,0,0,0);
-5,(0,1,0,0,2,0,0,0,0);
1/2,(0,1,1,0,0,0,0,0,0);
1,(0,0,0,0,0,2,1,1);
9,(0,0,1,0,0,0,0,0,3)</p>
<p>How can I achieve this?</p>
http://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/?answer=41532#post-id-41532Do you mean something like this?
sage: S = PolynomialRing(QQ, ['a','b','c'])
sage: p = S("a+a^2*b+c^3")
sage: p.dict()
{(0, 0, 3): 1, (1, 0, 0): 1, (2, 1, 0): 1}
See [the multivariate polynomial generic documentation](http://doc.sagemath.org/html/en/reference/polynomial_rings/sage/rings/polynomial/multi_polynomial_element.html) for a lot more on these objects.Tue, 13 Mar 2018 21:51:34 -0500http://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/?answer=41532#post-id-41532Comment by kcrisman for <p>Do you mean something like this?</p>
<pre><code>sage: S = PolynomialRing(QQ, ['a','b','c'])
sage: p = S("a+a^2*b+c^3")
sage: p.dict()
{(0, 0, 3): 1, (1, 0, 0): 1, (2, 1, 0): 1}
</code></pre>
<p>See <a href="http://doc.sagemath.org/html/en/reference/polynomial_rings/sage/rings/polynomial/multi_polynomial_element.html">the multivariate polynomial generic documentation</a> for a lot more on these objects.</p>
http://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/?comment=41635#post-id-41635Probably; you'd have to coerce this into a polynomial ring over `QQ[b]` but I'm not quite sure how to do that - it may be as easy as defining `T = ` the right ring and then doing it.Mon, 19 Mar 2018 12:20:51 -0500http://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/?comment=41635#post-id-41635Comment by DanialBagh for <p>Do you mean something like this?</p>
<pre><code>sage: S = PolynomialRing(QQ, ['a','b','c'])
sage: p = S("a+a^2*b+c^3")
sage: p.dict()
{(0, 0, 3): 1, (1, 0, 0): 1, (2, 1, 0): 1}
</code></pre>
<p>See <a href="http://doc.sagemath.org/html/en/reference/polynomial_rings/sage/rings/polynomial/multi_polynomial_element.html">the multivariate polynomial generic documentation</a> for a lot more on these objects.</p>
http://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/?comment=41535#post-id-41535Thanks for the answer, but what if I want to treat 'b' as coefficient and just get the coefficients of the polynomial with respect to 'a' and 'c'? Can i do that?Tue, 13 Mar 2018 23:24:37 -0500http://ask.sagemath.org/question/41526/get-the-coefficients-of-polynomial-of-several-variables/?comment=41535#post-id-41535