为什么SymPy没有集成这个功能?

时间:2018-03-08 21:03:35

标签: python sympy symbolic-math

我有以下内容:

x,x1,x2,t=symbols('x x1 x2 t')
f=t*x1*x2*(x-t)**(-0.5)
integrate(f,t)

我可以与xx1x2进行整合,但是当我尝试整合t(这就是我想要的)时,我得到了以下错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/sympy/integrals/integrals.py", line 1295, in integrate
    risch=risch, manual=manual)
  File "/usr/local/lib/python2.7/dist-packages/sympy/integrals/integrals.py", line 486, in doit
    conds=conds)
  File "/usr/local/lib/python2.7/dist-packages/sympy/integrals/integrals.py", line 908, in _eval_integral
    h = meijerint_indefinite(g, x)
  File "/usr/local/lib/python2.7/dist-packages/sympy/integrals/meijerint.py", line 1612, in meijerint_indefinite
    res = _meijerint_indefinite_1(f.subs(x, x + a), x)
  File "/usr/local/lib/python2.7/dist-packages/sympy/integrals/meijerint.py", line 1677, in _meijerint_indefinite_1
    r = hyperexpand(r.subs(t, a*x**b), place=place)
  File "/usr/local/lib/python2.7/dist-packages/sympy/simplify/hyperexpand.py", line 2473, in hyperexpand
    return f.replace(hyper, do_replace).replace(meijerg, do_meijer)
  File "/usr/local/lib/python2.7/dist-packages/sympy/core/basic.py", line 1408, in replace
    rv = bottom_up(self, rec_replace, atoms=True)
  File "/usr/local/lib/python2.7/dist-packages/sympy/simplify/simplify.py", line 999, in bottom_up
    rv = F(rv)
  File "/usr/local/lib/python2.7/dist-packages/sympy/core/basic.py", line 1393, in rec_replace
    new = _value(expr, result)
  File "/usr/local/lib/python2.7/dist-packages/sympy/core/basic.py", line 1336, in <lambda>
    _value = lambda expr, result: value(*expr.args)
  File "/usr/local/lib/python2.7/dist-packages/sympy/simplify/hyperexpand.py", line 2470, in do_meijer
    allow_hyper, rewrite=rewrite, place=place)
  File "/usr/local/lib/python2.7/dist-packages/sympy/simplify/hyperexpand.py", line 2355, in _meijergexpand
    t, 1/z0)
  File "/usr/local/lib/python2.7/dist-packages/sympy/simplify/hyperexpand.py", line 2281, in do_slater
    t, premult, bh, rewrite=None)
  File "/usr/local/lib/python2.7/dist-packages/sympy/simplify/hyperexpand.py", line 2038, in _hyperexpand
    ops += devise_plan(func, formula.func, z0)
  File "/usr/local/lib/python2.7/dist-packages/sympy/simplify/hyperexpand.py", line 1615, in devise_plan
    raise ValueError('Non-suitable parameters.')
ValueError: Non-suitable parameters.

这个错误是什么意思?

1 个答案:

答案 0 :(得分:0)

SymPy的许多例程都很难处理浮点数,特别是在指数中。尽可能使用有理数字。 common gotchas and pitfalls中的更多讨论。

>>> x,x1,x2,t=symbols('x x1 x2 t')
>>> f=t*x1*x2*(x-t)**(-Rational('0.5'))
>>> integrate(f,t).simplify()
Piecewise((2*sqrt(x)*x1*x2*(-I*t**2*sqrt((t - x)/x) - I*t*x*sqrt((t - x)/x) + 2*t*x + 2*I*x**2*sqrt((t - x)/x) - 2*x**2)/(3*(t - x)), Abs(t/x) > 1), (2*sqrt(x)*x1*x2*(-t**2*sqrt((-t + x)/x) - t*x*sqrt((-t + x)/x) + 2*t*x + 2*x**2*sqrt((-t + x)/x) - 2*x**2)/(3*(t - x)), True))

不是最好的答案,但这是一个答案。如果你知道x和t是肯定的,那么从简化开始就可以帮助简化它。还有两种情况,取决于Abs(t/x)是否大于1。

创造理性指数1/2的方法:

  • Rational(1, 2)
  • Rational('0.5')
  • S(1)/2
  • 或只使用具有相同效果的sqrt函数。