TITLE: Solving matrix equations of the form $X = AXA^T + C$
QUESTION [11 upvotes]: I'm trying to solve this matrix equation:
$$X = AXA^T + C$$
In particular,
$$
X = \begin{bmatrix} 1.5 & 1 \\ -0.7 & 0 \end{bmatrix}
X
\begin{bmatrix} 1.5 & -0.7 \\ 1 & 0 \end{bmatrix}
+ \begin{bmatrix} 1 & 0.5 \\ 0.5 & 0.25 \end{bmatrix}.
$$
I started by writing $X$ in open form.
$$X = \begin{bmatrix} a & b \\ c & d \end{bmatrix}$$
The equation becomes
$$
\begin{bmatrix} a & b \\ c & d \end{bmatrix}
= \begin{bmatrix} 1.5 & 1 \\ -0.7 & 0 \end{bmatrix}
\begin{bmatrix} a & b \\ c & d \end{bmatrix}
\begin{bmatrix} 1.5 & -0.7 \\ 1 & 0 \end{bmatrix}
+ \begin{bmatrix} 1 & 0.5 \\ 0.5 & 0.25 \end{bmatrix} \\
\begin{bmatrix} a & b \\ c & d \end{bmatrix}
= \begin{bmatrix} 1.5a+c & 1.5b+d \\ -0.7a & -0.7b \end{bmatrix}
\begin{bmatrix} 1.5 & -0.7 \\ 1 & 0 \end{bmatrix}
+ \begin{bmatrix} 1 & 0.5 \\ 0.5 & 0.25 \end{bmatrix} \\
\begin{bmatrix} a & b \\ c & d \end{bmatrix}
= \begin{bmatrix} 2.25a+1.5b+1.5c+d & -1.05a-0.7c \\ -1.05a-0.7b & 0.49a \end{bmatrix}
+ \begin{bmatrix} 1 & 0.5 \\ 0.5 & 0.25 \end{bmatrix} \\
\begin{bmatrix} a-1 & b-0.5 \\ c-0.5 & d-0.25 \end{bmatrix}
= \begin{bmatrix} 2.25a+1.5b+1.5c+d & -1.05a-0.7c \\ -1.05a-0.7b & 0.49a \end{bmatrix} \\
\begin{bmatrix} -1 & -0.5 \\ -0.5 & -0.25 \end{bmatrix}
= \begin{bmatrix} 1.25a+1.5b+1.5c+d & -1.05a-b-0.7c \\ -1.05a-0.7b-c & 0.49a-d \end{bmatrix} \\
$$
Equating cells of RHS and LHS matrices I wrote the linear equation set
$$
\begin{bmatrix}
1.25 & 1.5 & 1.5 & 1 \\
-1.05 & -1 & -0.7 & 0 \\
-1.05 & -0.7 & -1 & 0 \\
0.49 & 0 & 0 & -1 \\
\end{bmatrix}
\begin{bmatrix}
a \\
b \\
c \\
d
\end{bmatrix} =
\begin{bmatrix}
-1 \\
-0.5 \\
-0.5 \\
-0.25
\end{bmatrix}
$$
I solved this set of equations using two different software tool to find exact same $a$, $b$, $c$ and $d$ values:
$$X = \begin{bmatrix} a & b \\ c & d \end{bmatrix} = 
\begin{bmatrix} 18.8802 & -11.3672 \\ -11.3672 & 9.5013 \end{bmatrix}$$
Now remember the equation above:
$$
\begin{bmatrix} a & b \\ c & d \end{bmatrix}
= \begin{bmatrix} 1.5 & 1 \\ -0.7 & 0 \end{bmatrix}
\begin{bmatrix} a & b \\ c & d \end{bmatrix}
\begin{bmatrix} 1.5 & -0.7 \\ 1 & 0 \end{bmatrix}
+ \begin{bmatrix} 1 & 0.5 \\ 0.5 & 0.25 \end{bmatrix}
$$
When I put the $a$, $b$, $c$ and $d$ values in the RHS, I don't find the $X$ matrix on the LHS.
$$
\begin{bmatrix} 1.5 & 1 \\ -0.7 & 0 \end{bmatrix}
\begin{bmatrix} 18.8802 & -11.3672 \\ -11.3672 & 9.5013 \end{bmatrix}
\begin{bmatrix} 1.5 & -0.7 \\ 1 & 0 \end{bmatrix}
+ \begin{bmatrix} 1 & 0.5 \\ 0.5 & 0.25 \end{bmatrix}
= \begin{bmatrix} 163.4421 & -332.7002 \\ -332.7002 & 857.9770 \end{bmatrix}
$$
What am I doing wrong in my calculations?

REPLY [2 votes]: Given $\mathrm A, \mathrm B \in \mathbb R^{n \times n}$, we have a linear matrix equation in $\mathrm X \in \mathbb R^{n \times n}$
$$\rm X = A X A^{\top} + B$$
Vectorizing both sides, we obtain a system of $n^2$ linear equations in $n^2$ unknowns
$$\left( \mathrm I_{n^2} - \left( \mathrm A \otimes \mathrm A \right) \right) \mbox{vec} (\mathrm X) = \mbox{vec} (\mathrm B)$$

Example
Let
$$\mathrm A := \begin{bmatrix} 1.5 & 1 \\ -0.7 & 0\end{bmatrix} \qquad \qquad \qquad \mathrm B := \begin{bmatrix} 1 & 0.5 \\ 0.5 & 0.25\end{bmatrix}$$
Using NumPy, we solve the linear system and then un-vectorize the solution:
>>> import numpy as np
>>> A = np.array([[ 1.5, 1.0],
                  [-0.7, 0.0]])
>>> B = np.array([[ 1.0, 0.5],
                  [ 0.5, 0.25]])
>>> I4 = np.identity(4)
>>> solution = np.linalg.solve(I4 - np.kron(A,A), np.reshape(B, 4))
>>> X = np.reshape(solution, (2,2))
>>> X
array([[ 18.88020833, -11.3671875 ],
       [-11.3671875 ,   9.50130208]])