Verifying the solution to a vectorized matrix/vector equation After struggling with a problem for some time I arrived at what I thought to be a solution. However, I could not verify the derived expression as a solution to my equation in numerical experiments. So my question is if it is the math or the code that is wrong, and to answer this I'm asking for your help to check my math.
For $\mathbf{Y}_n \in \mathbb{R}^{1 \times r}$, $\mathbf{U}_n \in \mathbb{R}^{1 \times r}$, $\mathbf{V} \in \mathbb{R}^{T \times r}$ and $\mathbf{S}_n \in \left \{ 0, 1 \right \}^{T \times T}$, my goal is to solve for $\mathbf{V}$ in
$$
\sum_n \left \{ - \mathbf{S}_n^\top \left ( \mathbf{Y}_n^\top - \mathbf{S}_n^\top\mathbf{V}\mathbf{U}_n^\top \right )\mathbf{U}_n + \mathbf{V} + \mathbf{D}\mathbf{V}  \right \} = 0.
$$
Note that $\mathbf{S}_n$ is really a shift matrix with ones on the superdiagonal/subdiagonal. The superdiagonal/subdiagonal may vary with each $n$ so usually $\mathbf{S}_n \neq \mathbf{S}_{n + 1}$. Expanding the LHS and rearranging I get
$$
\sum_n \left \{  \mathbf{S}_n^\top\mathbf{S}_n^\top\mathbf{V}\mathbf{U}_n^\top \mathbf{U}_n + \mathbf{V} + \mathbf{D}\mathbf{V} \right \} = \sum_n \left \{\mathbf{S}_n^\top \mathbf{Y}_n^\top \mathbf{U}_n  \right \}
$$
For future reference, let $\mathbf{\widetilde{S}}_n = \mathbf{S}_n^\top\mathbf{S}_n^\top$ and $\mathbf{\widetilde{U}}_n = \mathbf{U}_n^\top \mathbf{U}_n$. To solve for $\mathbf{V}$ I apply vectorization to each term in the summation using the following identities:
$$
vec(\mathbf{A}\mathbf{B}) = (\mathbf{I} \otimes \mathbf{A})vec(\mathbf{B}); \quad vec(\mathbf{A}\mathbf{B}\mathbf{C}) = (\mathbf{C}^\top \otimes \mathbf{A})vec(\mathbf{B})
$$
Here, $\mathbf{I}$ is the identity matrix and $\otimes$ represents the Kronecker product. The vectorization brings the equation I try to solve into the form
$$
\sum_n \left \{ \left [ (\widetilde{\mathbf{U}}_n ^\top \otimes \widetilde{\mathbf{S}}_n) + (\mathbf{I} \otimes \mathbf{I}) + (\mathbf{I} \otimes \mathbf{D}) \right ]vec(\mathbf{V}) \right \} = \sum_n \left \{ (\mathbf{U}_n^\top \otimes \mathbf{S}_n^\top)vec(\mathbf{Y}_n^\top) \right \}
$$
Now, I solve for $vec(\mathbf{V})$ and obtain
$$
vec(\mathbf{V}) = \sum_n \left \{ (\mathbf{U}_n^\top \otimes \mathbf{S}_n^\top)vec(\mathbf{Y}_n^\top) \left [ (\widetilde{\mathbf{U}}_n ^\top \otimes \widetilde{\mathbf{S}}_n) + (\mathbf{I} \otimes \mathbf{I}) + (\mathbf{I} \otimes \mathbf{D}) \right ]  ^{-1} \right \}
$$
However, reshaping this expression for $vec(\mathbf{V})$ it into a $T \times r$ matrix and plugging it into the first equation of this post, the output of my simulation is different from zero.
 A: You vectorized the equation correctly, but everything you did after that was incorrect. We have
$$
\sum_n \left \{ \left [ (\widetilde{\mathbf{U}}_n ^\top \otimes \widetilde{\mathbf{S}}_n) + (\mathbf{I} \otimes \mathbf{I}) + (\mathbf{I} \otimes \mathbf{D}) \right ]\operatorname{vec}(\mathbf{V}) \right \} = 
\operatorname{vec}\left[ \sum_n \mathbf S_n^\top \mathbf  Y_n^\top \mathbf  U_n\right]
\\
\left[\sum_n \left [ (\widetilde{\mathbf{U}}_n ^\top \otimes \widetilde{\mathbf{S}}_n) + (\mathbf{I} \otimes \mathbf{I}) + (\mathbf{I} \otimes \mathbf{D}) \right ] \right] 
\operatorname{vec}(\mathbf{V})
= 
\operatorname{vec}\left[ \sum_n \mathbf S_n^\top \mathbf  Y_n^\top \mathbf  U_n\right].
$$
This is an equation of the form $\mathbf A \mathbf x = \mathbf b$, which has the solution $\mathbf x = \mathbf A^{-1}\mathbf b$. So, we have
$$
\operatorname{vec}(\mathbf{V})
= 
\left[\sum_n \left [ (\widetilde{\mathbf{U}}_n ^\top \otimes \widetilde{\mathbf{S}}_n) + (\mathbf{I} \otimes \mathbf{I}) + (\mathbf{I} \otimes \mathbf{D}) \right ] \right]^{-1} \operatorname{vec}\left[ \sum_n \mathbf S_n^\top \mathbf  Y_n^\top \mathbf  U_n\right].
$$
This is not equivalent to the expression that you have presented.
You never state how many terms there are in the sum $\sum_n$. However, with this additional information, the sum that defines the matrix $\mathbf A$ can be simplified to
$$
\sum_{n=1}^N \left [ (\widetilde{\mathbf{U}}_n ^\top \otimes \widetilde{\mathbf{S}}_n) + (\mathbf{I} \otimes \mathbf{I}) + (\mathbf{I} \otimes \mathbf{D}) \right ] = 
N \cdot \mathbf I \otimes (\mathbf {I + D}) + \sum_{n=1}^N (\widetilde{\mathbf{U}}_n ^\top \otimes \widetilde{\mathbf{S}}_n).
$$
