Sage多项式系数包括零

Jim*_*imx 2 sage binomial-coefficients

例如,如果我们在SAGE中有多元多项式

    f=3*x^3*y^2+x*y+3
Run Code Online (Sandbox Code Playgroud)

如何显示系数的完整列表,包括最大dregree项和常数之间缺失项的零值.

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

给我列表

    [3, 1, 3]
Run Code Online (Sandbox Code Playgroud)

但我希望将"完整"列表放入矩阵中.在上面的例子中它应该是

    [3, ,0 , 0, 1, 0, 0, 0, 0, 3]
Run Code Online (Sandbox Code Playgroud)

对应条款:

    x^2*y^2, x^2*y, x*y^2, x*y, x^2, y^2, x, y, constant
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

小智 5

您所需的输出定义不明确,因为您列出的单项式不在字典顺序中(您在代码的第一行中使用).无论如何,使用双循环,您可以按照您想要的任何特定方式排列系数.这是一种自然的方法:

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}))
Run Code Online (Sandbox Code Playgroud)

现在,系数是[3, 0, 0, 0, 1, 0, 0, 0, 3]对应的

x^2*y^2, x^2*y, x^2, x*y^2, x*y, x, y, constant
Run Code Online (Sandbox Code Playgroud)

内置.coefficients()方法仅在您使用时才有用,.monomials()它提供具有这些系数的单项式的匹配列表.