LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
LeftQR.m
1function varargout=LeftQR(varargin)
2%
3% [R,Q]=LeftQR(A)
4% Computes an orthogonal factorization Q such that Q*A = R is upper triangular
5% by means of householder transformations
6%
7% when a third output argument is added a column permutation is done such that
8% diagonal elements of R are sorted in absolute decreasing values
9% [R,Q,e]=LeftQR(A), Q*A(:,e)=R
10%
11% when a second input arguments is present, Q is not computed explicitly.
12% orthogonal transformations are directly applied to B.
13% on output B contains the transformed B.
14% [R,B]=LeftQR(A,B)
15% [R,B,e]=LeftQR(A,B)
16%
17% Remark .- Real and complex matrix ar supported
18%
19% Author : Alain Barraud, copyright abc.consultant@wanadoo.fr 2003-2018
20%
21%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22narg=nargin;
23if narg==0,error('LeftQR requires at least one argument');end
24A=varargin{1};
25if narg==2
26 B=varargin{2};
27else
28 B=[];
29end
30if nargout==3, Pivoting=true;else, Pivoting=false;end
31%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32%initialisation and memory allocation
33[m,n] = size(A);%A dimension
34if Pivoting, e=(1:n);end
35r=min(m,n);
36if nargin==1
37 Q=eye(m);%memory allocation
38end
39%begin factorization
40for k = 1:r%loop on the A columns
41 if Pivoting%do columns permutation (rank revealing)
42 I=(k:m);%the set of used row indices for pivoting
43 J=(k:n);%the set of used columns indices for pivoting
44 tmp=sum(A(I,J).*conj(A(I,J)),1);[tmpx,piv]=max(tmp);
45 piv=k-1+piv;%piv is the next column to be used
46 if piv~=k, tmp=e(k);e(k)=e(piv);e(piv)=tmp;tmp=A(:,k);A(:,k)=A(:,piv);A(:,piv)=tmp;end
47 end
48 [A(:,k),uk,betak]=CHouseholder(A(:,k),(k+1:m),k);%compute the kth Householder transformation
49 if Pivoting
50 if k==1
51 Ref=abs(A(1,1));
52 elseif tmpx<n*eps*Ref
53 A(k:m,:)=0;
54 break
55 end
56 end
57 %apply this transformation to the following A(:,J) columns if any
58 if k<n,J=(k+1:n);A(:,J)=ApplyHouseholder(A(:,J),uk,betak,'L');end
59 if nargin==1%Compute Q explicitly
60 Q=ApplyHouseholder(Q,uk,betak,'L');
61 elseif ~isempty(B)%apply Q to B
62 B=ApplyHouseholder(B,uk,betak,'L');
63 end
64end
65varargout{1}=A(1:r,:);
66if nargin==2
67 varargout{2}=B;
68else
69 varargout{2}=Q;
70end
71if nargout==3
72 varargout{3}=e;
73end
74%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%