如何让 SymPy 收集偏导数? [英] How do I get SymPy to collect partial derivatives?
问题描述
我一直在使用 SymPy 来扩展复杂偏微分方程的项,并想使用 collect 函数来收集项.然而,处理微分变量不同的二阶(或更高阶)导数似乎存在问题.
I have been using SymPy to expand the terms of a complex partial differential equation and would like to use the collect function to gather terms. However, it seems to have a problem dealing with second (or higher order) derivatives where the variables of differentiation differ.
在下面的代码示例中 collect(expr6...
有效,但 collect(expr7 ...
无效,返回错误消息 "NotImplementedError:改进收集中的 MV 衍生支持
.错误显然与两种情况下的 psi.diff(x,y)
差异有关.我需要做什么对任何人来说都很明显让 collect(expr7 ...
工作?
In the code example below collect(expr6...
works, but collect(expr7 ...
does not, returning the error message "NotImplementedError: Improve MV Derivative support in collect"
. The error is clearly related to the psi.diff(x,y)
difference in the two cases. Is it obvious to anyone what I need to do to have collect(expr7 ...
work?
干杯
理查德
示例:
from sympy import *
psi = Function("psi") (x,y,z,t)
expr6=2*psi.diff(x,x)+3*U*psi.diff(x)+5*psi.diff(y)
expr7=2*psi.diff(x,y)+3*U*psi.diff(x)+5*psi.diff(y)
collect(expr6, psi.diff(x),evaluate=False, exact=False) # works
#collect(expr7, psi.diff(x),evaluate=False, exact=False)
# throws an error: NotImplementedError: Improve MV Derivative support in collect
推荐答案
我遇到了这个问题,我的解决方法是首先使用简单的虚拟变量执行替换,collect
基于这些简单的变量,然后替换回更高级的变量.可能有一些极端情况,但它似乎对我有用.
I've bumped into this issue and my workaround is to perform a substitution with simple dummy variables first, collect
based on these simple variables, and then substitute back the more advanced variables. There might be some corner cases, but it seems to work for me.
from sympy import symarray, collect
def mycollect(expr, var_list, evaluate=True, **kwargs):
""" Acts as collect but substitute the symbols with dummy symbols first so that it can work with partial derivatives.
Matrix expressions are also supported.
"""
if not hasattr(var_list, '__len__'):
var_list=[var_list]
# Mapping Var -> Dummy, and Dummy-> Var
Dummies=symarray('DUM', len(var_list))
Var2Dummy=[(var, Dummies[i]) for i,var in enumerate(var_list)]
Dummy2Var=[(b,a) for a,b in Var2Dummy]
# Replace var with dummies and apply collect
expr = expr.expand().doit()
expr = expr.subs(Var2Dummy)
if hasattr(expr, '__len__'):
expr = expr.applyfunc(lambda ij: collect(ij, Dummies, **kwargs))
else:
expr = collect(expr, Dummies, evaluate=evaluate, **kwargs)
# Substitute back
if evaluate:
return expr.subs(Dummy2Var)
d={}
for k,v in expr.items():
k=k.subs(Dummy2Var)
v=v.subs(Dummy2Var)
d[k]=v
return d
例如:
mycollect(expr6, psi.diff(x), evaluate=False)
mycollect(expr7, psi.diff(x), evaluate=False)
返回:
{Derivative(psi(x, y, z, t), (x, 2)): 2, Derivative(psi(x, y, z, t), x): 3*U, 1: 5*Derivative(psi(x, y, z, t), y)}
{Derivative(psi(x, y, z, t), x, y): 2, Derivative(psi(x, y, z, t), x): 3*U, 1: 5*Derivative(psi(x, y, z, t), y)}
这篇关于如何让 SymPy 收集偏导数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!