Jimx
Jimx

Reputation: 90

Sage polynomial coefficients including zeros

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?

Upvotes: 2

Views: 1093

Answers (1)

user3717023
user3717023

Reputation:

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.

Upvotes: 5

Related Questions