user32882
user32882

Reputation: 5907

mpmath laplace inverse function in python

I am trying to find the laplace inverse of an expression for which all but one variable are already defined at the time of declaration:

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'))

But I am getting this error:

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

I also tried the lambda function format suggested by the doc website but still got the same error.

Does the mpmath.invertlaplace function require that everything be in numerical termsat the time of definition? I am asking because this worked:

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

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

If so, I need to be able to circumvent this. The whole point for me is to play around with the other variables and see how they affect the inverse laplacian. Furthermore one would think that the function gets evaluated before it is passed on to mpmath.

If not, then what on earth is going on here?

Upvotes: 1

Views: 1039

Answers (1)

user32882
user32882

Reputation: 5907

Allright I got it. Basically the function that mp.invertlaplace needs to itself only use mpmath defined functions. In the code provided in the original question I am using exp from the numpy library. So exp(x) is really numpy.exp(x). To make the code work it needs to call the mpmath.exp function as follows:

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))

I have not tested the above on the reduced example I provided in the original question, since it is a subset of the more general script. However it should work and this appears to be the root of the problem.

Upvotes: 2

Related Questions