-
Notifications
You must be signed in to change notification settings - Fork 29
/
opBlockDiag.m
290 lines (243 loc) · 8.63 KB
/
opBlockDiag.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
classdef opBlockDiag < opSpot
%OPBLOCKDIAG Operator-diagonal operator.
%
% B = opBlockDiag(OP1, OP2,...,OPN,OVERLAP) creates a compound block
% operator with the input operators OP1, OP2,... on the diagonal of
% B, e.g., B = DIAG([OP1 OP2 ... OPN]). When OVERLAP is a positive
% integer the blocks will be offset OVERLAP rows relative to the
% previous operator, when OVERLAP is negative the operators are
% offset by the absolute value of OVERLAP in columns. Note that
% choosing OVERLAP larger than the operator size may cause the
% matrix to become block antidiagonal.
%
% B = opBlockDiag(WEIGHT,OP1,...,OPN,OVERLAP) additionally
% weights each block by the elements of the vector WEIGHT. If
% only a single operator is given it is replicated as many times
% as there are weights.
%
% B = opBlockDiag(N,OP,OVERLAP) similar as above with WEIGHT
% equal to ones(N,1). This will cause operator OP to be repeated
% N times.
%
% See also opFoG, opKron, opDictionary.
% Copyright 2009, Ewout van den Berg and Michael P. Friedlander
% See the file COPYING.txt for full copyright information.
% Use the command 'spot.gpl' to locate this file.
% http://www.cs.ubc.ca/labs/scl/spot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Properties
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
properties( SetAccess = private )
funHandle % Multiplication function
end % properties
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constructor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function op = opBlockDiag(varargin)
% Extract weight, overlap, and operator list parameters
idxStart = 1; idxEnd = nargin;
weights = [];
overlap = 0;
if nargin > 0 && isnumeric(varargin{1})
weights = varargin{1};
weights = weights(:);
idxStart = idxStart + 1;
end
if nargin > 2 && isscalar(varargin{end})
overlap = varargin{end};
idxEnd = nargin - 1;
end
opList = varargin(idxStart:idxEnd);
% Check number of operators
if isempty(opList)
error('At least one operator must be specified.');
end
% Check overlap parameter
if ~spot.utils.isposintscalar(abs(overlap)+1)
error('Overlap must be an integer scalar.');
end
% Convert all arguments to operators
for i=1:length(opList)
if ~isa(opList{i},'opSpot')
opList{i} = opMatrix(opList{i});
end
end
% Set weights
if isempty(weights)
weights = ones(length(opList),1);
elseif spot.utils.isposintscalar(weights) && length(opList) == 1
weights = ones(weights,1);
end;
% Check complexity and size and repeat operators
opListNew = {};
if length(opList) == 1
% Repeat one operator with given weights
opA = opList{1};
[m,n] = size(opA);
m = m * length(weights);
n = n * length(weights);
% Get complexity
cflag = ~isreal(opA) || ~all(isreal(weights));
linear = opA.linear;
% Add operators to list
for i=1:length(weights)
opListNew{end+1} = opA;
end
else
% Initialize
m = 0; n = 0; cflag = 0; linear = 1;
for i=1:length(opList)
% Get operator and update size
opA = opList{i};
m = m + size(opA,1);
n = n + size(opA,2);
% Get complexity
cflag = cflag | ~isreal(opA) | ~isreal(weights(i));
linear = linear & opA.linear;
% Append operator
opListNew{end+1} = opA;
end
end
opList = opListNew;
% Construct function handle
if overlap == 0
fun = @(x,mode) opBlockDiag_intrnl(m,n,opList,weights,x,mode);
elseif overlap < 0
% Overlap in columns
m = 0; n = 0; colOffset = 0; column = 0;
% Compute number of columns and column offset of first operator
for i=1:length(opList)
[mOp,nOp] = size(opList{i});
m = m + mOp;
n = max(n,column+nOp);
colOffset = min(colOffset,column);
column = column + nOp + overlap; % Overlap is negative
end
n = n - colOffset;
fun = @(x,mode) opBlockDiagCol_intrnl(m,n,-colOffset,-overlap,opList,weights,x,mode);
else
% Overlap in rows
m = 0; n = 0; rowOffset = 0; row = 0;
% Compute number of rows and row offset of first operator
for i=1:length(opList)
[mOp,nOp] = size(opList{i});
m = max(m,row+mOp);
n = n + nOp;
rowOffset = min(rowOffset,row);
row = row + mOp - overlap; % Overlap is positive
end
m = m - rowOffset;
fun = @(x,mode) opBlockDiagRow_intrnl(m,n,-rowOffset,overlap,opList,weights,x,mode);
end
% Construct operator
op = op@opSpot('BlockDiag', m, n);
op.cflag = cflag;
op.linear = linear;
op.children = opList;
op.funHandle = fun;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function str = char(op)
% Initialize
str = 'BlockDiag(';
for i=1:length(op.children)
strOp = char(op.children{i});
if i~=1
str = [str, ', ', strOp];
else
str = [str, strOp];
end
end
str = [str, ')'];
end % Display
end % Methods
methods ( Access = protected )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Multiply
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = multiply(op,x,mode)
y = op.funHandle(x,mode);
end % Multiply
end % Methods
end % Classdef
%=======================================================================
function y = opBlockDiag_intrnl(m,n,opList,weights,x,mode)
kx = 0; ky = 0;
if mode == 1
y = zeros(m,1);
for i=1:length(opList)
op = opList{i};
mOp = op.m;
nOp = op.n;
y(ky+1:ky+mOp) = weights(i) * op * x(kx+1:kx+nOp);
kx = kx + nOp;
ky = ky + mOp;
end;
else
y = zeros(n,1);
for i=1:length(opList)
op = opList{i};
mOp = op.m;
nOp = op.n;
y(ky+1:ky+nOp) = conj(weights(i)) * op' * x(kx+1:kx+mOp);
kx = kx + mOp;
ky = ky + nOp;
end
end
end
%=======================================================================
function y = opBlockDiagCol_intrnl(m,n,offset,overlap,opList,weights,x,mode)
kx = 0; ky = 0;
if mode == 1
kx = offset; y = zeros(m,1);
for i=1:length(opList)
op = opList{i};
mOp = op.m;
nOp = op.n;
y(ky+1:ky+mOp) = weights(i) * (op * x(kx+1:kx+nOp));
kx = kx + nOp - overlap;
ky = ky + mOp;
end
else
ky = offset; y = zeros(n,1);
for i=1:length(opList)
op = opList{i};
mOp = op.m;
nOp = op.n;
y(ky+1:ky+nOp) = y(ky+1:ky+nOp) + conj(weights(i))*(op' * x(kx+1:kx+mOp));
kx = kx + mOp;
ky = ky + nOp - overlap;
end
end
end
%=======================================================================
function y = opBlockDiagRow_intrnl(m,n,offset,overlap,opList,weights,x,mode)
kx = 0; ky = 0;
if mode == 1
ky = offset; y = zeros(m,1);
for i=1:length(opList)
op = opList{i};
mOp = op.m;
nOp = op.n;
y(ky+1:ky+mOp) = y(ky+1:ky+mOp) + weights(i) * (op * x(kx+1:kx+nOp));
kx = kx + nOp;
ky = ky + mOp - overlap;
end
else
kx = offset; y = zeros(n,1);
for i=1:length(opList)
op = opList{i};
mOp = op.m;
nOp = op.n;
y(ky+1:ky+nOp) = conj(weights(i)) * (op' * x(kx+1:kx+mOp));
kx = kx + mOp - overlap;
ky = ky + nOp;
end
end
end