forked from dilipkay/hsc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compute_cn.m
116 lines (92 loc) · 2.39 KB
/
compute_cn.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function [c emin emax pc pemin pemax] = compute_cn(L,pfun)
%clf
N = size(L, 1);
Vmax = randn(size(L,1),1);
Vmin = randn(size(L,1),1);
VVmin = randn(size(L,1),1);
pVmax = randn(size(L,1),1);
pVmin = randn(size(L,1),1);
pVVmin = randn(size(L,1),1);
%gen_ev_plot(L, pfun);
for k=1:300
Vmax = L * Vmax ;
Vmax = Vmax - mean(Vmax);
Vmax = Vmax / norm(Vmax) ;
pVmax = pfun(L * pVmax) ;
pVmax = pVmax - mean(pVmax);
pVmax = pVmax / norm(pVmax) ;
emax(k) = Vmax'*L*Vmax ;
pemax(k) = pVmax'*pfun(L*pVmax) ;
end
%figure(202); clf
figure; clf;
subplot(2,2,1), plot(emax)
subplot(2,2,2), plot(pemax)
emax = Vmax'*L*Vmax ;
pemax = pVmax'*pfun(L*pVmax) ;
opemax = 0.9 / pemax;
oemax = 0.9 / emax;
tx = rand(size(L,1),1) ;
tx = tx - mean(tx) ;
b = L *tx ;
b = b - mean(b) ;
x = zeros(size(L,1),1) ;
for k=1:300
%pVmin = pVmin + opemax * (pVmin - pfun(L*pVmin)) ;
%if(k < 50)
pVmin = pVmin - opemax * pfun(L*pVmin) ;
pVmin = pVmin - mean(pVmin);
pVmin = pVmin / norm(pVmin) ;
pemin(k) = pVmin'*pfun(L*pVmin) ;
%end
%Vmin = Vmin + oemax * (Vmin - L*Vmin) ;
Vmin = Vmin - oemax * L*Vmin ;
Vmin = Vmin - mean(Vmin);
Vmin = Vmin / norm(Vmin) ;
emin(k) = Vmin'*L*Vmin ;
end
subplot(2,2,3), plot(emin), title('org')
hold on
if (0)
clear emin;
for k=1:30
VVmin = solve(VVmin, L, pfun) ;
VVmin = VVmin - mean(VVmin);
VVmin = VVmin / norm(VVmin) ;
emin(k) = VVmin'*L*VVmin ;
end
subplot(2,2,3), plot(emin,'r'), title('org')
end;
subplot(2,2,4), plot(pemin), title('pre')
n = sqrt(N);
%figure; imagesc(reshape(abs(pVmin), n, n)); title('Min eigenvector (Ours)');
emin = Vmin'*L*Vmin ;
emin = emin(end);
pemin = pVmin'*pfun(L*pVmin) ;
emax = eigs(L, 1, 'LM');
emin = eigs(L, 2, 'SM');
emin = sort(emin, 'ascend');
if (min(abs(emin)) < 1e-5)
emin = max(emin);
else
emin = min(emin);
end;
c = emax / emin;
pc = pemax / pemin;
function x = solve(b,L,pfun)
x = zeros(size(b)) ;
for k=1:100
x = x + 0.5 * (pfun(b-L*x)) ;
end
function gen_ev_plot(L, pfun)
d = eig(full(L));
figure; plot(sort(d)); title('Original EV');
N = size(L, 1);
B = zeros(N, N);
for i=1:N
ee = zeros(N, 1);
ee(i) = 1;
B(:, i) = pfun(ee);
end;
d = eig(L*B);
figure; plot(sort(abs(d))); title('Precond. EV');