Sage polynomial coefficients including zeros

早过忘川 提交于 2019-12-11 03:53:45

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/30952289/sage-polynomial-coefficients-including-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!