在Sympy中与非交换符号集成

时间:2018-02-27 01:08:23

标签: python sympy

假设我有

x=Symbol('x',commutative=False)

和一些表达

expr = x**2+x

然后如果你试图整合这个

integrate(expr,x)

出现以下错误:

  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 774, in _eval_integral
    poly = f.as_poly(x)
  File "/usr/local/lib/python2.7/dist-packages/sympy/core/basic.py", line 706, in as_poly
    poly = Poly(self, *gens, **args)
  File "/usr/local/lib/python2.7/dist-packages/sympy/polys/polytools.py", line 113, in __new__
    opt = options.build_options(gens, args)
  File "/usr/local/lib/python2.7/dist-packages/sympy/polys/polyoptions.py", line 731, in build_options
    return Options(gens, args)
  File "/usr/local/lib/python2.7/dist-packages/sympy/polys/polyoptions.py", line 154, in __init__
    preprocess_options(args)
  File "/usr/local/lib/python2.7/dist-packages/sympy/polys/polyoptions.py", line 152, in preprocess_options
    self[option] = cls.preprocess(value)
  File "/usr/local/lib/python2.7/dist-packages/sympy/polys/polyoptions.py", line 293, in preprocess
    raise GeneratorsError("non-commutative generators: %s" % str(gens))
sympy.polys.polyerrors.GeneratorsError: non-commutative generators: (x,)

有什么方法可以解决这个问题吗?因为我真的需要x不可交换

1 个答案:

答案 0 :(得分:1)

事实上,先进的集成方法(Risch,Meijer G)需要变量是可交换的;他们依靠像往常一样工作的多项式。将集成方法限制为&#34;手册&#34; (基本微积分技术)避免了错误:

x = Symbol('x',commutative=False)
integrate(x**2 + x, x, manual=True)

返回x**2/2 + x**3/3。权衡是只评估简单积分(在微积分课程的水平上)。

那就是说,我并非100%确定manualintegrate模块中的所有集成规则都是在考虑可能缺乏交换性的情况下编写的。