I want to solve quite simple equations with symbolic coefficients:
from sympy import *
a, b = symbols('a b', commutative=False)
x = IndexedBase('x')
basis = [a, b, a * b - b * a]
el = b * a - a * b
coefs = [x[k] for k in range(len(basis))]
eq = el - sum([c * bel for c, bel in zip(coefs, basis)])
solve(eq.expand(), coefs)
The equation is -x[0]*a - x[1]*b + x[2]*(-a*b + b*a) - a*b + b*a
==0, and the solution is, obviously, x[0]=0, x[1]=0, x[2]=-1
, but SymPy returns [(-x[1]*a**(-1)*b + x[2]*a**(-1)*b*a - x[2]*b + a**(-1)*b*a - b, x[1], x[2])]
.
How do I solve this type of equations?
I could use collect()
to get coeffs, extract them, and then solve all the resulting equations. But at the moment collect()
doesn't work for non-commutative symbols.
As far as I understand, the problem is that SymPy thinks that a
can be equal to a*b
(or that they can be related in some other way). How can I tell it that these symbols and products are definitely distinct, linearly independent, and cannot be expressed one by means of another?