0

Recently I want to make some code to make inverse Laplace transformation.

I use mpmath.invertlaplace function.

It works quite well, but I want to remove for loop to get this result.

Here is an example.

import mpmath as mp
def func = lambda s: 1 / (s + k)

Time = mp.linspace(1,10,100)

result = []

for i in Time:

     value = mp.invertlaplace(func, i, method = 'stehfest', degree = 32)

     result.append(value)

return Time, result

Unfortunately, I cannot use array for mpmath.invertlaplace function.

Is there another library or function that I use?

1 Answers1

0

I'm not sure if understood what you really want, but the code you provided above has a couple of errors that prevent it from working. First off, func has no "def" and it should therefore be:

func = lambda s: 1.0/(s + 1.0)

Also, k in lambdawasn't defined, so I made it equal to one (1.0) just to get the code started.

The code below works fine. Starting with it, you can (hopefully) change it to meet your goals.

import mpmath as mp

func = lambda s: 1.0/(s + 1.0)

Time = mp.linspace(1,10,100)

result = []

for i in Time:

    value = mp.invertlaplace(func, i, method = 'stehfest', degree = 32)

    result.append(value)

    print (i, value)

Hope that helps. Good luck!

  • Oh. Thank you very much. It was my first question in stack overflow, so I make some mistake. yes. I make a mistake. I need to define k = 1.0 like you. Actually, my real question is 'removing for loop'. Because I want to make it faster. – Thereexist Dec 20 '19 at 02:49