简体   繁体   English

"错误:无法使用 sympy 获得拉普拉斯逆变换表达式"

[英]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:我一直在尝试用 sympy 为以下传递函数 wlt 计算拉普拉斯逆变换:

                             -2⋅s         
         (1.0e-8⋅s + 0.02)⋅ℯ             
────────────────────────────────────────
  ⎛         2                           ⎞
s⋅⎝1.0e-11⋅s  + 2.0000003e-5⋅s + 0.00177⎠

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 定义为实数:

t = sympy.Symbol('t', real=True)

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.

我在 sp.inverse_laplace_transform(...) 中遇到了类似的错误,并且将 Sympy 从 1.9 降级到 1.7.1 有所帮助。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM