quadratic matrix equation Find all symmetric matrices $X=X^{T}$ such that 
\begin{align}
XDX^{T}=-D  \quad  (1)
\end{align}
where $D\ne 0$ is a real diagonal matrix. 
For example, $X=iI$ satisfies $(1)$. Can you get a necessary and sufficient characterization for such matrices $X$? Thanks a lot. 
 A: Here is a general procedure, which yields results for generic matrices $D$. First, replacing $X$ by $iX$ changes the problem to $XDX = D$, which is the one we will attack. Pre-multiply by $D$, set $Z = DX$; a necessary condition on $X$ is thus  $Z^2 = D^2$. Hence $Z:= N$ is a  square root of $D^2$, so if $D$ is invertible, then $X = D^{-1}N$. When $D$ is invertible, this is a solution to $XDX = D$, as is easily checked. In general, square roots need not commute with each other (both $N$ and $D$ are square roots of $D^2$); but if $DN = ND$, then $X^2 = I$. In that case, the involution $X$ is special.
If, as user35593 says, $D^2$ has distinct eigenvalues (equivalently, $D$ has distinct eigenvalues and not both $\pm z$ can be eigenvalues of $D$ for any complex $z$), then there are only $2^n$ square roots of $D^2$, and these all commute with $D$. In that case, the solution satisfies $X^2 = I$ (the identity), so we can write $X = 2E - I$ for some idempotent $E$. But the requirement that $X$ be symmetric, that is, $X = X^T$ (without the complex conjugation)—which was obviously unchanged on replacing $X$ by $iX$ in the original), forces $E = E^T$. 
So if $E$ happens to be real, we obtain that there are two subspaces each spanned by eigenvectors of $D$ that are orthogonal, and together span the whole space. Of course, this happens when $D$ is diagonal, so we obtain $2^n$ solutions in this case, the diagonal entries are precisely $\pm 1$. 
If $D$ is invertible, but $D^2$ has multiple eigenvalues, there are now infinitely many $N$ that do not commute with $D$, so we obtain infinitely many solutions (before checking whether they are symmetric), in addition to the standard ones.
If $D$ is not invertible, then we have to assume something like its range is orthogonal to its kernel (obvious for diagonal matrices), and then follow the same procedure on the range—if not both $\pm z$ are in the nonzero spectrum of $D$ and  $D$ has distinct nonzero eigenvalues. So some care is necessary if $D$ is not invertible, checking that whatever comes out will be a solution to $XDX = D$, not merely $(DX)^2 = D^2$. 
A: Since $XD^2=XD(-XDX)=-XDXDX=(-XDX)DX=D^2X$, the matrices $X$ and $D^2$ commute. W.L.O.G we can assume that entries of $D$ which have the same absolute value are consecutive (multiply with a permutation matrix). It follows that $X$ has block diagonal form where each absolute value corresponds to a block. The block corresponding to the value $0$ (if it exists) is an arbitrary symmetric matrix. Let $Y$ be the block of $X$ with absolute value $a\neq 0$ and let $p$ be the number of diagonal entries which are equal to $a$ and $q$ the number of diagonal entries of $D$ which are equal to $-a$. Then $YI_{p,q}Y=-I_{p,q}$ where $I_{p,q}=diag(\underbrace{1,\dots,1}_{p},\underbrace{-1,\dots,-1}_{q})$. Since $Y$ is symmetric there exist a Autonne–Takagi factorization $Y=V^TMV$ with unitary $V$ and diagonal $M=diag(m_1,\dots m_{p+q})$ with $m_i\geq 0$. Therefore $V^TMVI_{p,q}V^TMV=-I_{p,q}$ and hence $M(VI_{p,q}V^T)M=-conj(VI_{p,q}V^T)$. Hence it follows that $VI_{p,q}V^T=iZ$ where $Z$ is a real symmetric matrix. Furthermore if $Z_{j,k}\neq 0$ we have $m_jm_k=1$. On the other hand every matrix consisting of blocks $Y=V^TMV$ satisfying the conditions above will by construction be a solution.
