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.Wed, 15 Mar 2017 13:06:03 -0500Multivariate polynomial coefficients including zeroshttp://ask.sagemath.org/question/36947/multivariate-polynomial-coefficients-including-zeros/ Dears,
I would like to get the coefficients of a multivariate polynomial **including the zero coefficients (in their correct positions).** I have found a similar answer as regards a polynomial f of two variables x,y
P.<x,y> = PolynomialRing(ZZ, 2, order='lex')
f=3*x^2*y^2+x*y+3
Then using the following code:
coeffs = []
for i in range(f.degree(x), -1, -1):
for j in range(f.degree(y), -1, -1):
coeffs.append(f.coefficient({x:i, y:j}))
The result is as expected:
[3, ,0 , 0, 1, 0, 0, 0, 0, 3]
**Now, I would like to extend this solution for a multivariate polynomial of n variables [xo,x1,...xn-1]**
The polynomial is defined with the following code: (Note: q=next_prime(10000))
A1 = [(', '.join('x%i'%i for i in [0.. n-1]))]; ### construct a suitable multivariate ring
V = var(A1[0])
x=vector(list(V))
P1=PolynomialRing(GF(q),V)
How can I do that? Any help will be much appreciated.
Regards,
Natassa
Wed, 15 Mar 2017 09:17:06 -0500http://ask.sagemath.org/question/36947/multivariate-polynomial-coefficients-including-zeros/Comment by nbruin for <p>Dears,</p>
<p>I would like to get the coefficients of a multivariate polynomial <strong>including the zero coefficients (in their correct positions).</strong> I have found a similar answer as regards a polynomial f of two variables x,y</p>
<pre><code>P.<x,y> = PolynomialRing(ZZ, 2, order='lex')
f=3*x^2*y^2+x*y+3
</code></pre>
<p>Then using the following code:</p>
<pre><code>coeffs = []
for i in range(f.degree(x), -1, -1):
for j in range(f.degree(y), -1, -1):
coeffs.append(f.coefficient({x:i, y:j}))
</code></pre>
<p>The result is as expected:
[3, ,0 , 0, 1, 0, 0, 0, 0, 3]</p>
<p><strong>Now, I would like to extend this solution for a multivariate polynomial of n variables [xo,x1,...xn-1]</strong>
The polynomial is defined with the following code: (Note: q=next_prime(10000))</p>
<pre><code>A1 = [(', '.join('x%i'%i for i in [0.. n-1]))]; ### construct a suitable multivariate ring
V = var(A1[0])
x=vector(list(V))
P1=PolynomialRing(GF(q),V)
</code></pre>
<p>How can I do that? Any help will be much appreciated.</p>
<p>Regards,
Natassa</p>
http://ask.sagemath.org/question/36947/multivariate-polynomial-coefficients-including-zeros/?comment=36949#post-id-36949A tip independent of your actual question: you can construct this polynomial ring in a cleaner way with
P1=PolynomialRing(GF(q),n,'x')
If you want the variables bound to symbols so that you can write `x0^2+x1^2` etc. then you can use
P1.inject_variables()
or, if you want the variables as a tuple/list/whatever:
x=P1.gens()
x=vector(P1.gens())
etc.Wed, 15 Mar 2017 11:04:28 -0500http://ask.sagemath.org/question/36947/multivariate-polynomial-coefficients-including-zeros/?comment=36949#post-id-36949Answer by tmonteil for <p>Dears,</p>
<p>I would like to get the coefficients of a multivariate polynomial <strong>including the zero coefficients (in their correct positions).</strong> I have found a similar answer as regards a polynomial f of two variables x,y</p>
<pre><code>P.<x,y> = PolynomialRing(ZZ, 2, order='lex')
f=3*x^2*y^2+x*y+3
</code></pre>
<p>Then using the following code:</p>
<pre><code>coeffs = []
for i in range(f.degree(x), -1, -1):
for j in range(f.degree(y), -1, -1):
coeffs.append(f.coefficient({x:i, y:j}))
</code></pre>
<p>The result is as expected:
[3, ,0 , 0, 1, 0, 0, 0, 0, 3]</p>
<p><strong>Now, I would like to extend this solution for a multivariate polynomial of n variables [xo,x1,...xn-1]</strong>
The polynomial is defined with the following code: (Note: q=next_prime(10000))</p>
<pre><code>A1 = [(', '.join('x%i'%i for i in [0.. n-1]))]; ### construct a suitable multivariate ring
V = var(A1[0])
x=vector(list(V))
P1=PolynomialRing(GF(q),V)
</code></pre>
<p>How can I do that? Any help will be much appreciated.</p>
<p>Regards,
Natassa</p>
http://ask.sagemath.org/question/36947/multivariate-polynomial-coefficients-including-zeros/?answer=36952#post-id-36952I guess your problem comes the iterated loops that you can not write one by one since you do ont know a priori how many loops there will be (it depends on the number of variables). To solve this issue, you can have a look at `itertools.product`:
sage: from itertools import product
sage: product?
Then you can do something like (i just give some hints about `zip` and `dictionaries`):
sage: for I in product(*[range(f.degree(v), -1, -1) for v in P.gens()]):
sage: print I
sage: print dict(zip(P.gens(),I))
Wed, 15 Mar 2017 13:06:03 -0500http://ask.sagemath.org/question/36947/multivariate-polynomial-coefficients-including-zeros/?answer=36952#post-id-36952