issue solving linear systems involving non commutative variables with sympy

28 Views Asked by At

I am trying to solve a linear system of equations involving non commutative variables using sympy solvers.

It doesn't work with solve(), which does not seem to accept systems of equations involving non commutative variables.

It doesn't work either with solve_linear_system(), which accepts non commutative variables, but expects a matrix representation of the system, which might break the non commutativity requirement.

Two minimal working example below, 1st one to show the issue with solve(), 2nd one to show that with solve_linear_system().

Minimal working example 1:

import sympy as sy
a, b = sy.symbols('a b', commutative=False)

eq1 = a-1
eq2 = b-2

system = sy.Matrix([[1,0,1],[0,1,2]])

sy.solve([eq1,eq2],[a,b]) # GeneratorsError: non-commutative generators: (a,)
sy.solve_linear_system(system,a,b) # works fine in this particularly simple case

In this example, solve_linear_system() works ok while solve() returns the error below:

sy.solve([eq1,eq2],[a,b])
Traceback (most recent call last):

  File "<ipython-input-7-e0159133dbda>", line 1, in <module>
    sy.solve([eq1,eq2],[a,b])

  File "C:\Users\util-mahe\anaconda3\lib\site-packages\sympy\solvers\solvers.py", line 1147, in solve
    linear, solution = _solve_system(f, symbols, **flags)

  File "C:\Users\util-mahe\anaconda3\lib\site-packages\sympy\solvers\solvers.py", line 1765, in _solve_system
    _linear, subsol = _solve_system(subexpr, subsyms, **flags)

  File "C:\Users\util-mahe\anaconda3\lib\site-packages\sympy\solvers\solvers.py", line 1799, in _solve_system
    poly = g.as_poly(*symbols, extension=True)

  File "C:\Users\util-mahe\anaconda3\lib\site-packages\sympy\core\expr.py", line 1102, in as_poly
    poly = Poly(self, *gens, **args)

  File "C:\Users\util-mahe\anaconda3\lib\site-packages\sympy\polys\polytools.py", line 164, in __new__
    opt = options.build_options(gens, args)

  File "C:\Users\util-mahe\anaconda3\lib\site-packages\sympy\polys\polyoptions.py", line 744, in build_options
    return Options(gens, args)

  File "C:\Users\util-mahe\anaconda3\lib\site-packages\sympy\polys\polyoptions.py", line 153, in __init__
    preprocess_options(args)

  File "C:\Users\util-mahe\anaconda3\lib\site-packages\sympy\polys\polyoptions.py", line 151, in preprocess_options
    self[option] = cls.preprocess(value)

  File "C:\Users\util-mahe\anaconda3\lib\site-packages\sympy\polys\polyoptions.py", line 292, in preprocess
    raise GeneratorsError("non-commutative generators: %s" % str(gens))

GeneratorsError: non-commutative generators: (a,)

Minimal working example 2:

import sympy as sy
a, b, w, z = sy.symbols('a b w z', commutative=False)

eq1 = z*a-w
eq2 = b-2

system = sy.Matrix([[z,0,w],[0,1,2]])

sy.solve_linear_system(system,a,b)
sy.solve_linear_system_LU(system,[a,b])

In this example, solve_linear_system() returns a=w*z^(-1) instead of a=z^(-1)*w because the matrix representation does not allow to state that the initial equation involves z*a rather than a*z. The issue is the same with solve_linear_system_LU().

I found several similar issues regarding solving with noncommutative variables (see bellow), but none of them adresses the issue I am raising here.

I am using sympy 1.12

Could someone help?

0

There are 0 best solutions below