# How do you solve a linear equation of matrices? [duplicate]

The function Solve works fine for scalars:

In[]:= Solve[A x - x B + C == 0, x]
Out[]= {{x -> -(C/(A - B))}}


When using matrices however,

mA = {{1, 0}, {0, 1}};
mB = {{2, 0}, {0, 2}};
mC = {{3, 1}, {0, 3}};

Solve[mA.x - x.mB + mC == 0, x]

Out[]= {}


There is no output, though there should be (x={{3,1},{0,3}}, I guess). The same happens when the matrix multiplication operator "." is replaced with a white space.

How can linear matrix equations like these be solved?

• @Artes I'm sorry, I don't see how the answer to the linked question could solve a linear system like this (note also that x is multiplied from the left with mA and from the right with mB). Apr 30 '16 at 17:56
• You should note that this is a so-called Lyapunov equation and Mathematica has a dedicated function for solving it! (At least for the cases where C is positive definite) Apr 30 '16 at 19:57
• @sebhofer That's pretty sweet, it's a pity then that this is just a trial equation for something bigger. Apr 30 '16 at 20:14
• Oh, also note that I haven't realized your equation has two distinct matrices A and B so its not a Lyapunov equation. Sorry for the confusion! Apr 30 '16 at 20:19

With[{x = Array[x, Dimensions[mA]]}, Solve[mA .x - x. mB + mC == 0, Flatten@x]] With[{x = Array[x, Dimensions[mA]]},  x /. Solve[mA .x - x. mB + mC == 0, Flatten@x]]