-
Notifications
You must be signed in to change notification settings - Fork 61
/
proj_spectral.m
226 lines (207 loc) · 6.26 KB
/
proj_spectral.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
function op = proj_spectral( q, SYM_FLAG, LARGESCALE )
%PROJ_SPECTRAL Projection onto the set of matrices with spectral norm less than or equal to q
% OP = PROJ_SPECTRAL( q ) returns a function that implements the
% indicator for matrices with spectral norm less than q.
% Q is optional; if omitted, Q=1 is assumed. But if Q is supplied,
% it must be a positive real scalar.
% This function is like proj_psdUTrace.m but does not assume
% that inputs are square Hermitian positive semidefinite matrices.
% OP = PROJ_SPECTRAL( q, 'sym' ) or OP = PROJ_SPECTRAL( 'eig' )
% will instruct the code that the matrix is Hermitian and
% therefore the relations between singular- and eigen-values
% are well known, and the code will use the more efficient
% eigenvalue decomposition (instead of the SVD).
% OP = PROJ_SPECTRAL( ..., largescale) will switch to using
% svds (or PROPACK, if installed and in the path) or eigs
% instead of svd or eig, if largescale==true. This is usually
% beneficial for large, sparse variables.
%
% Dual: prox_nuclear(q)
% (if domain is pos. semidefinite matrices, then prox_trace(q)
% is also the dual, and is more efficient than prox_nuclear(q) ).
%
% See also prox_nuclear, proj_linf, proj_nuclear, prox_spectral, proj_maxEig
% Sept 1, 2012
if nargin < 3 || isempty(LARGESCALE)
LARGESCALE = false;
end
if nargin == 0,
q = 1;
elseif ~isnumeric( q ) || ~isreal( q ) || numel( q ) ~= 1 || q <= 0,
error( 'Argument must be positive.' );
end
vectorFunction = proj_linf( q );
if nargin >= 2 && ( ...
~isempty(strfind(lower(SYM_FLAG),'eig')) || ...
~isempty(strfind(lower(SYM_FLAG),'sym')) )
if LARGESCALE
% clear the persistent values:
proj_spectral_eigs_q();
op = @(varargin)proj_spectral_eigs_q( q,vectorFunction, varargin{:} );
else
op = @(varargin)proj_spectral_eig_q( q,vectorFunction, varargin{:} );
end
else
if LARGESCALE
% clear the persistent values:
proj_spectral_svds_q();
op = @(varargin)proj_spectral_svds_q( q,vectorFunction, varargin{:} );
else
op = @(varargin)proj_spectral_svd_q( q,vectorFunction, varargin{:} );
end
end
function [ v, X ] = proj_spectral_svd_q( q,eproj, X, t )
sx = size( X );
SP = issparse(X);
if length( sx ) > 2,
X = reshape( X, prod(sx(1:end-1)), sx(end) );
end
v = 0;
if nargin > 3 && t > 0,
if SP, X = full(X); end % svd, eig, and norm require full matrices
[U,D,V] = svd( X, 'econ' );
[dum,D] = eproj(diag(D),q); % not q*t, since we are just projecting...
X = U*diag(D)*V';
if SP, X = sparse(X); end
X = reshape( X, sx );
else
if SP,
[nrm,cnt] = normest(X,1e-3);
else
nrm = norm(X);
end
if nrm > q
v = Inf;
end
end
function [ v, X ] = proj_spectral_eig_q( q,eproj, X, t )
SP = issparse(X);
v = 0;
if nargin > 3 && t > 0,
if SP, X = full(X); end % svd, eig, and norm require full matrices
[V,D] = safe_eig(X); % just in case X is sparse
[dum,D] = eproj(diag(D),q); % not q*t, since we are just projecting...
X = V*diag(D)*V';
if SP, X = sparse(X); end
else
if SP, [nrm,cnt] = normest(X,1e-3);
else nrm = norm(X);
end
if nrm > q
v = Inf;
end
end
% --------------- largescale functions: use eigs or svds -----------------------
function [ v, X ] = proj_spectral_eigs_q( q,eproj, X, t )
persistent oldRank
persistent nCalls
persistent V
if nargin == 0, oldRank = []; v = nCalls; nCalls = []; V=[]; return; end
if isempty(nCalls), nCalls = 0; end
SP = issparse(X);
v = 0;
if nargin > 3 && t > 0,
if isempty(oldRank), K = 10;
else, K = oldRank + 2;
end
[M,N] = size(X);
ok = false;
opts = [];
opts.tol = 1e-10;
if isreal(X)
opts.issym = true;
end
while ~ok
K = min( [K,M,N] );
[V,D] = eigs( X, K, 'LM', opts );
ok = (min(abs(diag(D))) < q) || ( K == min(M,N) );
if ok, break; end
K = 2*K;
if K > 10
opts.tol = 1e-6;
end
if K > 40
opts.tol = 1e-4;
end
if K > 100
opts.tol = 1e-3;
end
if K > min(M,N)/2
[V,D] = safe_eig(full((X+X')/2));
ok = true;
end
end
oldRank = length(find(abs(diag(D)) > q));
[dum,D_proj] = eproj(diag(D),q);
% we want to keep the singular vectors that we haven't discovered
% small = X - V*D*V'
% large = V*D_proj*V'
% and add together to get X - V*(D-Dproj)*V'
X = X - V*diag(diag(D)-D_proj)*V';
if SP, X = sparse(X); end
else
if SP, [nrm,cnt] = normest(X,1e-3);
else nrm = norm(X);
end
if nrm > q
v = Inf;
end
end
function [ v, X ] = proj_spectral_svds_q( q,eproj, X, t )
persistent oldRank
persistent nCalls
persistent V
if nargin == 0, oldRank = []; v = nCalls; nCalls = []; V=[]; return; end
if isempty(nCalls), nCalls = 0; end
SP = issparse(X);
v = 0;
if nargin > 3 && t > 0,
if isempty(oldRank), K = 10;
else, K = oldRank + 2;
end
[M,N] = size(X);
ok = false;
opts = [];
opts.tol = 1e-10; % the default in svds
opt = [];
opt.eta = eps; % makes compute_int slow
opt.delta = 10*opt.eta;
while ~ok
K = min( [K,M,N] );
if exist('lansvd','file')
[U,D,V] = lansvd(X,K,'L',opt );
else
[U,D,V] = svds(X,K,'L',opts);
end
ok = (min(abs(diag(D))) < q) || ( K == min(M,N) );
if ok, break; end
K = 2*K;
if K > 10
opts.tol = 1e-6;
end
if K > 40
opts.tol = 1e-4;
end
if K > 100
opts.tol = 1e-3;
end
if K > min(M,N)/2
[U,D,V] = svd( full(X), 'econ' );
ok = true;
end
end
oldRank = length(find(abs(diag(D)) > q));
[dum,D_proj] = eproj(diag(D),q);
X = X - U*diag(diag(D)-D_proj)*V';
if SP, X = sparse(X); end
else
if SP, [nrm,cnt] = normest(X,1e-3);
else nrm = norm(X);
end
if nrm > q
v = Inf;
end
end
% TFOCS v1.3 by Stephen Becker, Emmanuel Candes, and Michael Grant.
% Copyright 2013 California Institute of Technology and CVX Research.
% See the file LICENSE for full license information.