2

If we have a multivariate polynomial in SAGE for instance

    f=3*x^3*y^2+x*y+3

how can i display the full list of coefficients including the zero ones from missing terms between maximum dregree term and constant.

    P.<x,y> = PolynomialRing(ZZ, 2, order='lex')
    f=3*x^2*y^2+x*y+3
    f.coefficients()

gives me the list

    [3, 1, 3]

but i'd like the "full" list to put into a a matrix. In the above example it should be

    [3, ,0 , 0, 1, 0, 0, 0, 0, 3]

corresponding to terms:

    x^2*y^2, x^2*y, x*y^2, x*y, x^2, y^2, x, y, constant

Am I missing something?

Jimx
  • 90
  • 1
  • 7

1 Answers1

5

Your desired output isn't quite well defined, because the monomials you listed are not in the lexicographic order (which you used in the first line of your code). Anyway, using a double loop you can arrange coefficients in any specific way you want. Here is a natural way to do this:

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

Now coeffs is [3, 0, 0, 0, 1, 0, 0, 0, 3], corresponding to

x^2*y^2, x^2*y, x^2, x*y^2, x*y, x, y, constant

The built-in .coefficients() method is only useful if you also use .monomials() which provides a matching list of monomials that have those coefficients.

  • You are right I wasn't clear about my output and not aware of the coefficient() method. The workaround i was using was building monomials([x,y],[2,2]) and comparing each element. My problem with my ordering though is taking monomial x*y>x^2 AND x*y>y^2. I still can't find any known ordering or method to do this so your answer is still the simplest and closest. Thanks! – Jimx Jun 25 '15 at 09:19