Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve matrix equation with sympy?

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?

like image 540
Dirk Avatar asked Apr 02 '14 18:04

Dirk


People also ask

How do you solve a matrix in Python?

To solve a linear matrix equation, use the numpy. linalg. solve() method in Python. The method computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.

How do you transpose a matrix in SymPy?

To actually compute the transpose, use the transpose() function, or the . T attribute of matrices. Represents the trace of a matrix expression. Represents a matrix using a function ( Lambda ) which gives outputs according to the coordinates of each matrix entries.


1 Answers

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.

like image 71
asmeurer Avatar answered Oct 09 '22 06:10

asmeurer