python 中的 mpmath 拉普拉斯反函数

mpmath laplace inverse function in python

我正在尝试查找一个表达式的拉普拉斯逆函数,该表达式在声明时除了一个变量外都已定义:

from numpy import *
import mpmath as mp
p0 = 1
E = 2
c= 3
L = 4
x = 2.5
t = linspace(1,5,10)
ulaplace = []

def U(s):
    return(c*p0*(-exp(L*s/c) + exp(s*(L + 2*x)/c))*exp(-s*x/c)/(E*s**2*(exp(2*L*s/c) + 1)))

for ti in t:
    ulaplace.append(mp.invertlaplace(U, ti, method='talbot'))

但是我收到这个错误:

Traceback (most recent call last):
  File "D:\TEMP\IDLEscripts\CompareAnalyticalSolutions2.py", line 46, in <module>
    ulaplace.append(mp.invertlaplace(U, ti, method='talbot'))
  File "C:\Python35\lib\site-packages\mpmath\calculus\inverselaplace.py", line 805, in invertlaplace
    fp = [f(p) for p in rule.p]
  File "C:\Python35\lib\site-packages\mpmath\calculus\inverselaplace.py", line 805, in <listcomp>
    fp = [f(p) for p in rule.p]
  File "D:\TEMP\IDLEscripts\CompareAnalyticalSolutions2.py", line 43, in U
    return(c*p0*(-exp(L*s/c) + exp(s*(L + 2*x)/c))*exp(-s*x/c)/(E*s**2*(exp(2*L*s/c) + 1)))
TypeError: attribute of type 'int' is not callable

我也尝试了 doc website 建议的 lambda function 格式,但仍然出现同样的错误。

mpmath.invertlaplace 函数是否要求在定义时所有内容都以数字表示?我问是因为这有效:

>>> import mpmath as mp
>>> def F(s):
    return 1/s

>>> mp.invertlaplace(F,5, method = 'talbot')
mpf('1.0')

如果是这样,我需要能够规避这一点。对我来说,重点是研究其他变量,看看它们如何影响逆拉普拉斯算子。此外,人们会认为该函数在传递给 mpmath 之前得到了评估。

如果不是,那这到底是怎么回事?

好的,我知道了。基本上 mp.invertlaplace 需要的函数本身只使用 mpmath 定义的函数。在原始问题中提供的代码中,我使用 numpy 库中的 exp。所以 exp(x) 实际上是 numpy.exp(x)。要使代码正常工作,需要调用 mpmath.exp 函数,如下所示:

def U(s):
    return -p0*mp.exp(s*x/c)/(E*s*(-s*mp.exp(L*s/c)/c - s*mp.exp(-L*s/c)/c)) + p0*mp.exp(-s*x/c)/(E*s*(-s*mp.exp(L*s/c)/c - s*mp.exp(-L*s/c)/c))

我没有在原始问题中提供的简化示例中测试以上内容,因为它是更通用脚本的子集。但是它应该可以工作,这似乎是问题的根源。