【问题标题】:Laplace transform of unknown integral (function of time)未知积分的拉普拉斯变换(时间的函数)
【发布时间】:2018-10-27 15:04:54
【问题描述】:

我需要计算一个积分函数的拉普拉斯变换。看来sympy还不能理解。

假设如下:

from sympy import *
s, t = symbols('s t')
I = Function('I')(t)
eq1 = integrate(I, t)
transforms.laplace_transform(eq1, t, s)

解决办法应该是:I(s) / s

但是,sympy 给出:LaplaceTransform(Integral(I(t), t), t, s)

这似乎是一个未解决的问题Issue 7219。有什么解决办法吗?

【问题讨论】:

  • 关于解决方法:Eric Wieser 在 github 问题上的“糟糕的实现”怎么样?
  • 这似乎只适用于衍生品。已经尝试过了,它给出了相同的结果。

标签: python transform sympy integral


【解决方案1】:

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 的参数,并检查内部是否有纯DerivativeIntegral。对于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 的参数是其他任何东西,则表达式将保持不变。

【讨论】:

  • 太棒了!感谢您的解决方法。它解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
  • 1970-01-01
相关资源
最近更新 更多