# [sympy] Solving for a matrix?

```Dear list.

I have this equation:```
```
a = M * b,

where a and b are column vectors and M is a 4x4 matrix.
a and b consist of quite simple expressions, M is unknown:

>>> import sympy as sp
>>> a0, a1, a2, a3 = sp.symbols('a:4')
>>> a = sp.Matrix([a3, a2, a1, a0])
>>> b = sp.Matrix([a0, a3 + a2 + a1 + a0, a1, 3 * a3 + 2 * a2 + a1])
>>> M = sp.MatrixSymbol('M', 4, 4)
>>> sp.Eq(a, M * b)
Eq(Matrix([[a3], [a2], [a1], [a0]]), M*Matrix([
[               a0],
[a0 + a1 + a2 + a3],
[               a1],
[ a1 + 2*a2 + 3*a3]]))

What's the most straightforward way to solve this for M?

I have actually found a way, but it seems a bit non-obvious to me:

>>> def get_value(i, j):
...     return b[i].as_coefficients_dict()[a[j]]
...
>>> M_inv = sp.Matrix(4, 4, get_value)
>>> M_inv.inv()
Matrix([
[ 2, -2,  1,  1],
[-3,  3, -2, -1],
[ 0,  0,  1,  0],
[ 1,  0,  0,  0]])

This is the solution I'm looking for, but I was hoping there is a
better way to get it.
Also, the way I did it I had to know that I could look for the inverse
and then undo the inverse afterwards, but I would prefer if there is a
solution without this manual step.

cheers,
Matthias

--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
To post to this group, send email to sympy@googlegroups.com.
To view this discussion on the web visit