使用常数多项式的Sum,SymPy中的GeneratorsNeeded错误

时间:2021-10-18 23:21:46

I'm getting the error

我收到了错误

sympy.polys.polyerrors.GeneratorsNeeded: can't initialize from 'dict' without generators

sympy.polys.polyerrors.GeneratorsNeeded:无法在没有生成器的情况下从'dict'初始化

when I try to sum a zero polynomial. The error occurs at the line

当我尝试求零多项式时。错误发生在该行

    g = k*Sum(f,(m,k,n)).doit()

Here m and n are symbols, and k is an int. The error occurs the first time through the loop, when f has been initialized to Poly(0,m). The script computes a sequence of polynomials. If instead I initialize f to Poly(m,m) the script runs to completion. If I try Poly(1,m) I get the same error as before.

这里m和n是符号,k是int。当f初始化为Poly(0,m)时,第一次通过循环时会发生错误。该脚本计算一系列多项式。相反,如果我将f初始化为Poly(m,m),脚本将运行完成。如果我尝试Poly(1,m),我会得到与以前相同的错误。

How can I define a constant polynomial so that my script will execute properly?

如何定义常量多项式以便我的脚本正确执行?

1 个解决方案

#1


1  

The Sum passes doit() to the object it is trying to sum, which is where the error occurs. So, a minimal example is

Sum将doit()传递给它试图求和的对象,这是发生错误的地方。所以,一个最小的例子是

x = symbols('x')
f = Poly(0, x)
f.doit()  #  GeneratorsNeeded: can't initialize from 'dict' without generators

Looks like a bug, an edge case not being addressed correctly. But there is a workaround: pass an expression to Sum (which is what it expects anyway), not a Poly object. A Poly is turned into an expression with as_expr().

看起来像一个错误,边缘情况没有得到正确处理。但是有一个解决方法:将表达式传递给Sum(无论如何都是它所期望的),而不是Poly对象。使用as_expr()将Poly转换为表达式。

f = Poly(0, m)
Sum(f.as_expr(), (m, 0, n)).doit()   #  0

#1


1  

The Sum passes doit() to the object it is trying to sum, which is where the error occurs. So, a minimal example is

Sum将doit()传递给它试图求和的对象,这是发生错误的地方。所以,一个最小的例子是

x = symbols('x')
f = Poly(0, x)
f.doit()  #  GeneratorsNeeded: can't initialize from 'dict' without generators

Looks like a bug, an edge case not being addressed correctly. But there is a workaround: pass an expression to Sum (which is what it expects anyway), not a Poly object. A Poly is turned into an expression with as_expr().

看起来像一个错误,边缘情况没有得到正确处理。但是有一个解决方法:将表达式传递给Sum(无论如何都是它所期望的),而不是Poly对象。使用as_expr()将Poly转换为表达式。

f = Poly(0, m)
Sum(f.as_expr(), (m, 0, n)).doit()   #  0