'ERROR: can't get the inverse laplace transform expression with sympy
I've been trying to calculate the inverse laplace transform with sympy for the follow transfer function wlt:
-2⋅s
(1.0e-8⋅s + 0.02)⋅ℯ
────────────────────────────────────────
⎛ 2 ⎞
s⋅⎝1.0e-11⋅s + 2.0000003e-5⋅s + 0.00177⎠
with the next expression:
wtl_t = sp.inverse_laplace_transform(wtl, s, t, noconds=True)
where
s = sp.symbols('s')
t = sp.symbols('t', positive=True)
but when I run the code, this errors appears:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/sympy/polys/polyutils.py", line 211, in _parallel_dict_from_expr_if_gens
monom[indices[base]] = exp
KeyError: RisingFactorial(_t + 1, 1.0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/sympy/polys/partfrac.py", line 81, in apart
(P, Q), opt = parallel_poly_from_expr((P, Q), x, **options)
File "/usr/local/lib/python3.9/dist-packages/sympy/polys/polytools.py", line 4340, in parallel_poly_from_expr
return _parallel_poly_from_expr(exprs, opt)
File "/usr/local/lib/python3.9/dist-packages/sympy/polys/polytools.py", line 4393, in _parallel_poly_from_expr
reps, opt = _parallel_dict_from_expr(exprs, opt)
File "/usr/local/lib/python3.9/dist-packages/sympy/polys/polyutils.py", line 332, in _parallel_dict_from_expr
reps, gens = _parallel_dict_from_expr_if_gens(exprs, opt)
File "/usr/local/lib/python3.9/dist-packages/sympy/polys/polyutils.py", line 216, in _parallel_dict_from_expr_if_gens
raise PolynomialError("%s contains an element of "
sympy.polys.polyerrors.PolynomialError: RisingFactorial(_t + 1, 1.0) contains an element of the set of generators.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/mnt/hgfs/FACULTAD/3er año/SDC/S&C_withPython/SDC_ex/GTP2_ex3.py", line 62, in <module>
wtl_t = sp.inverse_laplace_transform(wtl, s, t, noconds=True)
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 1341, in inverse_laplace_transform
return InverseLaplaceTransform(F, s, t, plane).doit(**hints)
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 130, in doit
return self._compute_transform(self.function,
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 1287, in _compute_transform
return _inverse_laplace_transform(F, s, t, self.fundamental_plane, **hints)
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 218, in wrapper
res = func(*args, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 1213, in _inverse_laplace_transform
f, cond = inverse_mellin_transform(F, s, exp(-t), (None, oo),
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 908, in inverse_mellin_transform
return InverseMellinTransform(F, s, x, strip[0], strip[1]).doit(**hints)
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 130, in doit
return self._compute_transform(self.function,
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 846, in _compute_transform
return _inverse_mellin_transform(F, s, x, strip, **hints)
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 218, in wrapper
res = func(*args, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/sympy/integrals/transforms.py", line 773, in _inverse_mellin_transform
h = hyperexpand(G)
File "/usr/local/lib/python3.9/dist-packages/sympy/simplify/hyperexpand.py", line 2513, in hyperexpand
return f.replace(hyper, do_replace).replace(meijerg, do_meijer)
File "/usr/local/lib/python3.9/dist-packages/sympy/core/basic.py", line 1497, in replace
rv = walk(self, rec_replace)
File "/usr/local/lib/python3.9/dist-packages/sympy/core/basic.py", line 1481, in walk
rv = F(rv)
File "/usr/local/lib/python3.9/dist-packages/sympy/core/basic.py", line 1490, in rec_replace
v = _value(expr, result)
File "/usr/local/lib/python3.9/dist-packages/sympy/core/basic.py", line 1419, in <lambda>
_value = lambda expr, result: value(*expr.args)
File "/usr/local/lib/python3.9/dist-packages/sympy/simplify/hyperexpand.py", line 2509, in do_meijer
r = _meijergexpand(G_Function(ap[0], ap[1], bq[0], bq[1]), z,
File "/usr/local/lib/python3.9/dist-packages/sympy/simplify/hyperexpand.py", line 2394, in _meijergexpand
slater2, cond2 = do_slater(tr(func.bm), tr(func.an), tr(func.bq), tr(func.ap),
File "/usr/local/lib/python3.9/dist-packages/sympy/simplify/hyperexpand.py", line 2320, in do_slater
hyp = _hyperexpand(Hyper_Function(nap, nbq), harg, ops,
File "/usr/local/lib/python3.9/dist-packages/sympy/simplify/hyperexpand.py", line 2061, in _hyperexpand
formula = try_lerchphi(func)
File "/usr/local/lib/python3.9/dist-packages/sympy/simplify/hyperexpand.py", line 1799, in try_lerchphi
part = apart(numer/denom, t)
File "/usr/local/lib/python3.9/dist-packages/sympy/utilities/decorator.py", line 34, in threaded_func
return func(expr, *args, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/sympy/polys/partfrac.py", line 84, in apart
raise PolynomialError(msg)
sympy.polys.polyerrors.PolynomialError: RisingFactorial(_t + 1, 1.0) contains an element of the set of generators.
I also tried with partial fractions and factorize the TF, but nothing happened.
Solution 1:[1]
I had a similar error with sp.inverse_laplace_transform(...) and downgrading Sympy from 1.9 to 1.7.1 helped.
Solution 2:[2]
I don't know what is the problem in your case but maybe try:
def invL(F):
return sympy.inverse_laplace_transform(F, s, t)
invL(wtl)
Also you need to define t as real:
t = sympy.Symbol('t', real=True)
Solution 3:[3]
There seems to be something...interesting...between ints and floats:
import sympy as sym
sym.init_printing()
sym.var('t', real=True)
sym.var('s')
# this works
F = 10/(2*s**3+5*s**2+s)
sym.inverse_laplace_transform(F, s, t)
# this works
G = F/5
sym.inverse_laplace_transform(G, s, t)
# this fails
H = F/5.0
sym.inverse_laplace_transform(H, s, t)
# this fails
I = 10.0/(2.0*s**3+5.0*s**2+1.0*s)
sym.inverse_laplace_transform(I, s, t)
Here's the fail (not for the faint of heart):
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\anaconda3_2021_05\lib\site-packages\sympy\polys\polyutils.py in _parallel_dict_from_expr_if_gens(exprs, opt)
210
--> 211 monom[indices[base]] = exp
212 except KeyError:
KeyError: RisingFactorial(_t + 1, 1.0)
During handling of the above exception, another exception occurred:
PolynomialError Traceback (most recent call last)
~\anaconda3_2021_05\lib\site-packages\sympy\polys\partfrac.py in apart(f, x, full, **options)
80 try:
---> 81 (P, Q), opt = parallel_poly_from_expr((P, Q), x, **options)
82 except PolynomialError as msg:
~\anaconda3_2021_05\lib\site-packages\sympy\polys\polytools.py in parallel_poly_from_expr(exprs, *gens, **args)
4339 opt = options.build_options(gens, args)
-> 4340 return _parallel_poly_from_expr(exprs, opt)
4341
~\anaconda3_2021_05\lib\site-packages\sympy\polys\polytools.py in _parallel_poly_from_expr(exprs, opt)
4392
-> 4393 reps, opt = _parallel_dict_from_expr(exprs, opt)
4394 if not opt.gens:
~\anaconda3_2021_05\lib\site-packages\sympy\polys\polyutils.py in _parallel_dict_from_expr(exprs, opt)
331 if opt.gens:
--> 332 reps, gens = _parallel_dict_from_expr_if_gens(exprs, opt)
333 else:
~\anaconda3_2021_05\lib\site-packages\sympy\polys\polyutils.py in _parallel_dict_from_expr_if_gens(exprs, opt)
215 else:
--> 216 raise PolynomialError("%s contains an element of "
217 "the set of generators." % factor)
PolynomialError: RisingFactorial(_t + 1, 1.0) contains an element of the set of generators.
During handling of the above exception, another exception occurred:
PolynomialError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8232/971710189.py in <module>
1 H = F/5.0
----> 2 sym.inverse_laplace_transform(H, s, t)
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in inverse_laplace_transform(F, s, t, plane, **hints)
1420 if isinstance(F, MatrixBase) and hasattr(F, 'applyfunc'):
1421 return F.applyfunc(lambda Fij: inverse_laplace_transform(Fij, s, t, plane, **hints))
-> 1422 return InverseLaplaceTransform(F, s, t, plane).doit(**hints)
1423
1424
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in doit(self, **hints)
134 if try_directly:
135 try:
--> 136 return self._compute_transform(self.function,
137 self.function_variable, self.transform_variable, **hints)
138 except IntegralTransformError:
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in _compute_transform(self, F, s, t, **hints)
1366
1367 def _compute_transform(self, F, s, t, **hints):
-> 1368 return _inverse_laplace_transform(F, s, t, self.fundamental_plane, **hints)
1369
1370 def _as_integral(self, F, s, t):
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in wrapper(noconds, *args, **kwargs)
222 @wraps(func)
223 def wrapper(*args, noconds=default, **kwargs):
--> 224 res = func(*args, **kwargs)
225 if noconds:
226 return res[0]
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in _inverse_laplace_transform(F, s, t_, plane, simplify)
1285
1286 if F.is_Add:
-> 1287 f = Add(*[_inverse_laplace_transform(X, s, t, plane, simplify)\
1288 for X in F.args])
1289 return _simplify(f.subs(t, t_), simplify), True
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in <listcomp>(.0)
1285
1286 if F.is_Add:
-> 1287 f = Add(*[_inverse_laplace_transform(X, s, t, plane, simplify)\
1288 for X in F.args])
1289 return _simplify(f.subs(t, t_), simplify), True
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in wrapper(noconds, *args, **kwargs)
222 @wraps(func)
223 def wrapper(*args, noconds=default, **kwargs):
--> 224 res = func(*args, **kwargs)
225 if noconds:
226 return res[0]
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in _inverse_laplace_transform(F, s, t_, plane, simplify)
1290
1291 try:
-> 1292 f, cond = inverse_mellin_transform(F, s, exp(-t), (None, oo),
1293 needeval=True, noconds=False)
1294 except IntegralTransformError:
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in inverse_mellin_transform(F, s, x, strip, **hints)
912 hankel_transform, inverse_hankel_transform
913 """
--> 914 return InverseMellinTransform(F, s, x, strip[0], strip[1]).doit(**hints)
915
916
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in doit(self, **hints)
134 if try_directly:
135 try:
--> 136 return self._compute_transform(self.function,
137 self.function_variable, self.transform_variable, **hints)
138 except IntegralTransformError:
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in _compute_transform(self, F, s, x, **hints)
850 'Component %s not recognised.' % f)
851 strip = self.fundamental_strip
--> 852 return _inverse_mellin_transform(F, s, x, strip, **hints)
853
854 def _as_integral(self, F, s, x):
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in wrapper(noconds, *args, **kwargs)
222 @wraps(func)
223 def wrapper(*args, noconds=default, **kwargs):
--> 224 res = func(*args, **kwargs)
225 if noconds:
226 return res[0]
~\anaconda3_2021_05\lib\site-packages\sympy\integrals\transforms.py in _inverse_mellin_transform(F, s, x_, strip, as_meijerg)
777 else:
778 try:
--> 779 h = hyperexpand(G)
780 except NotImplementedError:
781 raise IntegralTransformError(
~\anaconda3_2021_05\lib\site-packages\sympy\simplify\hyperexpand.py in hyperexpand(f, allow_hyper, rewrite, place)
2511 if not r.has(nan, zoo, oo, -oo):
2512 return r
-> 2513 return f.replace(hyper, do_replace).replace(meijerg, do_meijer)
~\anaconda3_2021_05\lib\site-packages\sympy\core\basic.py in replace(self, query, value, map, simultaneous, exact)
1496 return expr
1497
-> 1498 rv = walk(self, rec_replace)
1499 return (rv, mapping) if map else rv
1500
~\anaconda3_2021_05\lib\site-packages\sympy\core\basic.py in walk(rv, F)
1480 if rv == e and e != newargs[i]:
1481 return rv
-> 1482 rv = F(rv)
1483 return rv
1484
~\anaconda3_2021_05\lib\site-packages\sympy\core\basic.py in rec_replace(expr)
1489 result = _query(expr)
1490 if result or result == {}:
-> 1491 v = _value(expr, result)
1492 if v is not None and v != expr:
1493 if map:
~\anaconda3_2021_05\lib\site-packages\sympy\core\basic.py in <lambda>(expr, result)
1418 _value = lambda expr, result: value(*expr.args)
1419 elif callable(value):
-> 1420 _value = lambda expr, result: value(*expr.args)
1421 else:
1422 raise TypeError(
~\anaconda3_2021_05\lib\site-packages\sympy\simplify\hyperexpand.py in do_meijer(ap, bq, z)
2507
2508 def do_meijer(ap, bq, z):
-> 2509 r = _meijergexpand(G_Function(ap[0], ap[1], bq[0], bq[1]), z,
2510 allow_hyper, rewrite=rewrite, place=place)
2511 if not r.has(nan, zoo, oo, -oo):
~\anaconda3_2021_05\lib\site-packages\sympy\simplify\hyperexpand.py in _meijergexpand(func, z0, allow_hyper, rewrite, place)
2392 for op in ops:
2393 op._poly = Poly(op._poly.subs({z: 1/t, _x: -_x}), _x)
-> 2394 slater2, cond2 = do_slater(tr(func.bm), tr(func.an), tr(func.bq), tr(func.ap),
2395 t, 1/z0)
2396
~\anaconda3_2021_05\lib\site-packages\sympy\simplify\hyperexpand.py in do_slater(an, bm, ap, bq, z, zfinal)
2318 # t*k ... we are using polar numbers for consistency!
2319 premult = (t/k)**bh
-> 2320 hyp = _hyperexpand(Hyper_Function(nap, nbq), harg, ops,
2321 t, premult, bh, rewrite=None)
2322 res += fac * hyp
~\anaconda3_2021_05\lib\site-packages\sympy\simplify\hyperexpand.py in _hyperexpand(func, z, ops0, z0, premult, prem, rewrite)
2059 # Now try a lerch phi formula
2060 if formula is None:
-> 2061 formula = try_lerchphi(func)
2062
2063 if formula is None:
~\anaconda3_2021_05\lib\site-packages\sympy\simplify\hyperexpand.py in try_lerchphi(func)
1797 # a*t**b (b a non-negative integer), and a dict terms, where
1798 # terms[a] = [(b, c)] means that there is a term b/(t-a)**c.
-> 1799 part = apart(numer/denom, t)
1800 args = Add.make_args(part)
1801 monomials = []
~\anaconda3_2021_05\lib\site-packages\sympy\utilities\decorator.py in threaded_func(expr, *args, **kwargs)
32 func(expr.rhs, *args, **kwargs))
33 else:
---> 34 return func(expr, *args, **kwargs)
35
36 return threaded_func
~\anaconda3_2021_05\lib\site-packages\sympy\polys\partfrac.py in apart(f, x, full, **options)
82 except PolynomialError as msg:
83 if f.is_commutative:
---> 84 raise PolynomialError(msg)
85 # non-commutative
86 if f.is_Mul:
PolynomialError: RisingFactorial(_t + 1, 1.0) contains an element of the set of generators.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Huzeyfe E. |
| Solution 2 | ouflak |
| Solution 3 | DukeEgr93 |
