-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingular_vectors.m
72 lines (58 loc) · 1.89 KB
/
singular_vectors.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function [M, H] = singular_vectors(A, toll, left)
%SINGULAR_VECTORS Function to compute the left or right singular vectors
% Given a matrix A and a tollerance for the stopping criterion, this
% function computes the left or right singular vectors of A and its
% corrisponding singular values. If left is true then U is calculated, V
% otherwise.
if left
[H, P] = hessemberg(A * A.');
else
[H, P] = hessemberg(A.' * A);
end
n = length(H);
% G will hold c and s for each step
G = zeros(n - 1, 2);
% G_aux will be equal to [c, -s; s, c] for each step
G_aux = zeros(2);
% M will contain the eigenvectors
M = P;
err = toll + 1;
while err > toll
H1 = H;
T = H(n, n) * eye(n);
% shifting
H = H - T;
for k = 1:n-1
[G(k, 1), G(k, 2)] = givens(H, k, k+1);
G_aux(1, 1) = G(k, 1);
G_aux(1, 2) = - G(k, 2);
G_aux(2, 1) = G(k, 2);
G_aux(2, 2) = G(k, 1);
% nullify the elements below the diagonal
H(k:k+1, k:n) = G_aux * H(k:k+1, k:n);
end
for k = 1:n-1
G_aux(1, 1) = G(k, 1);
G_aux(1, 2) = G(k, 2);
G_aux(2, 1) = - G(k, 2);
G_aux(2, 2) = G(k, 1);
% reconstruct the Hessemberg form
H(1:k+1, k:k+1) = H(1:k+1, k:k+1) * G_aux;
% update M as M * Q = M * G12' * G23' * ...
M(1:n, k:k+1) = M(1:n, k:k+1) * G_aux;
end
% shifting
H = H + T;
err = norm(diag(H - H1), 1);
end
% calculate square root for singular values
H = sqrt( diag( diag( H ) ) );
% discard imaginary parts if too small
discard_imag = 10^-5;
if all( imag( diag(H) ) < discard_imag )
H = real(H);
end
if all( imag( diag(M) ) < discard_imag )
M = real(M);
end
end