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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
// 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_misc
//! @{
namespace priv
{
template<typename eT>
struct functor_scalar_times
{
const eT k;
functor_scalar_times(const eT in_k) : k(in_k) {}
arma_inline eT operator()(const eT val) const { return val * k; }
};
}
template<typename T1>
inline
void
spop_scalar_times::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_scalar_times>& in)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
if(in.aux != eT(0))
{
out.init_xform(in.m, priv::functor_scalar_times<eT>(in.aux));
}
else
{
const SpProxy<T1> P(in.m);
out.zeros( P.get_n_rows(), P.get_n_cols() );
}
}
namespace priv
{
template<typename T>
struct functor_cx_scalar_times
{
typedef std::complex<T> out_eT;
const out_eT k;
functor_cx_scalar_times(const out_eT in_k) : k(in_k) {}
arma_inline out_eT operator()(const T val) const { return val * k; }
};
}
template<typename T1>
inline
void
spop_cx_scalar_times::apply(SpMat< std::complex<typename T1::pod_type> >& out, const mtSpOp< std::complex<typename T1::pod_type>, T1, spop_cx_scalar_times >& in)
{
arma_extra_debug_sigprint();
typedef typename T1::pod_type T;
typedef typename std::complex<T> out_eT;
if(in.aux_out_eT != out_eT(0))
{
out.init_xform_mt(in.m, priv::functor_cx_scalar_times<T>(in.aux_out_eT));
}
else
{
const SpProxy<T1> P(in.m);
out.zeros( P.get_n_rows(), P.get_n_cols() );
}
}
namespace priv
{
struct functor_square
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return val*val; }
};
}
template<typename T1>
inline
void
spop_square::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_square>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_square());
}
namespace priv
{
struct functor_sqrt
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return eop_aux::sqrt(val); }
};
}
template<typename T1>
inline
void
spop_sqrt::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_sqrt>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_sqrt());
}
namespace priv
{
struct functor_abs
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return eop_aux::arma_abs(val); }
};
}
template<typename T1>
inline
void
spop_abs::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_abs>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_abs());
}
namespace priv
{
struct functor_cx_abs
{
template<typename T>
arma_inline T operator()(const std::complex<T>& val) const { return std::abs(val); }
};
}
template<typename T1>
inline
void
spop_cx_abs::apply(SpMat<typename T1::pod_type>& out, const mtSpOp<typename T1::pod_type, T1, spop_cx_abs>& in)
{
arma_extra_debug_sigprint();
out.init_xform_mt(in.m, priv::functor_cx_abs());
}
namespace priv
{
struct functor_arg
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return arma_arg<eT>::eval(val); }
};
}
template<typename T1>
inline
void
spop_arg::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_arg>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_arg());
}
namespace priv
{
struct functor_cx_arg
{
template<typename T>
arma_inline T operator()(const std::complex<T>& val) const { return std::arg(val); }
};
}
template<typename T1>
inline
void
spop_cx_arg::apply(SpMat<typename T1::pod_type>& out, const mtSpOp<typename T1::pod_type, T1, spop_cx_arg>& in)
{
arma_extra_debug_sigprint();
out.init_xform_mt(in.m, priv::functor_cx_arg());
}
namespace priv
{
struct functor_real
{
template<typename T>
arma_inline T operator()(const std::complex<T>& val) const { return val.real(); }
};
}
template<typename T1>
inline
void
spop_real::apply(SpMat<typename T1::pod_type>& out, const mtSpOp<typename T1::pod_type, T1, spop_real>& in)
{
arma_extra_debug_sigprint();
out.init_xform_mt(in.m, priv::functor_real());
}
namespace priv
{
struct functor_imag
{
template<typename T>
arma_inline T operator()(const std::complex<T>& val) const { return val.imag(); }
};
}
template<typename T1>
inline
void
spop_imag::apply(SpMat<typename T1::pod_type>& out, const mtSpOp<typename T1::pod_type, T1, spop_imag>& in)
{
arma_extra_debug_sigprint();
out.init_xform_mt(in.m, priv::functor_imag());
}
namespace priv
{
struct functor_conj
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return eop_aux::conj(val); }
};
}
template<typename T1>
inline
void
spop_conj::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_conj>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_conj());
}
template<typename T1>
inline
void
spop_repelem::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_repelem>& in)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const unwrap_spmat<T1> U(in.m);
const SpMat<eT>& X = U.M;
const uword copies_per_row = in.aux_uword_a;
const uword copies_per_col = in.aux_uword_b;
const uword out_n_rows = X.n_rows * copies_per_row;
const uword out_n_cols = X.n_cols * copies_per_col;
const uword out_nnz = X.n_nonzero * copies_per_row * copies_per_col;
if( (out_n_rows > 0) && (out_n_cols > 0) && (out_nnz > 0) )
{
umat locs(2, out_nnz, arma_nozeros_indicator());
Col<eT> vals( out_nnz, arma_nozeros_indicator());
uword* locs_mem = locs.memptr();
eT* vals_mem = vals.memptr();
typename SpMat<eT>::const_iterator X_it = X.begin();
typename SpMat<eT>::const_iterator X_end = X.end();
for(; X_it != X_end; ++X_it)
{
const uword col_base = copies_per_col * X_it.col();
const uword row_base = copies_per_row * X_it.row();
const eT X_val = (*X_it);
for(uword cols = 0; cols < copies_per_col; cols++)
for(uword rows = 0; rows < copies_per_row; rows++)
{
(*locs_mem) = row_base + rows; ++locs_mem;
(*locs_mem) = col_base + cols; ++locs_mem;
(*vals_mem) = X_val; ++vals_mem;
}
}
out = SpMat<eT>(locs, vals, out_n_rows, out_n_cols);
}
else
{
out.zeros(out_n_rows, out_n_cols);
}
}
template<typename T1>
inline
void
spop_reshape::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_reshape>& in)
{
arma_extra_debug_sigprint();
out = in.m;
out.reshape(in.aux_uword_a, in.aux_uword_b);
}
template<typename T1>
inline
void
spop_resize::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_resize>& in)
{
arma_extra_debug_sigprint();
out = in.m;
out.resize(in.aux_uword_a, in.aux_uword_b);
}
namespace priv
{
struct functor_floor
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return eop_aux::floor(val); }
};
}
template<typename T1>
inline
void
spop_floor::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_floor>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_floor());
}
namespace priv
{
struct functor_ceil
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return eop_aux::ceil(val); }
};
}
template<typename T1>
inline
void
spop_ceil::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_ceil>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_ceil());
}
namespace priv
{
struct functor_round
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return eop_aux::round(val); }
};
}
template<typename T1>
inline
void
spop_round::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_round>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_round());
}
namespace priv
{
struct functor_trunc
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return eop_aux::trunc(val); }
};
}
template<typename T1>
inline
void
spop_trunc::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_trunc>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_trunc());
}
namespace priv
{
struct functor_sign
{
template<typename eT>
arma_inline eT operator()(const eT val) const { return arma_sign(val); }
};
}
template<typename T1>
inline
void
spop_sign::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_sign>& in)
{
arma_extra_debug_sigprint();
out.init_xform(in.m, priv::functor_sign());
}
template<typename T1>
inline
void
spop_diagvec::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_diagvec>& in)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
const unwrap_spmat<T1> U(in.m);
const SpMat<eT>& X = U.M;
const uword a = in.aux_uword_a;
const uword b = in.aux_uword_b;
const uword row_offset = (b > 0) ? a : 0;
const uword col_offset = (b == 0) ? a : 0;
arma_debug_check_bounds
(
((row_offset > 0) && (row_offset >= X.n_rows)) || ((col_offset > 0) && (col_offset >= X.n_cols)),
"diagvec(): requested diagonal out of bounds"
);
const uword len = (std::min)(X.n_rows - row_offset, X.n_cols - col_offset);
Col<eT> cache(len, arma_nozeros_indicator());
eT* cache_mem = cache.memptr();
uword n_nonzero = 0;
for(uword i=0; i < len; ++i)
{
const eT val = X.at(i + row_offset, i + col_offset);
cache_mem[i] = val;
n_nonzero += (val != eT(0)) ? uword(1) : uword(0);
}
out.reserve(len, 1, n_nonzero);
uword count = 0;
for(uword i=0; i < len; ++i)
{
const eT val = cache_mem[i];
if(val != eT(0))
{
access::rw(out.row_indices[count]) = i;
access::rw(out.values[count]) = val;
++count;
}
}
access::rw(out.col_ptrs[0]) = 0;
access::rw(out.col_ptrs[1]) = n_nonzero;
}
template<typename T1>
inline
void
spop_flipud::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_flipud>& in)
{
arma_extra_debug_sigprint();
out = reverse(in.m, 0);
}
template<typename T1>
inline
void
spop_fliplr::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_fliplr>& in)
{
arma_extra_debug_sigprint();
out = reverse(in.m, 1);
}
//! @}
|