Ask Your Question

Multivariate polynomial coefficients including zeros

asked 2017-03-15 15:17:06 +0100


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') 

Then using the following code:

coeffs = []
for i in range(, -1, -1):
  for j in range(, -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])

How can I do that? Any help will be much appreciated.

Regards, Natassa

edit retag flag offensive close merge delete


A tip independent of your actual question: you can construct this polynomial ring in a cleaner way with


If you want the variables bound to symbols so that you can write x0^2+x1^2 etc. then you can use


or, if you want the variables as a tuple/list/whatever:



nbruin gravatar imagenbruin ( 2017-03-15 17:04:28 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-03-15 19:06:03 +0100

tmonteil gravatar image

I 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(, -1, -1) for v in P.gens()]):
sage:     print I
sage:     print dict(zip(P.gens(),I))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2017-03-15 15:17:06 +0100

Seen: 332 times

Last updated: Mar 15 '17