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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup spop_diagmat
//! @{
template<typename T1>
inline
void
spop_diagmat::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_diagmat>& in)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
if(in.is_alias(out) == false)
{
spop_diagmat::apply_noalias(out, in.m);
}
else
{
SpMat<eT> tmp;
spop_diagmat::apply_noalias(tmp, in.m);
out.steal_mem(tmp);
}
}
template<typename T1>
inline
void
spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpBase<typename T1::elem_type, T1>& expr)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const SpProxy<T1> P(expr.get_ref());
const uword P_n_rows = P.get_n_rows();
const uword P_n_cols = P.get_n_cols();
const uword P_n_nz = P.get_n_nonzero();
const bool P_is_vec = (P_n_rows == 1) || (P_n_cols == 1);
if(P_is_vec) // generate a diagonal matrix out of a vector
{
const uword N = (P_n_rows == 1) ? P_n_cols : P_n_rows;
out.zeros(N, N);
if(P_n_nz == 0) { return; }
typename SpProxy<T1>::const_iterator_type it = P.begin();
if(P_n_cols == 1)
{
for(uword i=0; i < P_n_nz; ++i)
{
const uword row = it.row();
out.at(row,row) = (*it);
++it;
}
}
else
if(P_n_rows == 1)
{
for(uword i=0; i < P_n_nz; ++i)
{
const uword col = it.col();
out.at(col,col) = (*it);
++it;
}
}
}
else // generate a diagonal matrix out of a matrix
{
out.zeros(P_n_rows, P_n_cols);
const uword N = (std::min)(P_n_rows, P_n_cols);
if( (is_SpMat<typename SpProxy<T1>::stored_type>::value) && (P_n_nz >= 5*N) )
{
const unwrap_spmat<typename SpProxy<T1>::stored_type> U(P.Q);
const SpMat<eT>& X = U.M;
for(uword i=0; i < N; ++i)
{
const eT val = X.at(i,i); // use binary search
if(val != eT(0)) { out.at(i,i) = val; }
}
}
else
{
if(P_n_nz == 0) { return; }
typename SpProxy<T1>::const_iterator_type it = P.begin();
for(uword i=0; i < P_n_nz; ++i)
{
const uword row = it.row();
const uword col = it.col();
if(row == col) { out.at(row,row) = (*it); }
++it;
}
}
}
}
template<typename T1, typename T2>
inline
void
spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_plus>& expr)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const unwrap_spmat<T1> UA(expr.A);
const unwrap_spmat<T2> UB(expr.B);
const SpMat<eT>& A = UA.M;
const SpMat<eT>& B = UB.M;
arma_debug_assert_same_size(A.n_rows, A.n_cols, B.n_rows, B.n_cols, "addition");
const bool is_vec = (A.n_rows == 1) || (A.n_cols == 1);
if(is_vec) // generate a diagonal matrix out of a vector
{
const uword N = (A.n_rows == 1) ? A.n_cols : A.n_rows;
out.zeros(N,N);
if(A.n_rows == 1)
{
for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(0,i) + B.at(0,i); }
}
else
{
for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(i,0) + B.at(i,0); }
}
}
else // generate a diagonal matrix out of a matrix
{
SpMat<eT> AA; spop_diagmat::apply_noalias(AA, A);
SpMat<eT> BB; spop_diagmat::apply_noalias(BB, B);
out = AA + BB;
}
}
template<typename T1, typename T2>
inline
void
spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_minus>& expr)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const unwrap_spmat<T1> UA(expr.A);
const unwrap_spmat<T2> UB(expr.B);
const SpMat<eT>& A = UA.M;
const SpMat<eT>& B = UB.M;
arma_debug_assert_same_size(A.n_rows, A.n_cols, B.n_rows, B.n_cols, "subtraction");
const bool is_vec = (A.n_rows == 1) || (A.n_cols == 1);
if(is_vec) // generate a diagonal matrix out of a vector
{
const uword N = (A.n_rows == 1) ? A.n_cols : A.n_rows;
out.zeros(N,N);
if(A.n_rows == 1)
{
for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(0,i) - B.at(0,i); }
}
else
{
for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(i,0) - B.at(i,0); }
}
}
else // generate a diagonal matrix out of a matrix
{
SpMat<eT> AA; spop_diagmat::apply_noalias(AA, A);
SpMat<eT> BB; spop_diagmat::apply_noalias(BB, B);
out = AA - BB;
}
}
template<typename T1, typename T2>
inline
void
spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_schur>& expr)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const unwrap_spmat<T1> UA(expr.A);
const unwrap_spmat<T2> UB(expr.B);
const SpMat<eT>& A = UA.M;
const SpMat<eT>& B = UB.M;
arma_debug_assert_same_size(A.n_rows, A.n_cols, B.n_rows, B.n_cols, "element-wise multiplication");
const bool is_vec = (A.n_rows == 1) || (A.n_cols == 1);
if(is_vec) // generate a diagonal matrix out of a vector
{
const uword N = (A.n_rows == 1) ? A.n_cols : A.n_rows;
out.zeros(N,N);
if(A.n_rows == 1)
{
for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(0,i) * B.at(0,i); }
}
else
{
for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(i,0) * B.at(i,0); }
}
}
else // generate a diagonal matrix out of a matrix
{
SpMat<eT> AA; spop_diagmat::apply_noalias(AA, A);
SpMat<eT> BB; spop_diagmat::apply_noalias(BB, B);
out = AA % BB;
}
}
template<typename T1, typename T2>
inline
void
spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_times>& expr)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const unwrap_spmat<T1> UA(expr.A);
const unwrap_spmat<T2> UB(expr.B);
const SpMat<eT>& A = UA.M;
const SpMat<eT>& B = UB.M;
arma_debug_assert_mul_size(A.n_rows, A.n_cols, B.n_rows, B.n_cols, "matrix multiplication");
const uword C_n_rows = A.n_rows;
const uword C_n_cols = B.n_cols;
const bool is_vec = (C_n_rows == 1) || (C_n_cols == 1);
if(is_vec) // generate a diagonal matrix out of a vector
{
const SpMat<eT> C = A*B;
spop_diagmat::apply_noalias(out, C);
}
else // generate a diagonal matrix out of a matrix
{
const uword N = (std::min)(C_n_rows, C_n_cols);
if( (A.n_nonzero >= 5*N) || (B.n_nonzero >= 5*N) )
{
out.zeros(C_n_rows, C_n_cols);
for(uword k=0; k < N; ++k)
{
typename SpMat<eT>::const_col_iterator B_it = B.begin_col_no_sync(k);
typename SpMat<eT>::const_col_iterator B_it_end = B.end_col_no_sync(k);
eT acc = eT(0);
while(B_it != B_it_end)
{
const eT B_val = (*B_it);
const uword i = B_it.row();
acc += A.at(k,i) * B_val;
++B_it;
}
out(k,k) = acc;
}
}
else
{
const SpMat<eT> C = A*B;
spop_diagmat::apply_noalias(out, C);
}
}
}
//
//
template<typename T1>
inline
void
spop_diagmat2::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_diagmat2>& in)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const uword row_offset = in.aux_uword_a;
const uword col_offset = in.aux_uword_b;
const unwrap_spmat<T1> U(in.m);
if(U.is_alias(out))
{
SpMat<eT> tmp;
spop_diagmat2::apply_noalias(tmp, U.M, row_offset, col_offset);
out.steal_mem(tmp);
}
else
{
spop_diagmat2::apply_noalias(out, U.M, row_offset, col_offset);
}
}
template<typename eT>
inline
void
spop_diagmat2::apply_noalias(SpMat<eT>& out, const SpMat<eT>& X, const uword row_offset, const uword col_offset)
{
arma_extra_debug_sigprint();
const uword n_rows = X.n_rows;
const uword n_cols = X.n_cols;
const uword n_elem = X.n_elem;
if(n_elem == 0) { out.reset(); return; }
const bool X_is_vec = (n_rows == 1) || (n_cols == 1);
if(X_is_vec) // generate a diagonal matrix out of a vector
{
const uword n_pad = (std::max)(row_offset, col_offset);
out.zeros(n_elem + n_pad, n_elem + n_pad);
const uword X_n_nz = X.n_nonzero;
if(X_n_nz == 0) { return; }
typename SpMat<eT>::const_iterator it = X.begin();
if(n_cols == 1)
{
for(uword i=0; i < X_n_nz; ++i)
{
const uword row = it.row();
out.at(row_offset + row, col_offset + row) = (*it);
++it;
}
}
else
if(n_rows == 1)
{
for(uword i=0; i < X_n_nz; ++i)
{
const uword col = it.col();
out.at(row_offset + col, col_offset + col) = (*it);
++it;
}
}
}
else // generate a diagonal matrix out of a matrix
{
arma_debug_check_bounds
(
((row_offset > 0) && (row_offset >= n_rows)) || ((col_offset > 0) && (col_offset >= n_cols)),
"diagmat(): requested diagonal out of bounds"
);
out.zeros(n_rows, n_cols);
if(X.n_nonzero == 0) { return; }
const uword N = (std::min)(n_rows - row_offset, n_cols - col_offset);
for(uword i=0; i<N; ++i)
{
const uword row = i + row_offset;
const uword col = i + col_offset;
const eT val = X.at(row,col);
if(val != eT(0)) { out.at(row,col) = val; }
}
}
}
//! @}
|