为什么 SymPy 不集成这个功能呢?
Why doesn't SymPy integrate this function?
我有以下内容:
x,x1,x2,t=symbols('x x1 x2 t')
f=t*x1*x2*(x-t)**(-0.5)
integrate(f,t)
我可以针对 x
、x1
和 x2
进行整合,但是当我尝试针对 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.
这个错误是什么意思?
许多 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
函数,效果一样
我有以下内容:
x,x1,x2,t=symbols('x x1 x2 t')
f=t*x1*x2*(x-t)**(-0.5)
integrate(f,t)
我可以针对 x
、x1
和 x2
进行整合,但是当我尝试针对 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.
这个错误是什么意思?
许多 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
函数,效果一样