8

In sympy, given a matrix equation

M * x + N * y = 0 (or more complicated..)

how to solve this for x? (M,N = matrices, x,y = vectors)

I tried this with normal symbols, but obviously this failed. Using MatrixSymbol was not working as well. Is there some way to do it, or is sympy not capable of doing it?

Dirk
  • 1,789
  • 2
  • 21
  • 31
  • Are the elements of `M,N` symbols or simply numbers? – Hooked Apr 02 '14 at 20:06
  • 1
    the matrices M and N should not be defined by their elements, I'd just like to obtain the result x = M^-1 * ( - N * y ) – Dirk Apr 03 '14 at 05:54
  • 2
    SymPy doesn't currently support `solve` on matrix expressions but it could if enough people ask for it. Perhaps you could raise an issue on github? http://github.com/sympy/sympy/issues/ – MRocklin Apr 04 '14 at 23:26

1 Answers1

8

As MRocklin noted, MatrixExpressions don't support this yet, but noncommutative symbols do:

In [13]: M, N, x, y = symbols('M N x y', commutative=False)

In [15]: solve(M*x + N*y, x)
Out[15]:
⎡      -1⎤
⎣-N⋅y⋅M  ⎦

Unlike MatrixExpressions, noncommutative symbols don't have a notion of shape, so you'll need to keep track of that yourself. But this also shows that the basic things to implement this for MatrixExpression are already there. It will probably be easy to implement.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • hmm, good but not good enough... Someone will have to implement the real thing... – Dirk Apr 08 '14 at 08:17
  • 1
    As I said, it should be easy if you want to give it a shot. It should just be a matter of telling `solve` to treat matrix expressions the same as noncommutative symbols. – asmeurer Apr 08 '14 at 15:14
  • 1
    If I am not mistaken, the result of `solve(M*x + N*y, x)` should be $-M^{-1} N y$. – ken Mar 06 '17 at 13:23
  • 2
    Opened a new issue, "solve(M*x + N*y, x) fails for non-commutative symbols": https://github.com/sympy/sympy/issues/12258 – ken Mar 06 '17 at 21:57