代码之家  ›  专栏  ›  技术社区  ›  user32882

python中的mpmath-laplace反函数

  •  0
  • user32882  · 技术社区  · 8 年前

    我试图找到一个表达式的拉普拉斯逆,在声明时,除了一个变量外,所有变量都已定义:

    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
    

    lambda function 建议的格式 doc website

    mpmath.invertlaplace 函数要求在定义时一切都以数字形式存在?我之所以这样问是因为它奏效了:

    >>> import mpmath as mp
    >>> def F(s):
        return 1/s
    
    >>> mp.invertlaplace(F,5, method = 'talbot')
    mpf('1.0')
    

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

    如果没有,那么这里到底发生了什么?

    1 回复  |  直到 8 年前
        1
  •  1
  •   user32882    8 年前

    好的,我知道了。基本上是 mp.invertlaplace 只需自身使用 mpmath exp numpy 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))