comparison toolbox/trap2tri.m @ 0:8f23314345f4 draft

Create local repository for matrix toolboxes. Step #0 done.
author Antonio Pino Robles <data.script93@gmail.com>
date Wed, 06 May 2015 14:56:53 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8f23314345f4
1 function [Q, T] = trap2tri(L)
2 %TRAP2TRI Unitary reduction of trapezoidal matrix to triangular form.
3 % [Q, T] = TRAP2TRI(L), where L is an m-by-n lower trapezoidal
4 % matrix with m >= n, produces a unitary Q such that QL = [T; 0],
5 % where T is n-by-n and lower triangular.
6 % Q is a product of Householder transformations.
7
8 % Called by RANDSVD.
9 %
10 % Reference:
11 % G.H. Golub and C.F. Van Loan, Matrix Computations, second edition,
12 % Johns Hopkins University Press, Baltimore, Maryland, 1989.
13 % P5.2.5, p. 220.
14
15 [n, r] = size(L);
16
17 if r > n | norm(L-tril(L),1)
18 error('Matrix must be lower trapezoidal and m-by-n with m >= n.')
19 end
20
21 Q = eye(n); % To hold product of H.T.s
22
23 if r ~= n
24
25 % Reduce nxr L = r [L1] to lower triangular form: QL = [T].
26 % n-r [L2] [0]
27
28 for j=r:-1:1
29 % x is the vector to be reduced, which we overwrite with the H.T. vector.
30 x = L(j:n,j);
31 x(2:r-j+1) = zeros(r-j,1); % These elts of column left unchanged.
32 s = norm(x)*(sign(x(1)) + (x(1)==0)); % Modification for sign(1)=1.
33
34 % Nothing to do if x is zero (or x=a*e_1, but we don't check for that).
35 if s ~= 0
36 x(1) = x(1) + s;
37 beta = s'*x(1);
38
39 % Implicitly apply H.T. to pivot column.
40 % L(r+1:n,j) = zeros(n-r,1); % We throw these elts away at the end.
41 L(j,j) = -s;
42
43 % Apply H.T. to rest of matrix.
44 if j > 1
45 y = x'*L(j:n, 1:j-1);
46 L(j:n, 1:j-1) = L(j:n, 1:j-1) - x*(y/beta);
47 end
48
49 % Update H.T. product.
50 y = x'*Q(j:n,:);
51 Q(j:n,:) = Q(j:n,:) - x*(y/beta);
52 end
53 end
54 end
55
56 T = L(1:r,:); % Rows r+1:n have been zeroed out.