LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
SimilarityMatrix.m
1% B = SimilarityMatrix(A1, A2)
2%
3% Returns the matrix that transforms A1 to A2.
4%
5% Parameters
6% ----------
7% A1 : matrix, shape (N,N)
8% The smaller matrix
9% A2 : matrix, shape (M,M)
10% The larger matrix (M>=N)
11%
12% Returns
13% -------
14% B : matrix, shape (N,M)
15% The matrix satisfying `A_1\,B = B\,A_2`
16%
17% Notes
18% -----
19% For the existence of a (unique) solution the larger
20% matrix has to inherit the eigenvalues of the smaller one.
21
22function B = SimilarityMatrix (A1, A2)
23
24 if size(A1,1)~=size(A1,2) || size(A2,1)~=size(A2,2)
25 error('SimilarityMatrix: The input matrices must be square!');
26 end
27
28 N1 = size(A1,1);
29 N2 = size(A2,1);
30
31 if N1>N2
32 error('SimilarityMatrix: The first input matrix must be smaller than the second one!');
33 end
34
35 [Q1,R1]=schur(A1,'complex');
36 [Q2,R2]=schur(A2,'complex');
37
38 c1 = sum(Q2',2);
39 c2 = sum(Q1',2);
40 I = eye(N2);
41 X = zeros(N1,N2);
42 for k=N1:-1:1
43 M = R1(k,k)*I-R2;
44 if k==N1
45 m = zeros(1,N2);
46 else
47 m = -R1(k,k+1:end)*X(k+1:end,:);
48 end
49 X(k,:) = linsolve([M,c1]',[m,c2(k)]')';
50 end
51 B = real(Q1*X*Q2');
52end