Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Solving AM = MB
Date: Wed, 15 Oct 2008 01:48:02 +0000 (UTC)
Organization: SARA Inc
Lines: 51
Message-ID: <gd3i4i$13v$1@fred.mathworks.com>
References: <gd34pk$csc$1@fred.mathworks.com> <gd36qn$sl9$1@fred.mathworks.com> <gd38sd$eb9$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1224035282 1151 172.30.248.37 (15 Oct 2008 01:48:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 15 Oct 2008 01:48:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 966359
Xref: news.mathworks.com comp.soft-sys.matlab:495210


"David Doria" <daviddoria@gmail.com> wrote in message <gd38sd$eb9$1@fred.mathworks.com>...
> So I was looking to solve
> AM = MB 
> 
> And abba.m solves
> AB = BA
> 
> which is slightly different, because there are only 2 matrices A and B, instead of A,B, and M.
> 
> I was a victim of your comment "% have fun with this next line...". I read the help for kron() but I have no background with tensors.  Could you give a couple line explanation of what that is doing? I can't modify your code to fit this problem because I don't know what that line does, and it seems to be about the whole thing!!
> 
> Thanks,
> 
> Dave


John's method is difficult to generalize for all cases.  I have tried to generalize his technique for my own uses.  

Assuming you have a matrix equation of the form:

  A1*X + X*A2 + A3*X*A4 = 0

where A1, A2, A3, A4, and X are all n-by-n matrices, you can solve this equation using the following commands:

A1X = kron(eye(n),A1);
XA2 = kron(A2.',eye(n));
A3XA4 = kron(eye(n),A3)*kron(A4.',eye(n));
M = (A1X + XA2 + A3XA4);
Mnull = null(M);
if ~isempty(Mnull)
  X = reshape(sum(Mnull,2),n,n);
else
  X = zeros(n,n);
end

However, if you have a matrix equation of the form:

A1*X + X*A2 + A3*X*A4 = A5

I use the variation:

% Same here
A1X = kron(eye(n),A1);
XA2 = kron(A2.',eye(n));
A3XA4 = kron(eye(n),A3)*kron(A4.',eye(n));
M = (A1X + XA2 + A3XA4);
% Here's where it differs
x = M\A5(:);
X = reshape(x,n,n);

There are improvements which can be made here, but I hope this gives you the gist of it.