在具有复系数(4 度)的多项式上使用 sympy 的求解器时出错
Error when using sympy's solver on polynomials with complex coefficients (4th deg)
尝试用 sympy 求解 4 次多项式方程时,我遇到了一些困难。我的代码和我试图求解的方程式:
import sympy as sym
from sympy import I
sym.init_printing()
k = sym.Symbol('k')
t, sigma ,k0, L , V = sym.symbols('t, sigma, k0, L,V')
x4 = ( -t**2 + 2*I * t / sigma**2 + 1/sigma**4)
x3 = ( -2*I * t * k0 / sigma**2 - 2*k0 / sigma**4)
x2 = ( L**2 + k0 **2 / sigma **4 + t**2 * V - 2 * I * t * V / sigma**2 -V/sigma**4)
x1 = (2*I * V * k0 / sigma**2 + 2*k0 * V / sigma **4)
x0 = (2*I*k0*t*V / sigma**2 - k0 **2 *V / sigma**4)
expr = x4 * k**4 + x3 * k**3 + x2 * k**2 + x1 * k + x0
expr2 = expr.subs({k0 :2 , sigma : .2 , L : 1, V:1})
sym.solvers.solve(expr2,k)
输出:
Traceback (most recent call last):
File "<ipython-input-4-e1ce7d8c9531>", line 1, in <module>
sols = sym.solvers.solve(expr2,k)
File "/usr/local/lib/python2.7/dist-packages/sympy/solvers /solvers.py", line 1125, in solve
solution = nfloat(solution, exponent=False)
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2465, in nfloat
return type(expr)([nfloat(a, n, exponent) for a in expr])
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2499, in nfloat
lambda x: isinstance(x, Function)))
File "/usr/local/lib/python2.7/dist-packages/sympy/core/basic.py", line 1087, in xreplace
value, _ = self._xreplace(rule)
File "/usr/local/lib/python2.7/dist-packages/sympy/core/basic.py", line 1095, in _xreplace
return rule[self], True
File "/usr/local/lib/python2.7/dist-packages/sympy/core/rules.py", line 59, in __getitem__
return self._transform(key)
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2498, in <lambda>
lambda x: x.func(*nfloat(x.args, n, exponent)),
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2465, in nfloat
return type(expr)([nfloat(a, n, exponent) for a in expr])
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2465, in nfloat
return type(expr)([nfloat(a, n, exponent) for a in expr])
TypeError: __new__() takes exactly 3 arguments (2 given)
而且我真的无法从中得到任何东西。我不太确定是什么原因造成的,我 "tested" 这个解算器用于更紧凑的多项式并且它运行良好。
看来您可以使用 solve(expr2, k, rational=False)
解决此问题。
尝试用 sympy 求解 4 次多项式方程时,我遇到了一些困难。我的代码和我试图求解的方程式:
import sympy as sym
from sympy import I
sym.init_printing()
k = sym.Symbol('k')
t, sigma ,k0, L , V = sym.symbols('t, sigma, k0, L,V')
x4 = ( -t**2 + 2*I * t / sigma**2 + 1/sigma**4)
x3 = ( -2*I * t * k0 / sigma**2 - 2*k0 / sigma**4)
x2 = ( L**2 + k0 **2 / sigma **4 + t**2 * V - 2 * I * t * V / sigma**2 -V/sigma**4)
x1 = (2*I * V * k0 / sigma**2 + 2*k0 * V / sigma **4)
x0 = (2*I*k0*t*V / sigma**2 - k0 **2 *V / sigma**4)
expr = x4 * k**4 + x3 * k**3 + x2 * k**2 + x1 * k + x0
expr2 = expr.subs({k0 :2 , sigma : .2 , L : 1, V:1})
sym.solvers.solve(expr2,k)
输出:
Traceback (most recent call last):
File "<ipython-input-4-e1ce7d8c9531>", line 1, in <module>
sols = sym.solvers.solve(expr2,k)
File "/usr/local/lib/python2.7/dist-packages/sympy/solvers /solvers.py", line 1125, in solve
solution = nfloat(solution, exponent=False)
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2465, in nfloat
return type(expr)([nfloat(a, n, exponent) for a in expr])
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2499, in nfloat
lambda x: isinstance(x, Function)))
File "/usr/local/lib/python2.7/dist-packages/sympy/core/basic.py", line 1087, in xreplace
value, _ = self._xreplace(rule)
File "/usr/local/lib/python2.7/dist-packages/sympy/core/basic.py", line 1095, in _xreplace
return rule[self], True
File "/usr/local/lib/python2.7/dist-packages/sympy/core/rules.py", line 59, in __getitem__
return self._transform(key)
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2498, in <lambda>
lambda x: x.func(*nfloat(x.args, n, exponent)),
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2465, in nfloat
return type(expr)([nfloat(a, n, exponent) for a in expr])
File "/usr/local/lib/python2.7/dist-packages/sympy/core/function.py", line 2465, in nfloat
return type(expr)([nfloat(a, n, exponent) for a in expr])
TypeError: __new__() takes exactly 3 arguments (2 given)
而且我真的无法从中得到任何东西。我不太确定是什么原因造成的,我 "tested" 这个解算器用于更紧凑的多项式并且它运行良好。
看来您可以使用 solve(expr2, k, rational=False)
解决此问题。