# How do you find all solutions to the matrix equation $XAX=A^T$?

I was recently asked to solve a problem in a programming interview involving word squares, and on further reflection I realized it could be recast as a linear algebra question. Since my solution has a worst-case time complexity of $O(n!)$ if $n$ is the width of $A$, out of personal curiosity I'm trying to find a better solution, as measured by worst-case time complexity.

Given an $n$ by $n$ square matrix $A$ over the reals with $n\geq 2$, is there an $n$ by $n$ permutation matrix $X$ satisfying $$XA=(XA)^T?$$

Since $(XA)^T=A^TX^T$ and $X$ is a permutation matrix, we have $X^T=X^{-1}$, hence this is equivalent to asking if there are any solutions to $$XAX=A^T$$ which are permutation matrices. Naively, I was hoping there would be a unique solution, but numeric techniques strongly indicate a wide variety of solutions, even when a satisfactory permutation exists.

Unfortunately, it doesn't seem that solutions form a linear subspace. For example, if $X$ is a solution, then $(aX)A(aX)=a^2A^T$ for any real $a$, so most multiples of $X$ are not solutions for most matrices $A$. Similar problems exist for sums of solutions. With this in mind, I'm not quite sure which method one might use to enumerate solutions. If they don't form a linear subspace, we can't just enumerate a basis.

Any partial solutions, theorems, pointers in the right direction, or related problems would be helpful. I'm not familiar with what seems to be a nonlinear matrix equation (if that's the right terminology).

Update This seems to be a special case of something called the algebraic Riccati equation, as indicated in another question. The techniques I have found so far emphasize one of two approaches:

• Algorithms for finding any solution from a given starting guess.
• Algorithms for finding the unique stabilizing solution, should one exist.

Given the control theoretic background for those approaches, they typically assume that $A^T$ is symmetric. In my case, if $A^T$ were symmetric I could simply take $X$ to be the identity and be done, and that assumption oversimplifies the problem. Additionally, those solutions do not seem to have a clean way to enumerate all solutions, inhibiting one from checking if any happen to be permutation matrices. Does anyone have more tips from a Riccati equation standpoint?

I would like to add that strictly speaking one can answer the question in finite time by simply enumerating all permutation matrices of the appropriate size and checking if they satisfy $XA=(XA)^T$. For some reason I'm having trouble describing, I don't think this captures the spirit of the question.

Update @GCab points out that $A$ would need a very peculiar structure for $X$ to be a permutation matrix. This is indeed the case, and $A$ would need to be symmetric on some row or column permutation. This does yield some insights. If $X$ is any permutation matrix, then $$(XAX)_{ij}=A_{ji}=(A^T)_{ij}$$ for any $i$, $j$ so that $X_{ij}=1$, and this does not hold when $X_{ij}=0$. In other words, the elements of $A$ come in pairs except for possibly the main diagonal of $XA$. Using this, one can rule out many possible permutations, since any element appearing only once in $A^T$ must give a location of a $1$ in $X$.

There are many other such combinatorial tricks that can greatly improve the average runtime. One can similarly note that for a permutation $X$ to exist as a solution, there must exist a bijection $f$ between the rows and columns of $A$ so that if $r$ is a row of $A$ then the count of each element of $r$ is the same as its count in $f(r)$. Such a heuristic has the potential to easily classify a choice of $A$ as not having a satisfactory solution, but it could still take an enormous amount of time to confirm that a given $A$ does have a satisfactory $X$.

• Just a thought, but $(AX)^2=A A^T$. If you can compute the square roots of $A A^T$, you can reduce the equation to $AX=\sqrt{A A^T}$. One way to get a square root of $A A^T$ is to put it in Jordan Normal form as $(M D^{1/2} M^{-1})^2=M D M^{-1}$. – abnry Jun 6 '18 at 15:18
• @abnry I like this. It yields all possible solutions (at most $2^n$ for non-degenerate cases) in a clean and understandable way and also admits a sub-factorial worst-case time complexity. If you want to write this up as an answer, I'll accept it. – Hans Musgrave Jun 6 '18 at 17:04

If your equation is solvable, then necessarily there is a solution to $(AX)^2=A A^T$. (Note that there is only a direct equivalence if $A$ is invertible.)

Thus, if we can compute all matrices $B_i$ such that $B_i^2 = AA^T$, then we should look for solutions to $AX = B_i$. If $A A^T = N D N^{-1}$ is a Jordan normal decomposition, then $B_i = N D_i^{1/2} N^{-1}$ is a square root where $D_i^{1/2}$ is a square root of $D$. In the case of $D$ diagonal, there are $2^n$ possible square roots. I am unsure of the theory behind finding square roots of matrices. This may be all the square roots but I don't know.

However, in our case we can use the fact that $A A^T$ is positive semi-definite, which simplifies a lot. In this case, $A A^T = N D N^{-1}$ where $D$ is diagonal. It is known in this case there is a unique positive semi-definite square root. I don't know if all square roots are generated from this one.

If the restriction on $X$ is that it is a permutation matrix, then it should be easy to determine if $AX=B_i$ has a solution. Once we find a solution, we must check that it satisfies our original equation $XAX=A^T$ if $A$ is not invertible.

If $\bf X$ is a Permutation Matrix, then its action on a matrix $\bf A$ is to permute (exchange) the rows of it if multiplied on the left ($\bf X \bf A$), or its columns when multiplied on the right ($\bf A \bf X$).

So you cannot obtain of exchanging the rows with the columns (${\bf A}^T$), unless the matrix has a peculiar structure.

Let's start with $n=2$. Apart from the identity, the other PM is the Exchange Matrix $${\bf J} = \left( {\matrix{ 0 & 1 \cr 1 & 0 \cr } } \right)$$

And \eqalign{ & {\bf J}\,{\bf A} = \left( {\matrix{ 0 & 1 \cr 1 & 0 \cr } } \right)\left( {\matrix{ {a_{\,1,\,1} } & {a_{\,1,\,2} } \cr {a_{\,2,\,1} } & {a_{\,2,\,2} } \cr } } \right) = \left( {\matrix{ {a_{\,2,\,1} } & {a_{\,2,\,2} } \cr {a_{\,1,\,1} } & {a_{\,1,\,2} } \cr } } \right) \cr & {\bf J}\,{\bf A}\,{\bf J}\, = \left( {\matrix{ {a_{\,2,\,1} } & {a_{\,2,\,2} } \cr {a_{\,1,\,1} } & {a_{\,1,\,2} } \cr } } \right)\left( {\matrix{ 0 & 1 \cr 1 & 0 \cr } } \right) = \left( {\matrix{ {a_{\,2,\,2} } & {a_{\,2,\,1} } \cr {a_{\,1,\,2} } & {a_{\,1,\,1} } \cr } } \right)\quad \leftrightarrow \quad {\bf A}^{\,T} = \left( {\matrix{ {a_{\,1,\,1} } & {a_{\,2,\,1} } \cr {a_{\,1,\,2} } & {a_{\,2,\,2} } \cr } } \right) \cr}

which means that $\bf A$ shall have a constant diagonal.

Every permutation can be expressed by the product of Transpositions and in particular by the product of Adjacent Transpositions.
An Adjacent Transposition matrix consists of an identity matrix in which a $2 \times 2$ diagonal block is replaced by $\bf J$.

Then I suppose you can continue from here.

• It's the "continuing from there" part I'm struggling with. The matrix $A$ would have to have a very peculiar structure -- it would need to be symmetric under some row permutation (or equivalently some column permutation). I'm not finding any way of describing that structure more compactly than simply checking all such permutations though. – Hans Musgrave Jun 6 '18 at 14:19

Let $$A\in M_n(\mathbb{R})$$. Does there exist a permutation matrix $$X$$ s.t. $$XA$$ is symmetric ?

$$XA$$ results in a permutation of the rows of $$A$$; then necessarily, $$A=[a_{i,j}]$$ has at most $$n(n+1)/2$$ distinct entries $$a_{i,j}$$. Consequently, if $$A$$ is randomly chosen, then there are no solutions. More precisely

There is a permutation solution iff $$A=PS$$ where $$P$$ is a permutation and $$S$$ is symmetric.

Method 1. (The worst one). We consider the algebraic system

$$(*)$$ $$XA$$ is symmetric, $$X^TX=I_n$$. In general, this system has $$2^n$$ solutions. It remains to see if, amongst the solutions, there are permutations; unfortunately, it's difficult to find all the solutions (even approximations) and it's practically impossible when $$n\geq 10$$.

Method 2. We can try successively all the permutations. Each test is very quick; thus it's feasible when (for example) $$n=10$$ and perhaps until $$n=20$$ ($$20!\approx 2.10^{18}$$). But beyond, that is impossible;

Method 3. There is an easy case. We assume that $$A$$ has exactly $$n$$ entries that appear exactly once.

Of course, these entries are the elements of the diagonal of $$S$$ (if there are solutions). On the row of index $$i$$, there must exist exactly one such entry: $$a_{i,k_i}$$. Moreover, the sequence $$k_1,\cdots,k_n$$ must be a permutation $$\sigma$$ of $$1,\cdots,n$$. Finally, if there is a solution, then it is unique and is equal to $$X=U^T$$, where $$U$$ is the permutation matrix associated to $$\sigma$$.

Of course, this method has a very small complexity; it only needs comparisons and exchanges.