python

时间:2017-08-04 07:13:18

标签: python mpmath

我试图找到一个表达式的laplace inverse,除了一个变量之外,在声明时已经定义了所有变量:

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之前会被评估。

如果没有,那么究竟是怎么回事?

1 个答案:

答案 0 :(得分:1)

好吧,我明白了。基本上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))

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