4

I'm trying to do the inverse Laplace transform of a generalized rational function of the form: D/(A*s**2 + B*s + C) using sympy.

from sympy import *
from sympy import inverse_laplace_transform as ilt
from sympy.abc import s, t
var('A:D')
eq = D/(A*s**2 + B*s + C)
solution = ilt(eq, s, t)

Knowing the answer from mathematical analysis to be:

-(A (e^(t (-1/2 sqrt(C^2-4 B)-C/2))-e^(t (1/2 sqrt(C^2-4 B)-C/2))))/sqrt(C^2-4 B)

But the sympy will NOT yield a solution and the code will stuck in an infinity CPU process with no specific gain. But putting the eq like this:

eq = B/((s - A)**2 + B**2)

sympy will result in the equation of the form like this:

-I*(I*exp(t*im(B))*sin(t*(re(B) - im(A))) - exp(t*im(B))*cos(t*(re(B) - im(A)))
+ I*exp(-t*im(B))*sin(t*(re(B) + im(A))) + exp(-t*im(B))*cos(t*(re(B) + im(A))))
*exp(t*re(A))*Heaviside(t)/2

Which is not what I would need to. Any suggestions on how to make sympy yield to such a human readable answer?

Reza Saidafkan
  • 320
  • 2
  • 9
  • If you know that A and B are real, set them so when you create them (`A, B = symbols("A:B", real=True)`). – asmeurer Dec 08 '13 at 22:39
  • Yes @asmeurer that worked for the case: `eq = B/((s - A)**2 + B**2)`. But still the main issue remains: if I introduce `A, B, C = symbols("A:C", real=True)`, with `eq = B/(s**2 - 2*A + C**2)`, the inverse Laplace transform will result in nothing but infinite CPU cycles. Any ideas ? – Reza Saidafkan Dec 09 '13 at 05:34
  • Can you keyboard interrupt it after it's run for a while and paste the traceback somewhere? – asmeurer Dec 09 '13 at 08:39
  • @asmeurer here you are: [http://pastebin.com/6RyhxFQR] it is the keyboard break for: `from sympy import *; from sympy import s, t; from sympy import inverse_laplace_transform as ilt; A, B = symbols("A:B"); eq = 1/(s**2 - A*2*s + B**2 ); ilt(eq, s, t)` – Reza Saidafkan Dec 09 '13 at 09:18
  • It says the paste has been removed. – asmeurer Dec 09 '13 at 09:21
  • here is another paste: [http://pastebin.com/FREAHWSM] – Reza Saidafkan Dec 09 '13 at 09:25
  • @asmeurer edit: A and B were actually introduced with `A, B = symbols("A:B", real = True)`, not `A, B = symbols("A:B")`, as you mentioned before. – Reza Saidafkan Dec 09 '13 at 09:28
  • BTW, `A*s**2 + B*s + C` is a second order polynomial, not a third. – Teepeemm Dec 09 '13 at 14:19
  • Yes it is. But they both seem to suffer in the same way. May be working on the second order polynomial yields an easier answer. – Reza Saidafkan Dec 09 '13 at 16:51
  • 1
    Oh, SO is putting the last `]` as part of the link. – asmeurer Dec 10 '13 at 06:42

1 Answers1

2

Looking at the traceback, this looks like a legitimate bug. You should report it in the SymPy issue tracker.

asmeurer
  • 86,894
  • 26
  • 169
  • 240