commutative=False assumption not always honored

66 views
Skip to first unread message

r.du...@gmail.com

unread,
Sep 4, 2017, 11:25:10 AM9/4/17
to sy...@googlegroups.com
Hello

I'm playing with SympyLive and it seems to me that the commutative=False assumption is not honored by some functions, resulting in wrong results.

Example: When A and b are non-commuting symbols

factor(A**2+B**2+2*A*B) gives (A+B)**2 but it should do nothing i.e. A**2+B**2+2*A*B

and itermonomials should give also the B*A monomial. Or at list they should refuse to answer when some variables are not commuting.

Hope this helps
ric

#############################
Python console for SymPy 1.0 (Python 2.7.12)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)

>>> from sympy.polys.orderings import *
>>> from sympy.polys.monomials import *
>>> A, B = symbols('A,B',commutative=False)
>>> A.is_commutative
False
>>> A*B-B*A ## does nothing: OK
A*B - B*A
>>> factor(A**2+B**2+2*A*B) ## wrong result
(A + B)**2
>>> itermonomials([A,B], 2)) ## wrong result
set([1, A, A**2, B, B**2, A*B])

Valeriia Gladkova

unread,
Sep 8, 2017, 10:11:27 AM9/8/17
to sympy
Yes, there are other problems with non-commutative things, like https://github.com/sympy/sympy/issues/12576#issuecomment-308384814 or https://github.com/sympy/sympy/issues/12258. It feels like non-commutative symbols weren't originally planned and when they were introduced, not all of the existing algorithms were changed to account for them. Or maybe the reason is that when those algorithms were being written, the authors forgot about non-commutativity. Hard to say, but something should be done about it. Could you open an issue if you haven't already done so?

Aaron Meurer

unread,
Sep 8, 2017, 4:24:42 PM9/8/17
to sy...@googlegroups.com
This is a bug (I noted it at https://github.com/sympy/sympy/issues/6225). 

The problem is that if someone writes an algorithm without explicitly checking for noncommutatives, it is easy to get a wrong result. It's difficult to guard against this in a universal way because often the assumption is implicit. There are a dozen different ways this could happen. For example, code might assume that (a*b)**n = a**n*b**n, or it might assume that (a + b)**2 = a**2 + 2*a*b + b**2. 

So in many cases there are some issues with noncommutatives, especially since they are not used as often as normal commutative expressions. But as we find more and more of them, we fix them and add tests for them, so they can't be broken again. 

Aaron Meurer

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/1d3c4817-ea9b-479f-b09f-263b4468d791%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages