# Solve matrix equation A*X=X*B using LeastSquares

I want to solve matrix equation A*X=X*B using LeastSquares method, but it is suited for equation like A*X=B.

All matrices are 3x3

A = {{a11, a12, a13}, {a21, a22, a23}, {a31, a32, a33}}

B = {{b11, b12, b13}, {b21, b22, b23}, {b31, b32, b33}}

X = {{x11, x12, x13}, {x21, x22, x23}, {x31, x32, x33}}

a33=b33=x33=1, a31=0,a32=0.


How can I do this?

Update: I figure out that it's special type of equation

I'm trying to solve a special case of Sylvester equation in my case it looks like $$A*X=X*B$$ so it can be write in form $$A*X+X*(-B)=C$$ where C consist of all 0 items.

I tried to solve it in Mathematica with LyapunovSolve but it gives me a all-zero trivial solution.

So I want to check if a non-trivial solution exists. It seems I find out how to test matrices A,B for non-trivial solution (but I'm not sure) if resultant equals to 0, then such solution exist.$$\mathrm{resultant}(\mathrm{det}(A-λE),\mathrm{det}(B-λE),λ)=0$$ I tried real matrices for example A = {{0 -1 300}, {1 0 0}, {0 0 1}}, B = {{-0.4009 -1.0787 446.1463}, {1.6180 0.8875 -159.2272}, { 0.0003 0.0029 1}} but there is maybe a problem because B matrix was defined experimentally and it has some small errors, with these matrices I have resultant -6.79 , but due to erorrs I don't know may be it close enough to 0?

My question is: Can you write condition of existence of non-trivial solution of Sylvester equation not in abstract form but in particular formula, preferably in Mathematica.

Also I don't understand why Mathematica gives me only trivial solution, when I try to solve this equation with parameters. For example I tried

am = {{a11, a12, a13}, {a21, a22, a23}, {a31, a32, a33}}

bm = {{b11, b12, b13}, {b21, b22, b23}, {b31, b32, b33}}

cm = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

LyapunovSolve[am,bm,cm]


which gives me all 0. Also I tried to multiply matrices and get 9 equations and solve them with function Solve which also give me all 0.

• You haven't said anything about the dimensions or special features of your $A$, $B$, and $X$ (assuming they're matrices). What is your actual problem? – J. M.'s discontentment Aug 6 '12 at 7:27
• @j-m A,B,X are matrices 3x3 and in matrix A last row is 0 0 1, and in matrices B,X last elements =1. A is rotation matrix, B,X is perspective transform matrices. here mathematica.stackexchange.com/questions/9106/… how I try to solve this problem in other way. – mrgloom Aug 6 '12 at 7:35
• I don't have time to write something up, but you might want to look into Procrustes transformations. – J. M.'s discontentment Aug 6 '12 at 7:41
• @j-m I don't understand how it can help. – mrgloom Aug 6 '12 at 8:02
• Also I find out that equations like AX=XB can be solved like this reference.wolfram.com/mathematica/ref/LyapunovSolve.html en.wikipedia.org/wiki/Sylvester_equation but it gives me all =0 for my matrices. – mrgloom Aug 6 '12 at 8:30

This is to get you started towards your strategy (using LeastSquares methods). One can put your equation A*X-X*B=C in the form D*Xp=Cp, where Xp, Cp are vectors containing the entries of X, C respectively.

a = {{a11, a12, a13}, {a21, a22, a23}, {a31, a32, a33}};
b = {{b11, b12, b13}, {b21, b22, b23}, {b31, b32, b33}};
x = {{x11, x12, x13}, {x21, x22, x23}, {x31, x32, x33}};

eqs = Flatten[Dot[a, x] - Dot[x, b], 1];

xp = Flatten[x]

(* {x11, x12, x13, x21, x22, x23, x31, x32, x33} *)

d = Outer[Coefficient[#1, #2] &, eqs, xp]

(* {{a11 - b11, -b21, -b31, a12, 0, 0, a13, 0, 0},
{-b12, a11 - b22, -b32, 0, a12, 0, 0, a13, 0},
{-b13, -b23, a11 - b33, 0, 0, a12, 0, 0, a13},
{a21, 0, 0, a22 - b11, -b21, -b31, a23, 0, 0},
{0, a21, 0, -b12, a22 - b22, -b32, 0, a23, 0},
{0, 0,  a21, -b13, -b23, a22 - b33, 0, 0, a23},
{a31, 0, 0, a32, 0, 0, a33 - b11, -b21, -b31},
{0, a31, 0, 0, a32, 0, -b12, a33 - b22, -b32},
{0, 0, a31, 0, 0, a32, -b13, -b23, a33 - b33}} *)


Example (from help on LyapunovSolve) :

a = {{-1, 1, 0}, {-1, -1, 1}, {1, 1, 0}} ;
b = {{-2, 0, 0},{0, -1, 0},{0, 0, -3}} ;
cMat = -{{Subscript[c, 1], 0, 0}, {0, Subscript[c, 2], 0},
{0, 0, Subscript[c, 3]}};

eqs = Flatten[Dot[a, x] + Dot[x, b], 1];
xp = Flatten[x];
cp = Flatten[cMat];
d = Outer[Coefficient[#1, #2] &, eqs, xp];

Dot[Inverse[d], cp]

(* { (5 Subscript[c, 1])/16, Subscript[c, 2]/2, Subscript[c, 3]/46,
-(Subscript[c, 1]/16), Subscript[c, 2], (2 Subscript[c, 3])/23,
Subscript[c, 1]/8, (3 Subscript[c, 2])/2, (17 Subscript[c, 3])/46} *)

LyapunovSolve[a, b, cMat]

(* {{(5 Subscript[c, 1])/16, Subscript[c, 2]/2, Subscript[c, 3]/46},
{-(Subscript[c, 1]/16), Subscript[c, 2], (2 Subscript[c, 3])/23},
{Subscript[c, 1]/8, (3 Subscript[c, 2])/2, (17 Subscript[c, 3])/46}} *)

• "One can put your equation AX+XB=C in the form D*X=C" the problem is that can't be done (or not so simple). – mrgloom Aug 6 '12 at 11:43
• @mrgloom Could you explain why the above is incorrect ? – b.gates.you.know.what Aug 6 '12 at 11:48
• maybe I don't understand how Outer works, but what we have in the output? 9 posible equations D*X=C? – mrgloom Aug 6 '12 at 12:11
• @mrgloom Please see edit. – b.gates.you.know.what Aug 6 '12 at 12:26
• I don't understand what you are trying to do. From the wikipedia article you cited above: "the Sylvester equation has a unique solution if and only if A and -B have no common eigenvalues". Therefore, for C=0 and if this condition is true, you will always get X=0. Is the condition fulfiled for your matrices A,B? – sebhofer Aug 7 '12 at 11:33