Error with inverse_laplace_transform (easy to solve)

548 views
Skip to first unread message

Javier Arantegui

unread,
May 6, 2020, 1:36:19 PM5/6/20
to sympy
Hello!

I'm using Sympy 1.5.1 and Python is 3.7.7. Both installed using Anaconda.

I have tried to do an inverse Laplace transform and I got an error:

inverse_laplace_transform(0.5/(.5*s+1)*1/s, s, t)

The error message is quite long, tell me if you need it.

I spent 10 minutes in front of my students trying to think what was wrong...

Finally, I was able to solve it with an easy change:

inverse_laplace_transform(0.5*2/(2*(.5*s+1))*1/s, s, t)


Best regards,

Javier

Oscar Benjamin

unread,
May 6, 2020, 5:00:28 PM5/6/20
to sympy
Hi Javier,

I'm not sure exactly the problem with the example you showed. I think
it's a bug somewhere.

It is usually better not to use floats in sympy. With exact rational
numbers your example works for me:

In [17]: inverse_laplace_transform(S.Half/(S.Half*s+1)*1/s, s, t)
Out[17]:
-2⋅t
θ(t) ℯ ⋅θ(t)
──── - ─────────
2 2


Oscar
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/aa5c4270-6c71-485b-8354-b5ea39e9c380%40googlegroups.com.

Aaron Meurer

unread,
May 6, 2020, 5:25:41 PM5/6/20
to sympy
Here is the error

>>> inverse_laplace_transform(0.5/(.5*s+1)*1/s, s, t)
Traceback (most recent call last):
File "./sympy/polys/polyutils.py", line 211, in
_parallel_dict_from_expr_if_gens
monom[indices[base]] = exp
KeyError: RisingFactorial(_t + 1, 2.0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "./sympy/polys/partfrac.py", line 82, in apart
(P, Q), opt = parallel_poly_from_expr((P, Q), x, **options)
File "./sympy/polys/polytools.py", line 4301, in parallel_poly_from_expr
return _parallel_poly_from_expr(exprs, opt)
File "./sympy/polys/polytools.py", line 4354, in _parallel_poly_from_expr
reps, opt = _parallel_dict_from_expr(exprs, opt)
File "./sympy/polys/polyutils.py", line 332, in _parallel_dict_from_expr
reps, gens = _parallel_dict_from_expr_if_gens(exprs, opt)
File "./sympy/polys/polyutils.py", line 217, in
_parallel_dict_from_expr_if_gens
"the set of generators." % factor)
sympy.polys.polyerrors.PolynomialError: RisingFactorial(_t + 1, 2.0)
contains an element of the set of generators.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./sympy/integrals/transforms.py", line 1293, in
inverse_laplace_transform
return InverseLaplaceTransform(F, s, t, plane).doit(**hints)
File "./sympy/integrals/transforms.py", line 121, in doit
self.function_variable, self.transform_variable, **hints)
File "./sympy/integrals/transforms.py", line 1245, in _compute_transform
return _inverse_laplace_transform(F, s, t, self.fundamental_plane, **hints)
File "./sympy/integrals/transforms.py", line 206, in wrapper
res = func(*args, **kwargs)
File "./sympy/integrals/transforms.py", line 1172, in
_inverse_laplace_transform
needeval=True, noconds=False)
File "./sympy/integrals/transforms.py", line 875, in inverse_mellin_transform
return InverseMellinTransform(F, s, x, strip[0], strip[1]).doit(**hints)
File "./sympy/integrals/transforms.py", line 121, in doit
self.function_variable, self.transform_variable, **hints)
File "./sympy/integrals/transforms.py", line 819, in _compute_transform
return _inverse_mellin_transform(F, s, x, strip, **hints)
File "./sympy/integrals/transforms.py", line 206, in wrapper
res = func(*args, **kwargs)
File "./sympy/integrals/transforms.py", line 746, in _inverse_mellin_transform
h = hyperexpand(G)
File "./sympy/simplify/hyperexpand.py", line 2491, in hyperexpand
return f.replace(hyper, do_replace).replace(meijerg, do_meijer)
File "./sympy/core/basic.py", line 1508, in replace
rv = bottom_up(self, rec_replace, atoms=True)
File "./sympy/simplify/simplify.py", line 1152, in bottom_up
rv = F(rv)
File "./sympy/core/basic.py", line 1489, in rec_replace
new = _value(expr, result)
File "./sympy/core/basic.py", line 1437, in <lambda>
_value = lambda expr, result: value(*expr.args)
File "./sympy/simplify/hyperexpand.py", line 2488, in do_meijer
allow_hyper, rewrite=rewrite, place=place)
File "./sympy/simplify/hyperexpand.py", line 2373, in _meijergexpand
t, 1/z0)
File "./sympy/simplify/hyperexpand.py", line 2346, in do_slater
t, premult, au, rewrite=None)
File "./sympy/simplify/hyperexpand.py", line 2042, in _hyperexpand
formula = try_lerchphi(func)
File "./sympy/simplify/hyperexpand.py", line 1783, in try_lerchphi
part = apart(numer/denom, t)
File "./sympy/utilities/decorator.py", line 36, in threaded_func
return func(expr, *args, **kwargs)
File "./sympy/polys/partfrac.py", line 85, in apart
raise PolynomialError(msg)
sympy.polys.polyerrors.PolynomialError: RisingFactorial(_t + 1, 2.0)
contains an element of the set of generators.

It looks like somewhere in the integration algorithm it creates
RisingFactorial(t + 1, 2.0) expecting it to be converted to a
polynomial. But this only works if the second argument is an integer,
not a float

>>> RisingFactorial(t + 1, 2)
(t + 1)*(t + 2)
>>> RisingFactorial(t + 1, 2.0)
RisingFactorial(t + 1, 2.0)

I would suggest opening an issue for this.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxQkmTvjGMWwe40mw3EFrk2eF2%2BY3hjiPpT9Xde8OYJb0A%40mail.gmail.com.

Gagandeep Singh (B17CS021)

unread,
May 6, 2020, 5:28:29 PM5/6/20
to sy...@googlegroups.com
May be using `as_int` with the second argument in `RisingFactorial` should solve the problem. Just an idea. 

With Regards,
Gagandeep Singh
Github - https://github.com/czgdp1807
LinkedIn - https://www.linkedin.com/in/czgdp1807

Kalevi Suominen

unread,
May 7, 2020, 3:01:26 AM5/7/20
to sympy
Actually, the second argument of rising (and falling) factorial should always be an integer. I think that `as_int` would be a good solution, it is reasonable to allow integral floating point values. It would probably raise an error in some cases where unevaluated expression is currently returned, but those should be fixed anyway.

Kalevi Suominen
> > To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.

> > To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/aa5c4270-6c71-485b-8354-b5ea39e9c380%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.

> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxQkmTvjGMWwe40mw3EFrk2eF2%2BY3hjiPpT9Xde8OYJb0A%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.

Javier Arantegui

unread,
May 7, 2020, 3:27:11 AM5/7/20
to sy...@googlegroups.com
Hello,

On Wed, May 6, 2020 at 11:00 PM Oscar Benjamin
<oscar.j....@gmail.com> wrote:

> It is usually better not to use floats in sympy.

Thank you for the tip. I read it yesterday and today I remembered
reading something about this some months ago. It's in the Gotchas
webpage:

https://docs.sympy.org/latest/tutorial/gotchas.html

Using Rational(), it works:

inverse_laplace_transform(Rational(1,2)/(Rational(1,2)*s**2+Rational(3,2)*s+1)*2/s,
s, t)

Javier
Reply all
Reply to author
Forward
0 new messages