Using Sympy: TypeError: '>=' not supported between instances of 'NoneType' and 'int'
Using Sympy: TypeError: '>=' not supported between instances of 'NoneType' and 'int'
所以,我有以下代码:
from sympy import Symbol, solve, nsolve
x1 = Symbol('x1')
x2 = Symbol('x2')
w1 = Symbol('w1')
w2 = Symbol('w2')
eq1 = w1 + w2
eq2 = (w1 * x1) + (w2 * x2)
eq3 = (w1 * x1**2) + (w2 * x2**2)
eq4 = (w1 * x1**3) + (w2 * x2**3)
print(nsolve((eq1, eq2, eq3, eq4), (x1, x2, w1, w2), (2, 0, 2/3, 0)))
对于这个问题:
这给了我这个作为回应:
x = findroot(f, x0, J=J, **kwargs)
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\calculus\optimization.py", line 969, in findroot
for x, error in iterations:
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\calculus\optimization.py", line 660, in __iter__
s = self.ctx.lu_solve(Jx, fxn)
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\linalg.py", line 226, in lu_solve
A, p = ctx.LU_decomp(A)
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\linalg.py", line 142, in LU_decomp
ctx.swap_row(A, j, p[j])
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\matrices.py", line 876, in swap_row
A[i,k], A[j,k] = A[j,k], A[i,k]
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\matrices.py", line 490, in __getitem__
if key[0] >= self.__rows or key[1] >= self.__cols:
TypeError: '>=' not supported between instances of 'NoneType' and 'int'
按照此处的文档操作:
https://docs.sympy.org/latest/modules/solvers/solvers.html
不太清楚是什么导致了这个问题,除了其中一个变量可能是 None。在Google上,很多类似错误的issues都被明确显示出来了,这里不是这样的。有什么建议吗?
编辑:我得到这个答案:
使用此代码:
from scipy.optimize import fsolve
def func(p):
x1, x2, w1, w2 = p
return (w1 + w2, (w1 * x1) + (w2 * x2), (w1 * x1**2) + (w2 * x2**2), (w1 * x1**3) + (w2 * x2**3))
x1, x2, w1, w2 = fsolve(func, (2, 0, 2/3, 0))
print(x1, x2, w1, w2)
所以代码应该 return 结果,但我不确定为什么它不适用于 Sympy。谢谢!
- 使用
nsolve()
时,RHS全为零。 RHS 上的非零项移动 (2, 0, 2/3, 0) 到 LHS。
nsolve()
的第三个参数是接近解的初始猜测。
有根据的猜测是区间 [-1,1]
中的 (x1,x2,w1,w2) = (-1,1,1,1)
具有相同的权重。
我尝试了其他一些猜测。其中一些导致了同样的错误。
输出:
w1 + w2 - 2
w1*x1 + w2*x2
w1*x1**2 + w2*x2**2 - 0.666666666666667
w1*x1**3 + w2*x2**3
Matrix([[-0.577350269189626], [0.577350269189626], [1.00000000000000], [1.00000000000000]])
结果与维基百科 table 上的 n=2
案例一致。
代码:
from sympy import Symbol, solve, nsolve
x1 = Symbol('x1')
x2 = Symbol('x2')
w1 = Symbol('w1')
w2 = Symbol('w2')
eq1 = w1 + w2 - 2
eq2 = (w1 * x1) + (w2 * x2) - 0
eq3 = (w1 * x1**2) + (w2 * x2**2) - 2 / 3
eq4 = (w1 * x1**3) + (w2 * x2**3) - 0
print(eq1, eq2, eq3, eq4, sep='\n')
print(nsolve((eq1, eq2, eq3, eq4), (x1, x2, w1, w2), (-1, 1, 0.5, 0.5)))
SymPy 能够求解非线性方程组,所以如果您不想猜测,您可以这样做:
>>> from sympy import Rational, nonlinsolve
>>> eq1 = w1 + w2 - 2
>>> eq2 = (w1 * x1) + (w2 * x2) - 0
>>> eq3 = (w1 * x1**2) + (w2 * x2**2) - Rational(2, 3) # 2/3 gives float 0.66..
>>> eq4 = (w1 * x1**3) + (w2 * x2**3) - 0
>>> nonlinsolve((eq1, eq2, eq3, eq4), (x1, x2, w1, w2))
FiniteSet((-sqrt(3)/3, sqrt(3)/3, 1, 1), (sqrt(3)/3, -sqrt(3)/3, 1, 1))
所以这给出了两个解决方案。
所以,我有以下代码:
from sympy import Symbol, solve, nsolve
x1 = Symbol('x1')
x2 = Symbol('x2')
w1 = Symbol('w1')
w2 = Symbol('w2')
eq1 = w1 + w2
eq2 = (w1 * x1) + (w2 * x2)
eq3 = (w1 * x1**2) + (w2 * x2**2)
eq4 = (w1 * x1**3) + (w2 * x2**3)
print(nsolve((eq1, eq2, eq3, eq4), (x1, x2, w1, w2), (2, 0, 2/3, 0)))
对于这个问题:
这给了我这个作为回应:
x = findroot(f, x0, J=J, **kwargs)
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\calculus\optimization.py", line 969, in findroot
for x, error in iterations:
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\calculus\optimization.py", line 660, in __iter__
s = self.ctx.lu_solve(Jx, fxn)
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\linalg.py", line 226, in lu_solve
A, p = ctx.LU_decomp(A)
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\linalg.py", line 142, in LU_decomp
ctx.swap_row(A, j, p[j])
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\matrices.py", line 876, in swap_row
A[i,k], A[j,k] = A[j,k], A[i,k]
File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\matrices.py", line 490, in __getitem__
if key[0] >= self.__rows or key[1] >= self.__cols:
TypeError: '>=' not supported between instances of 'NoneType' and 'int'
按照此处的文档操作:
https://docs.sympy.org/latest/modules/solvers/solvers.html
不太清楚是什么导致了这个问题,除了其中一个变量可能是 None。在Google上,很多类似错误的issues都被明确显示出来了,这里不是这样的。有什么建议吗?
编辑:我得到这个答案:
使用此代码:
from scipy.optimize import fsolve
def func(p):
x1, x2, w1, w2 = p
return (w1 + w2, (w1 * x1) + (w2 * x2), (w1 * x1**2) + (w2 * x2**2), (w1 * x1**3) + (w2 * x2**3))
x1, x2, w1, w2 = fsolve(func, (2, 0, 2/3, 0))
print(x1, x2, w1, w2)
所以代码应该 return 结果,但我不确定为什么它不适用于 Sympy。谢谢!
- 使用
nsolve()
时,RHS全为零。 RHS 上的非零项移动 (2, 0, 2/3, 0) 到 LHS。 nsolve()
的第三个参数是接近解的初始猜测。
有根据的猜测是区间[-1,1]
中的(x1,x2,w1,w2) = (-1,1,1,1)
具有相同的权重。
我尝试了其他一些猜测。其中一些导致了同样的错误。
输出:
w1 + w2 - 2
w1*x1 + w2*x2
w1*x1**2 + w2*x2**2 - 0.666666666666667
w1*x1**3 + w2*x2**3
Matrix([[-0.577350269189626], [0.577350269189626], [1.00000000000000], [1.00000000000000]])
结果与维基百科 table 上的 n=2
案例一致。
代码:
from sympy import Symbol, solve, nsolve
x1 = Symbol('x1')
x2 = Symbol('x2')
w1 = Symbol('w1')
w2 = Symbol('w2')
eq1 = w1 + w2 - 2
eq2 = (w1 * x1) + (w2 * x2) - 0
eq3 = (w1 * x1**2) + (w2 * x2**2) - 2 / 3
eq4 = (w1 * x1**3) + (w2 * x2**3) - 0
print(eq1, eq2, eq3, eq4, sep='\n')
print(nsolve((eq1, eq2, eq3, eq4), (x1, x2, w1, w2), (-1, 1, 0.5, 0.5)))
SymPy 能够求解非线性方程组,所以如果您不想猜测,您可以这样做:
>>> from sympy import Rational, nonlinsolve
>>> eq1 = w1 + w2 - 2
>>> eq2 = (w1 * x1) + (w2 * x2) - 0
>>> eq3 = (w1 * x1**2) + (w2 * x2**2) - Rational(2, 3) # 2/3 gives float 0.66..
>>> eq4 = (w1 * x1**3) + (w2 * x2**3) - 0
>>> nonlinsolve((eq1, eq2, eq3, eq4), (x1, x2, w1, w2))
FiniteSet((-sqrt(3)/3, sqrt(3)/3, 1, 1), (sqrt(3)/3, -sqrt(3)/3, 1, 1))
所以这给出了两个解决方案。