如何从 sympy 中的 NotImplementedError 获取值?

How can I get values from NotImplementedError in sympy?

我正在使用此代码:

from sympy.solvers import solve
from sympy import Symbol

x = Symbol('x')

function = input("Insert function: ")

def gx(function,x):
    return solve(function,x,dict=True)

print(gx(function,x))

当我写 cos(x)+x 时,我收到此错误消息:

Traceback (most recent call last):
  File "/home/raulpenate/Documents/pyhton/MetodosNumericos/Tareas/testing.py", line 11, in <module>
    print(gx(function,x))
  File "/home/raulpenate/Documents/pyhton/MetodosNumericos/Tareas/testing.py", line 9, in gx
    return solve(function,x)
  File "/home/raulpenate/.local/lib/python3.9/site-packages/sympy/solvers/solvers.py", line 1095, in solve
    solution = _solve(f[0], *symbols, **flags)
  File "/home/raulpenate/.local/lib/python3.9/site-packages/sympy/solvers/solvers.py", line 1714, in _solve
    raise NotImplementedError('\n'.join([msg, not_impl_msg % f]))
NotImplementedError: multiple generators [x, cos(x)]
No algorithms are implemented to solve equation x + cos(x)

我想从

NotImplementedError: multiple generators [x, cos(x)]

特别是 cos(x),我怎样才能从那里得到那个值?。我在 documentation.

中找不到该部分

NotImplementedError如果sympy找不到解析解就会出现。你可以用数字来解决这个问题:

from sympy import nsolve, cos, Symbol
x = Symbol("x")
nsolve(cos(x) + x, 0) # -0.739085133215161

这个问题不会在初等函数方面不太可能有封闭形式,参见https://math.stackexchange.com/questions/46934/what-is-the-solution-of-cosx-x/1174794#1174794 and https://mathworld.wolfram.com/DottieNumber.html