How to solve matrix equation with sympy?
Asked Answered
C

1

9

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?

Considered answered 2/4, 2014 at 18:29 Comment(3)
Are the elements of M,N symbols or simply numbers?Nerveless
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 )Considered
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? github.com/sympy/sympy/issuesSheffie
A
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.

Armory answered 6/4, 2014 at 17:34 Comment(4)
hmm, good but not good enough... Someone will have to implement the real thing...Considered
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.Armory
If I am not mistaken, the result of solve(M*x + N*y, x) should be $-M^{-1} N y$.Centralize
Opened a new issue, "solve(Mx + Ny, x) fails for non-commutative symbols": github.com/sympy/sympy/issues/12258Centralize

© 2022 - 2024 — McMap. All rights reserved.