-
Notifications
You must be signed in to change notification settings - Fork 61
/
linop_explicit.m
41 lines (38 loc) · 1.17 KB
/
linop_explicit.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
function A = linop_explicit( op )
%LINOP_EXPLICIT Outputs the explicit matrix representation
% of a (implicitly defined) linear operator.
% Useful for checking correctness of code.
% A = LINOP_EXPLICIT( OP )
% returns the matrix A such that A*X = OP(X)
%
% Note: may not play well with linear operators defined on complex numbers
% (but should be OK if the output inclides complex numbers)
% Introduced June 2016
if nargin == 0,
error( 'Not enough input arguments.' );
end
sz = op([],0);
if isnumeric(sz),
sz = { [sz(2),1], [sz(1),1] };
end
% convert [n1;n2] to [n1,n2] if necessary:
for kk = 1:2
sz{kk} = sz{kk}(:).';
end
% If inputs and outputs are not vectors, then we cannot represent
% with matrix multiplication
if sz{1}(2) ~= 1 || sz{2}(2) ~= 1
error('Cannot represent this operator as matrix since input/output is not a vector');
end
m = sz{2}(1);
n = sz{1}(1);
e = zeros(n,1);
A = zeros(m,n);
for i = 1:n
e(i) = 1;
A(:,i) = op( e, 1 );
e(i) = 0;
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.