What happens when matrix is moved to the other side of the equation? I have been doing several questions related to Matrix. 
I have a question ask me to simplify the expression (knowing that A,X,B are squared matrix with the same order):
$AXB=(AB)^2$
$AXB=A^2B^2$
Then I am thinking where should I put A and B when I move them to the other side (As matrix operation is not commutative):
Should it be: $X=A^2A^{-1}B^2B^{-1}$ which equals to $X=AB$ ?
However, the right answer is $X=BA$. Since they are the same order, does $AB=BA$? 
I have always been confused by this kind of question. I have no idea how to properly move the matrix to the other side of the equation
 A: There are a couple errors here. First note that $(AB)^2 = (AB)\cdot (AB) = ABAB$, not $A^2B^2$.
The bigger problem is with how you think about what you're doing. There is no mathematical notion of "moving one thing from one side of the equation to the other." There are only things which happen to look like they do this.
The fundamental rule here is that you must do exactly the same thing to both sides.
In this example, we have 
$$AXB=(AB)^2=ABAB.$$
We can now multiply by $A^{-1}$ on the left on both sides, this gives
$$A^{-1}AXB = A^{-1}ABAB,$$
or $$XB = BAB,$$ since $A^{-1}A=I$.
Now we can multiply by $B^{-1}$ on the right on both sides, to get
$$XBB^{-1} = BABB^{-1},$$
and simplifying, this gives
$$X=BA,$$
as desired.
Further comments:
Matrix Multiplication:
Matrix multiplication is not commutative. I.e., it is not true in general that $AB=BA$ for matrices $A$ and $B$. Indeed, $AB$ and $BA$ need not even both be the same size or both make sense. Consider the following examples:


*

*$$A=\newcommand\bmat{\begin{pmatrix}}\newcommand\emat{\end{pmatrix}}\bmat 0 & 1 \\ 0 & 0 \emat, \quad B = \bmat 0 & 0 \\ 1 & 0 \emat, $$ then
$$ AB = \bmat 1 & 0 \\ 0 & 0 \emat,\quad BA = \bmat 0 & 0 \\ 0 & 1 \emat. $$

*If $A$ is $2\times 3$ and $B$ is $3\times 2$, then $AB$ is $2\times 2$ and $BA$ is $3\times 3$.

*If $A$ is $2\times 3$ and $B$ is $3\times 4$, then $AB$ is $2\times 4$, but $BA$ doesn't even make sense, and can't be multiplied because the sizes of the matrices don't line up.


Invertibility of matrices:
Not every matrix has an inverse, i.e., given a $n\times n$ matrix $A$, there isn't always a matrix $A^{-1}$ such that $AA^{-1}=A^{-1}A=I$. I've assumed that $A$ and $B$ are invertible in your question although it wasn't stated, because otherwise $BA$ is not the unique possibility for $X$, which is the given answer. This is important though, otherwise I couldn't have multiplied by $A^{-1}$ and $B^{-1}$ in the solution.
Also even though nonsquare matrices cannot be invertible in the sense given above, they can have one sided inverses. I.e., if $A$ is $n\times m$, with $n\ge m$, there may be a matrix $B$ with $BA = I_m$, and if $m\ge n$, there may be a matrix $C$ with $AC = I_n$. These can also be very useful at times.
