I need to solve an underdetermined Lyapunov equation for unknown $n\times n$ matrix $X$.
$$AX + XA = B$$
The naive method is to vectorize $x=\operatorname{vec}(X)$ and use a least squares solver on the following equation to find the least-squares solution.
$$(I\otimes A + A\otimes I)x = \operatorname{vec}B$$
Experimentally I found that when $A$ and $B$ are positive semidefinite, I get the same solution by expressing $X$ in terms of $U,s$, the eigenvectors and eigenvalues of $A$ as below (proof) and modifying pointwise (Hadamard) division to skip division by zero, like how pseudo-inverse implementations do it.
$$X=U \left( \frac{U' BU}{s + s'} \right) U'$$
Does this appear in the literature, or does someone see a way to prove that this recovers least-squares solution?
Example
$$\text{A=}\left( \begin{array}{ccc} 8 & -8 & -8 \\ -8 & 9 & 8 \\ -8 & 8 & 8 \\ \end{array} \right)$$
$$\text{B=}\left( \begin{array}{ccc} 5 & 5 & -5 \\ 5 & 9 & -3 \\ -5 & -3 & 6 \\ \end{array} \right)$$
This equation is underdetermined because $A$ and $B$ are singular, hence standard Lyapunov solver fails. However, both least squares and truncated spectral decomposition methods succeed with the same answer:
$$X=\frac{1}{640}\left( \begin{array}{ccc} 1789 & 2928 & -1329 \\ 2928 & 4672 & -1968 \\ -1329 & -1968 & 869 \\ \end{array} \right)$$