python - 仅将 SymPy 中的 doit() 函数应用于表达式的外部

标签 python sympy

sympy 中的 doit() 函数继续执行并尽可能计算表达式。例如:

from sympy import *
u = IndexedBase('u')
i = symbols('i')
test = Sum(u[i],(i,1,3))
test.doit()

会回来

enter image description here

还有:

from sympy import *
u,x = symbols('u, x')
test = Derivative(u,x)
test.doit()

创建符号微分,然后计算它。在这种情况下,评估结果为零:

enter image description here

enter image description here

但是如果我想在 Sum 中使用 Derivative 怎么办? doit() 函数走得太远了:

from sympy import *
u = IndexedBase('u')
x = IndexedBase('x')
i = symbols('i')
test = Sum(Derivative(u[i],x[i]),(i,1,3))
test.doit()

这将再次返回零。我想扩大总和但实际上不评估导数。这应该导致如下表达式:

enter image description here

我怎样才能得到这个作为我的输出?是否可以让 doit() 命令只对外部函数 (Sum()) 起作用,而不对内部函数 (Derivative() 起作用>)?我做错了吗?

最佳答案

我找到了一种方法。

在仔细检查 doit() 命令参数后,设置选项 deep = False 似乎可以防止计算过深地进入表达式。此外,一些迹象表明可以更彻底地控制它。命令文档显示:

Evaluate objects that are not evaluate by default like limits, integrals, sums and products. All objects of this kind will be evaluated recursively, unless some species were excluded via 'hints' or unless the 'deep' hint was set to 'False'.

就我而言,我很好奇如何利用“提示”。如果有人可以提供有关“提示”如何工作的更多见解,那就太好了。谢谢。

关于python - 仅将 SymPy 中的 doit() 函数应用于表达式的外部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46024543/

相关文章:

python - 为什么 SymPy 不能正确处理实数?

python - 通过 Cygwin 运行 Python 脚本的问题

python - Sympy 反函数 : y = ax+b into x = (y-b)/a

python - 如何(Sanely)扩展 sympy.Rational

python - Numpy:如何检查 ndarray 中是否存在元组

python - Sympy 中导数函数的积分

scipy - python/scipy、numpy、sympy 中是否有与 Mathematicas RootApproximant 函数等效的函数?

python - 如何安装sklearn?

python - 用 python 组填充数据框的列

python - 在 Python 中使用 if 条件加速逐行循环