sympy / sympy-live

The source for the code at live.sympy.org

Home Page:https://live.sympy.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Non-commutative multiply triggers inappropriate substitutions

TobiSchluter opened this issue · comments

I was playing around with sympy a bit, but almost immediately ran into trouble. Even though I think it's a nice tool, I ran into this too fast to trust it :(

In sympy live I enter:

a, b,c = symbols('a b c', commutative=False)

and I try two operations (x, y are is a symbol in sympy live):

>>> expand((a*(x*b*c+c*b*a)*y)).subs([(b*c,-a),(a*a,-1),(c*b,a)])
         3
x*y + y*a 
>>> expand((a*(x*b*c+c*b*a)*y)).subs([(b*c,-a),(a*a,-1),(c*b,a)]).subs([(b*c,-a),(a*a,-1),(c*b,a)])
x*y - I*y

Note how the imaginary unit appears out of nowhere when the substitution is applied twice. I.e. a**3 is replaced by the imaginary unit.

I think the bug in the implementation is that it infers from a*a == -1 that a == I. This is not correct because the multiplication rules for non-commutative objects are not complex numbers. To quote a familiar example, quaternions have i*i == -1, and j*j == -1 but j is very different from i.

I see how one could work around this by defining a special symbol 'minusOne' together with a very carefully assembled set of substitution rules that mimic multiplication by a scalar, but is that the intended use?

This isn't a SymPy live issue. Can you open it in the main SymPy repo?

Thanks, done. Sorry about the noise and for not looking deeper when I wondered about the low number of bugs :)

Now number 12576 in the sympy tracker.