Wednesday 6 January 2021

How to find the common eigenvectors of two matrices with distincts eigenvalues

I am looking for finding or rather building common eigenvectors matrix X between 2 matrices A and B such as :

AX=aX with "a" the diagonal matrix corresponding to the eigenvalues

BX=bX with "b" the diagonal matrix corresponding to the eigenvalues

where A and B are square and diagonalizable matrices.

I took a look in a similar post but had not managed to conclude, i.e having valid results when I build the final wanted endomorphism F defined by : F = P D P^-1

I have also read the wikipedia topic and this interesting paper but couldn't have to extract methods pretty easy to implement.

Particularly, I am interested by the eig(A,B) Matlab function.

I tried to use it like this :

% Search for common build eigen vectors between FISH_sp and FISH_xc
[V,D] = eig(FISH_sp,FISH_xc);
% Diagonalize the matrix (A B^-1) to compute Lambda since we have AX=Lambda B X
[eigenv, eigen_final] = eig(inv(FISH_xc)*FISH_sp);
% Compute the final endomorphism : F = P D P^-1
FISH_final = V*eye(7).*eigen_final*inv(V)

But the matrix FISH_final don't give good results since I can do other computations from this matrix FISH_final (this is actually a Fisher matrix) and the results of these computations are not valid.

So surely, I must have done an error in my code snippet above. In a first time, I prefer to conclude in Matlab as if it was a prototype, and after if it works, look for doing this synthesis with MKL or with Python functions. Hence also tagging python.

If someone could help me to build these common eigenvectors and finding also the eigenvalues associated, this would be fine to tell it, I am a little lost between all the potential methods that exist to carry it out.

EDIT 1:

I have tried to apply the method described in the solution of @Argyll . Below the script :

% Search for common build eigen vectors between FISH_sp and FISH_xc
[V1,D1] = eig(FISH_sp);
[V2,D2] = eig(FISH_xc);
% Check espilon
for i=1:7
  tol=sum(abs(FISH_sp*V1(:,i)-D1(i)*V1(:,i)));
  tol
  tol=sum(abs(FISH_xc*V2(:,i)-D2(i)*V2(:,i)));
  tol
end
% Sorting
[d1,I1]=sort(diag(D1))
[d2,I2]=sort(diag(D2))
% Identify repeated elemnts
[~,ia,~]=unique(d1,'stable')
[~,ib,~]=unique(d2,'stable')
% Find a same space
W1 = V1(:,I1)
W2 = V2(:,I2)
% Loop for rref
m = zeros(numel(ia,ib));
numel(ia)
numel(ib)
for i=1:numel(ia)
    for j=1:numel(ib)
         %check_linear_dependency(col1,col2);
         [R,p] = rref(W1(:,ia(i):ia(i+1)-1), W2(:,ib(j):ib(j+1)-1));
    end
end
%%% I don't know after loop what to do to build common eigenvectors %%%

As you can see, I don't know the next step to do in order to build the matrix of common eigenvectors.

EDIT 2: I have modified my initial script while trying to follow the advices of @Argyll . Below the script :

% Search for common build eigen vectors between FISH_sp and FISH_xc
[V1,D1] = eig(FISH_sp);
[V2,D2] = eig(FISH_xc);
% Sorting
[d1,I1]=sort(diag(D1))
[d2,I2]=sort(diag(D2))
% Identify repeated elemnts
[~,ia,~]=unique(d1,'stable')
[~,ib,~]=unique(d2,'stable')
% Find a same space
W1 = V1(:,I1)
W2 = V2(:,I2)
% Loop for rref
m = zeros(numel(ia,ib));
m = zeros(numel(ia,ib));
numel(ia)
numel(ib)
for i=1:numel(ia)
  if (i==numel(ia) && i==numel(ib))
         col1 = W1(:,ia(i):ia(i+1)-1)
         col2 = W2(:,ib(i):ib(i+1)-1)
         m = null( [null(col1.').' ; null(col2.').'] )
         V(:,i) = m
       else
         col1 = W1(:,ia(i):ia(i+1))
         col2 = W2(:,ib(i):ib(i+1))
         m = null( [null(col1.').' ; null(col2.').'] )
         V(:,i) = m
       end
end

But an error occurs :

col1 =

   -0.0319    0.1203
   -0.0065    0.0321
   -0.3004    0.9392
    0.9522    0.3036
    0.0000   -0.0481
    0.0303    0.0018
    0.0332   -0.0892


col2 =

   -0.0133   -0.0077
   -0.0021   -0.1495
   -0.2471   -0.0179
    0.9687    0.0084
    0.0133   -0.9609
   -0.0020    0.2317
    0.0136    0.0144


m =

  7x0 empty double matrix

Unable to perform assignment because the size of the left side is 7-by-1 and the size of the right side is
7-by-0.

Error in compute_solving_stack_common_eigen_vectors_dev (line 72)
             V(:,i) = m

EDIT 3: I have kept the double loop on indices i and j and modified the last script above like this :

V = []
%numel(ia) equal to 7
%numel(ib) equal to 7
for i=1:numel(ia)
    for j=1:numel(ib)
           %check_linear_dependency(col1,col2);
           if (i==numel(ia) && j==numel(ib))
             col1 = W1(:,ia(end):end)
             col2 = W2(:,ib(end):end)
             m = null([null(col1.').' ; null(col2.').'])
             V=[V,m]
           else
             col1 = W1(:,ia(i):ia(i+1)-1)
             col2 = W2(:,ib(j):ib(j+1)-1)
             m = null([null(col1.').' ; null(col2.').'])
             V=[V,m]
           end
    end
end

But I get always the following error (I show only the end) :

...
col1 =

   -0.0319
   -0.0065
   -0.3004
    0.9522
    0.0000
    0.0303
    0.0332


col2 =

    0.2689
    0.8416
   -0.0630
   -0.0137
   -0.1810
   -0.2257
    0.3627


m =

  7x0 empty double matrix


V =

  7x0 empty double matrix


col1 =

   -0.0319
   -0.0065
   -0.3004
    0.9522
    0.0000
    0.0303
    0.0332

Index exceeds the number of array elements (7).

Error in compute_solving_stack_common_eigen_vectors_dev (line 72)
             col2 = W2(:,ib(j):ib(j+1)-1)

Why does the element m and so V remains to zero quantity (for example 7x0 empty double matrix) ?

I don't want to be boring or too insistent (thanks again @Argyll), so maybe I will launch a bounty to debug this part of the code, i.e this double loop.

IMPORTANT REMARK : Maybe some of you didn't fully understand my goal.

Concerning the common basis of eigen vectors, I am looking for a combination (vectorial or matricial) of V1 and V2 to build this new basis "P" in which, with others eigenvalues than known D1 and D2 (noted D1a and D2a'), we could have :

F = P (D1a+D2a) P^-1

To compute the new Fisher matrix F, I need to know P, D1a and D2a.

If I know common basis of eigen vectirs P, I can deduce D1a and Da2 from D1 and D2.



from How to find the common eigenvectors of two matrices with distincts eigenvalues

No comments:

Post a Comment