-
Notifications
You must be signed in to change notification settings - Fork 60
/
proj_linfl2.m
68 lines (62 loc) · 1.92 KB
/
proj_linfl2.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
function op = proj_linfl2( q )
%PROJ_LINFL2 Projection of each row onto the scaled l2 norm ball.
% OP = PROJ_LINFL2( Q ) returns an operator implementing the
% indicator function for the set of l2 norm ball of size q,
% { X | for all rows i, norm( X(i,:),2) <= q }. Q is optional; if omitted,
% Q=1 is assumed. But if Q is supplied, it must be a positive
% real scalar.
% Dual: prox_l1l2.m
% See also: prox_l1, prox_linf, proj_l1
if nargin == 0,
q = 1;
elseif ~isnumeric( q ) || ~isreal( q ) || numel( q ) ~= 1 || q <= 0,
error( 'Argument must be positive.' );
end
% In r2007a and later, we can use bsxfun instead of the spdiags trick
if exist('OCTAVE_VERSION','builtin')
vr = '2000';
else
vr=version('-release');
end
if str2num(vr(1:4)) >= 2007
op = @(varargin)proj_linfl2_q_bsxfun( q, varargin{:} );
else
% the default, using spdiags
op = @(varargin)proj_linfl2_q( q, varargin{:} );
end
function [ v, x ] = proj_linfl2_q( q, x, t )
v = 0;
switch nargin,
case 2,
if nargout == 2,
error( 'This function is not differentiable.' );
elseif norm( x(:), Inf ) > q,
v = Inf;
end
case 3,
% Compute the norms of the rows
m = size(x,1);
nrms = sqrt( sum( abs(x).^2 , 2 ) );
% Scale the rows using left diagonal multiplication
x = spdiags( min(1,q./nrms), 0, m, m )*x;
otherwise,
error( 'Not enough arguments.' );
end
function [ v, x ] = proj_linfl2_q_bsxfun( q, x, t )
v = 0;
switch nargin,
case 2,
if nargout == 2,
error( 'This function is not differentiable.' );
elseif norm( x(:), Inf ) > q,
v = Inf;
end
case 3,
nrms = sqrt( sum( abs(x).^2 , 2 ) );
bsxfun( @times, x, min(1,q./nrms) );
otherwise,
error( 'Not enough arguments.' );
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.