Python SymPy:如何使'nsolve'返回包含找到的解决方案的词典
我想使用Python SymPy:如何使'nsolve'返回包含找到的解决方案的词典,python,sympy,solver,Python,Sympy,Solver,我想使用solve作为solve的回退,并喜欢使用dict=True使solve返回一个包含找到的解决方案和相应变量的字典。但是,nsolve似乎没有此选项 这就是我正在使用的解决方法: from sympy import * def nsolve(equations, variables, guesses, **flags): from sympy import nsolve as originalnsolve result = originalnsolve(equation
solve
作为solve
的回退,并喜欢使用dict=True
使solve
返回一个包含找到的解决方案和相应变量的字典。但是,nsolve
似乎没有此选项
这就是我正在使用的解决方法:
from sympy import *
def nsolve(equations, variables, guesses, **flags):
from sympy import nsolve as originalnsolve
result = originalnsolve(equations, variables, guesses, **flags)
if "dict" in flags and flags["dict"]:
return [dict(zip(variables, [float(value) for value in result]))]
else:
return result
x, y = symbols("x y")
equations = [Eq(2*x+y, 3), Eq(y-x, 1)]
variables = [x, y]
guesses = [1, 1]
print("solve with dict = True produces:\n%s\n" % solve(equations, variables, dict = True) + "The result is a dictionary, as needed\n")
print("nsolve without dict = True produces:\n%s\n" % nsolve(equations, variables, guesses) + "nsolve doesn't return a dictionary\n")
print("nsolve with dict = True produces:\n%s\n" % nsolve(equations, variables, guesses, dict = True) + "My workaround wrapper function returns a dictionary\n")
solve with dict = True produces:
[{x: 2/3, y: 5/3}]
The result is a dictionary, as needed
nsolve without dict = True produces:
[0.666666666666667]
[ 1.66666666666667]
nsolve doesn't return a dictionary
nsolve with dict = True produces:
[{x: 0.6666666666666666, y: 1.6666666666666667}]
My workaround wrapper function returns a dictionary
输出为:
from sympy import *
def nsolve(equations, variables, guesses, **flags):
from sympy import nsolve as originalnsolve
result = originalnsolve(equations, variables, guesses, **flags)
if "dict" in flags and flags["dict"]:
return [dict(zip(variables, [float(value) for value in result]))]
else:
return result
x, y = symbols("x y")
equations = [Eq(2*x+y, 3), Eq(y-x, 1)]
variables = [x, y]
guesses = [1, 1]
print("solve with dict = True produces:\n%s\n" % solve(equations, variables, dict = True) + "The result is a dictionary, as needed\n")
print("nsolve without dict = True produces:\n%s\n" % nsolve(equations, variables, guesses) + "nsolve doesn't return a dictionary\n")
print("nsolve with dict = True produces:\n%s\n" % nsolve(equations, variables, guesses, dict = True) + "My workaround wrapper function returns a dictionary\n")
solve with dict = True produces:
[{x: 2/3, y: 5/3}]
The result is a dictionary, as needed
nsolve without dict = True produces:
[0.666666666666667]
[ 1.66666666666667]
nsolve doesn't return a dictionary
nsolve with dict = True produces:
[{x: 0.6666666666666666, y: 1.6666666666666667}]
My workaround wrapper function returns a dictionary
我的问题:
from sympy import *
def nsolve(equations, variables, guesses, **flags):
from sympy import nsolve as originalnsolve
result = originalnsolve(equations, variables, guesses, **flags)
if "dict" in flags and flags["dict"]:
return [dict(zip(variables, [float(value) for value in result]))]
else:
return result
x, y = symbols("x y")
equations = [Eq(2*x+y, 3), Eq(y-x, 1)]
variables = [x, y]
guesses = [1, 1]
print("solve with dict = True produces:\n%s\n" % solve(equations, variables, dict = True) + "The result is a dictionary, as needed\n")
print("nsolve without dict = True produces:\n%s\n" % nsolve(equations, variables, guesses) + "nsolve doesn't return a dictionary\n")
print("nsolve with dict = True produces:\n%s\n" % nsolve(equations, variables, guesses, dict = True) + "My workaround wrapper function returns a dictionary\n")
solve with dict = True produces:
[{x: 2/3, y: 5/3}]
The result is a dictionary, as needed
nsolve without dict = True produces:
[0.666666666666667]
[ 1.66666666666667]
nsolve doesn't return a dictionary
nsolve with dict = True produces:
[{x: 0.6666666666666666, y: 1.6666666666666667}]
My workaround wrapper function returns a dictionary
- 我是否缺少一个更简单的方法来让
返回字典nsolve
- 如果不是:我的方法有问题吗
nsolve
没有dict
选项。如果您想要请求一个,您应该在中打开一个功能请求,或者打开一个实现它的请求 好的,谢谢你的回复。那我就试试吧