TITLE: Is it true that any matrix in $M_2(\mathbb R)$ is the sum of two squares?
QUESTION [13 upvotes]: I recently show that every polynomial with real coefficient and $P$ is always is a sum of two squares of polynomials, therefore positive. These questions also appear often in arithmetic.

What if we change polynomials by matrices?

I ask my professor and unfortunately he had no idea if this result is already shown or not.
I tried, unsuccessfully, several tracks :
First denote that $$\forall M\in M_n(\mathbb R) , M=1/2[(A+A{}^t)+(A-A{}^t)].$$
and perhaps used the following result :

Any symmetric positive matrix admits a square root

Proof.
By spectral theorem, there exist $\Omega \in \mathbb O_n(\mathbb R)$ and $(\lambda_1,...\lambda_n)\in (\mathbb R^{+})^n$ such that $S=\Omega D \Omega^{-1}$ with $D$ the diagonal matrix formed by the positive eigenvalues​​.
Let $D'$ the diagonal matrix formed of the roots of the eigenvalues​​.
Then , $D'²=S$ and  $D'{}^t=D'$ thus she is a symmetric positive matrix.

Question : Is it possible to believe that all matrices (say in $M_2(\mathbb R)$) are the sum of two squares?

REPLY [6 votes]: Let us answer the more general question first:

Is every real square matrix the sum of squares of two real matrices? That is, given $M\in M_n(\mathbb R)$, is it always possible to write $M=A^2+B^2$ for some $A,B\in M_n(\mathbb R)$?

The answer is no.

Lemma 1 (cf. Horn and Johnson, Topics in Matrix Analysis, theorem 6.4.14, p.473). If a real square matrix $X$ has a real matrix square root, each Jordan block of $X$ corresponding to a negative eigenvalue must occur an even number of times.

Now, for a counterexample, consider $n=3$ and $M=-I$. If $A$ is a real square matrix, it is not hard to see that in the (complex) Jordan decomposition of $-I-A^2$, there must be some Jordan block of size 1 that corresponds to a negative eigenvalue and occurs only once in the Jordan form. Hence, by lemma 1, $-I-A^2$ has not any real square root, i.e. it is not possible to write $-I=A^2+B^2$ for some real $A,B$.
However, when $n=2$, the answer is yes. I believe that Jack has answered your question nicely. If you don't like the explicit formula that Jack has used, here is an alternative perspective. First, we have the following lemma, which is closely related to lemma 1:

Lemma 2 (cf. Walter J. Culver, On the existence and uniqueness of the real logarithm of a matrix, Proceedings of the American Mathematical Society, 17(5): 1146-1151, 1966). A real matrix has a real logarithm if and only if it is nonsingular and in its (complex) Jordan normal form, every Jordan block corresponding to a negative eigenvalue occurs an even number of times.

Now, given any $M\in M_2(\mathbb R)$ and sufficiently large $d>0$, the matrix
$$
M-A^2 := M-{\underbrace{\pmatrix{0&-\sqrt{d}\\ \sqrt{d}&0}}_A}^2 = M+dI
$$
has not any zero or negative eigenvalue. Therefore, by lemma 2, $M-A^2=e^Y$ for some real matrix $Y$. Put $B=e^{Y/2}$, we have $M=A^2+B^2$.