SymPy:如何使 `nsolve` return 成为包含找到的解决方案的字典

SymPy: How to make `nsolve` return a dictionary with the found solutions

我想使用 nsolve 作为 solve 的后备,并喜欢使用 dict = True 来制作 solve return 一个包含找到的解决方案的字典和相应的变量。但是,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

我的问题:

nsolve 没有 dict 选项。如果你想请求一个,你应该在 issue tracker 中打开一个功能请求,或者一个实现它的拉取请求。