quant-ph0507171/rjd.m
1: function [ V ,  qDs ]= rjd(A,threshold)
2: %***************************************
3: % joint diagonalization (possibly
4: % approximate) of REAL matrices.
5: %***************************************
6: % This function minimizes a joint diagonality criterion
7: % through n matrices of size m by m.
8: %
9: % Input :
10: % * the  n by nm matrix A is the concatenation of m matrices
11: %   with size n by n. We denote A = [ A1 A2 .... An ]
12: % * threshold is an optional small number (typically = 1.0e-8 see below).
13: %
14: % Output :
15: % * V is a n by n orthogonal matrix.
16: % * qDs is the concatenation of (quasi)diagonal n by n matrices:
17: %   qDs = [ D1 D2 ... Dn ] where A1 = V*D1*V' ,..., An =V*Dn*V'.
18: %
19: % The algorithm finds an orthogonal matrix V
20: % such that the matrices D1,...,Dn  are as diagonal as possible,
21: % providing a kind of `average eigen-structure' shared
22: % by the matrices A1 ,..., An.
23: % If the matrices A1,...,An do have an exact common eigen-structure
24: % ie a common othonormal set eigenvectors, then the algorithm finds it.
25: % The eigenvectors THEN are the column vectors of V
26: % and D1, ...,Dn are diagonal matrices.
27: % 
28: % The algorithm implements a properly extended Jacobi algorithm.
29: % The algorithm stops when all the Givens rotations in a sweep
30: % have sines smaller than 'threshold'.
31: % In many applications, the notion of approximate joint diagonalization
32: % is ad hoc and very small values of threshold do not make sense
33: % because the diagonality criterion itself is ad hoc.
34: % Hence, it is often not necessary to push the accuracy of
35: % the rotation matrix V to the machine precision.
36: % It is defaulted here to the square root of the machine precision.
37: % 
38: %
39: % Author : Jean-Francois Cardoso. cardoso@sig.enst.fr
40: % This software is for non commercial use only.
41: % It is freeware but not in the public domain.
42: % A version for the complex case is available
43: % upon request at cardoso@sig.enst.fr
44: %-----------------------------------------------------
45: % Two References:
46: %
47: % The algorithm is explained in:
48: %
49: %@article{SC-siam,
50: %   HTML =	"ftp://sig.enst.fr/pub/jfc/Papers/siam_note.ps.gz",
51: %   author = "Jean-Fran\c{c}ois Cardoso and Antoine Souloumiac",
52: %   journal = "{SIAM} J. Mat. Anal. Appl.",
53: %   title = "Jacobi angles for simultaneous diagonalization",
54: %   pages = "161--164",
55: %   volume = "17",
56: %   number = "1",
57: %   month = jan,
58: %   year = {1995}}
59: %
60: %  The perturbation analysis is described in
61: %
62: %@techreport{PertDJ,
63: %   author = "{J.F. Cardoso}",
64: %   HTML =	"ftp://sig.enst.fr/pub/jfc/Papers/joint_diag_pert_an.ps",
65: %   institution = "T\'{e}l\'{e}com {P}aris",
66: %   title = "Perturbation of joint diagonalizers. Ref\# 94D027",
67: %   year = "1994" }
68: %
69: %
70: %
71: [m,nm] = size(A);
72: V=eye(m);
73: 
74: if nargin==1, threshold=sqrt(eps); end;
75: 
76: encore=1;
77: while encore, encore=0;
78:  for p=1:m-1,
79:   for q=p+1:m,
80:    %%%computation of Givens rotations
81:    g=[ A(p,p:m:nm)-A(q,q:m:nm) ; A(p,q:m:nm)+A(q,p:m:nm) ];
82:    g=g*g';
83:    ton =g(1,1)-g(2,2); toff=g(1,2)+g(2,1);
84:    theta=0.5*atan2( toff , ton+sqrt(ton*ton+toff*toff) );
85:    c=cos(theta);s=sin(theta);
86:    encore=encore | (abs(s)>threshold);
87:     %%%update of the A and V matrices 
88:    if (abs(s)>threshold) ,
89:     Mp=A(:,p:m:nm);Mq=A(:,q:m:nm);
90:     A(:,p:m:nm)=c*Mp+s*Mq;A(:,q:m:nm)=c*Mq-s*Mp;
91:     rowp=A(p,:);rowq=A(q,:);
92:     A(p,:)=c*rowp+s*rowq;A(q,:)=c*rowq-s*rowp;
93:     temp=V(:,p);V(:,p)=c*V(:,p)+s*V(:,q); V(:,q)=c*V(:,q)-s*temp;
94:    end%%of the if
95:   end%%of the loop on q
96:  end%%of the loop on p
97: end%%of the while loop
98: qDs = A ;
99: