the issue 好像还没有修复。
但是,我们可以根据 Eric Wieser 为衍生品给出的"crappy implementation" 给出一个“糟糕的解决方法”。但是请注意,原始的 sn-p 似乎也不适用于导数,因为自 sn-p 发布以来,高阶导数的内部表示似乎发生了变化。
这是我的“糟糕”解决方法,它只捕获最简单的情况(仅针对t 的导数,仅针对t 的不定积分,其中t 是拉普拉斯变换作用的变量) :
from sympy import *
def laplace(e, t, s):
"""Hacked-up Laplace transform that handles derivatives and integrals
Updated generalization of https://github.com/sympy/sympy/issues/7219#issuecomment-154768904
"""
res = laplace_transform(e, t, s, noconds=True)
wf = Wild('f')
lw = LaplaceTransform(wf, t, s)
for exp in res.find(lw):
e = exp.match(lw)[wf]
args = e.args
if isinstance(e, Derivative):
# for derivative check that there's only d/dt^n with n>0
if len(args) == 2 and args[1][0] == t:
n = args[1][1]
if n > 0:
newexp = s**n * LaplaceTransform(e.args[0], t, s)
res = res.replace(exp, newexp)
elif isinstance(e, Integral):
# for integral check that there's only n consecutive indefinite integrals w.r.t. t
if all(len(arg) == 1 and arg[0] == t for arg in args[1:]):
newexp = s**(-len(args[1:])) * LaplaceTransform(args[0], t, s)
res = res.replace(exp, newexp)
# otherwise don't do anything
return res
x = Function('x')
s,t = symbols('s t')
print(laplace(Derivative(x(t), t, 3), t, s))
print(laplace(Integral(Integral(x(t), t), t), t, s))
以上输出
s**3*LaplaceTransform(x(t), t, s)
LaplaceTransform(x(t), t, s)/s**2
正如预期的那样。使用您的具体示例:
I = Function('I')(t)
eq1 = integrate(I, t)
LI = laplace(eq1, t, s)
print(LI)
我们得到
LaplaceTransform(I(t), t, s)/s
这是您所期望的“I(s)/s”的正确表示。
上述解决方法的工作方式是匹配LaplaceTransform 的参数,并检查内部是否有纯Derivative 或Integral。对于Derivative,我们检查是否仅与t 有区别;这就是 Eric 最初的解决方法所做的,但是虽然他的代码似乎期望 args 的形式为 Derivative(x(t), t, t, t),但目前衍生的表示形式是 Derivative(x(t), (t,3))。这就是为什么必须改变处理这个用例的原因。
至于Integrals,表示与原来的类似:Integral(x(t), t, t)是一个二重积分。我仍然需要调整 Eric 的原始值,因为这个表达式的 args 包含每个积分的元组而不是标量 t,以适应定积分。由于我们只想处理不定积分的简单情况,因此我确保只有不定积分并且仅针对t。
如果LaplaceTransform 的参数是其他任何东西,则表达式将保持不变。