Sympy中的手动集成无法与非交换符号

时间:2018-02-28 19:36:15

标签: python sympy

我有以下

x=Symbol('x',commutative=False)
y=Symbol('y',commutative=False)
expr = 2*x + 87*x*y + 7*y

现在,这可行

integrate(expr,y,manual=True)

因为它给出了

2*x*y + 87*x*y**2/2 + 7*y**2/2

但与x完全相同的事情失败了:

integrate(expr,x,manual=True)
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,)

为什么Sympy如此奇怪?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你似乎对此感到满意

integrate(2*x + 87*x*y + 7*y, y, manual=True)

返回

2*x*y + 87*x*y**2/2 + 7*y**2/2

但这个答案的第一个术语也可能是2*y*x。或x*y + y*x。这些都是不同的答案。那么,非交换符号的积分概念是否以明确的方式开始?也许不是说SymPy很奇怪,但问你的问题是。

此行为的具体原因是手动集成基于匹配某些模式。比如“常数时代的东西”模式:

coeff, f = integrand.as_independent(symbol)

方法as_independent按此顺序将产品拆分为independent * possibly_dependent。所以,

(x*y).as_independent(y)  # returns (x, y)
(x*y).as_independent(x)  # returns (1, x*y)

因此,当产品是非交换性时,仅在表达式前面识别常数因子。

我不认为如果不重写其中一个核心方法as_independent来支持非交换产品(可能返回independent * dependent * independent2),这对我来说很难解决。在做这项工作之前,我想知道目标(具有非通信变量的反衍生物)是否定义明确。