comparison armadillo-2.4.4/include/armadillo_bits/eop_core_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2010-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2010-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14 //! \addtogroup eop_core
15 //! @{
16
17
18 #undef arma_applier_1
19 #undef arma_applier_2
20 #undef arma_applier_3
21 #undef operatorA
22
23 #define arma_applier_1(operatorA) \
24 {\
25 uword i,j;\
26 \
27 for(i=0, j=1; j<n_elem; i+=2, j+=2)\
28 {\
29 eT tmp_i = P[i];\
30 eT tmp_j = P[j];\
31 \
32 tmp_i = eop_core<eop_type>::process(tmp_i, k);\
33 tmp_j = eop_core<eop_type>::process(tmp_j, k);\
34 \
35 out_mem[i] operatorA tmp_i;\
36 out_mem[j] operatorA tmp_j;\
37 }\
38 \
39 if(i < n_elem)\
40 {\
41 out_mem[i] operatorA eop_core<eop_type>::process(P[i], k);\
42 }\
43 }
44
45
46 #define arma_applier_2(operatorA) \
47 {\
48 uword count = 0;\
49 \
50 for(uword col=0; col<n_cols; ++col)\
51 {\
52 uword i,j;\
53 \
54 for(i=0, j=1; j<n_rows; i+=2, j+=2, count+=2)\
55 {\
56 eT tmp_i = P.at(i,col);\
57 eT tmp_j = P.at(j,col);\
58 \
59 tmp_i = eop_core<eop_type>::process(tmp_i, k);\
60 tmp_j = eop_core<eop_type>::process(tmp_j, k);\
61 \
62 out_mem[count ] operatorA tmp_i;\
63 out_mem[count+1] operatorA tmp_j;\
64 }\
65 \
66 if(i < n_rows)\
67 {\
68 out_mem[count] operatorA eop_core<eop_type>::process(P.at(i,col), k);\
69 ++count;\
70 }\
71 }\
72 }
73
74
75
76 #define arma_applier_3(operatorA) \
77 {\
78 uword count = 0;\
79 \
80 for(uword slice=0; slice<n_slices; ++slice)\
81 {\
82 for(uword col=0; col<n_cols; ++col)\
83 {\
84 uword i,j;\
85 \
86 for(i=0, j=1; j<n_rows; i+=2, j+=2, count+=2)\
87 {\
88 eT tmp_i = P.at(i,col,slice);\
89 eT tmp_j = P.at(j,col,slice);\
90 \
91 tmp_i = eop_core<eop_type>::process(tmp_i, k);\
92 tmp_j = eop_core<eop_type>::process(tmp_j, k);\
93 \
94 out_mem[count ] operatorA tmp_i;\
95 out_mem[count+1] operatorA tmp_j;\
96 }\
97 \
98 if(i < n_rows)\
99 {\
100 out_mem[count] operatorA eop_core<eop_type>::process(P.at(i,col,slice), k);\
101 ++count;\
102 }\
103 }\
104 }\
105 }
106
107
108
109 //
110 // matrices
111
112
113
114 template<typename eop_type>
115 template<typename T1>
116 arma_hot
117 inline
118 void
119 eop_core<eop_type>::apply(Mat<typename T1::elem_type>& out, const eOp<T1, eop_type>& x)
120 {
121 arma_extra_debug_sigprint();
122
123 typedef typename T1::elem_type eT;
124
125 const uword n_rows = out.n_rows;
126 const uword n_cols = out.n_cols;
127 const uword n_elem = out.n_elem;
128
129 // NOTE: we're assuming that the matrix has already been set to the correct size and there is no aliasing;
130 // size setting and alias checking is done by either the Mat contructor or operator=()
131
132 const eT k = x.aux;
133 eT* out_mem = out.memptr();
134
135 if(Proxy<T1>::prefer_at_accessor == false)
136 {
137 typename Proxy<T1>::ea_type P = x.P.get_ea();
138
139 arma_applier_1(=);
140 }
141 else
142 {
143 const Proxy<T1>& P = x.P;
144
145 arma_applier_2(=);
146 }
147 }
148
149
150
151 template<typename eop_type>
152 template<typename T1>
153 arma_hot
154 inline
155 void
156 eop_core<eop_type>::apply_inplace_plus(Mat<typename T1::elem_type>& out, const eOp<T1, eop_type>& x)
157 {
158 arma_extra_debug_sigprint();
159
160 typedef typename T1::elem_type eT;
161
162 const uword n_rows = x.get_n_rows();
163 const uword n_cols = x.get_n_cols();
164
165 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "addition");
166
167 eT* out_mem = out.memptr();
168 const uword n_elem = out.n_elem;
169
170 const eT k = x.aux;
171
172 if(Proxy<T1>::prefer_at_accessor == false)
173 {
174 typename Proxy<T1>::ea_type P = x.P.get_ea();
175
176 arma_applier_1(+=);
177 }
178 else
179 {
180 const Proxy<T1>& P = x.P;
181
182 arma_applier_2(+=);
183 }
184 }
185
186
187
188 template<typename eop_type>
189 template<typename T1>
190 arma_hot
191 inline
192 void
193 eop_core<eop_type>::apply_inplace_minus(Mat<typename T1::elem_type>& out, const eOp<T1, eop_type>& x)
194 {
195 arma_extra_debug_sigprint();
196
197 typedef typename T1::elem_type eT;
198
199 const uword n_rows = x.get_n_rows();
200 const uword n_cols = x.get_n_cols();
201
202 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "subtraction");
203
204 eT* out_mem = out.memptr();
205 const uword n_elem = out.n_elem;
206
207 const eT k = x.aux;
208
209 if(Proxy<T1>::prefer_at_accessor == false)
210 {
211 typename Proxy<T1>::ea_type P = x.P.get_ea();
212
213 arma_applier_1(-=);
214 }
215 else
216 {
217 const Proxy<T1>& P = x.P;
218
219 arma_applier_2(-=);
220 }
221 }
222
223
224
225 template<typename eop_type>
226 template<typename T1>
227 arma_hot
228 inline
229 void
230 eop_core<eop_type>::apply_inplace_schur(Mat<typename T1::elem_type>& out, const eOp<T1, eop_type>& x)
231 {
232 arma_extra_debug_sigprint();
233
234 typedef typename T1::elem_type eT;
235
236 const uword n_rows = x.get_n_rows();
237 const uword n_cols = x.get_n_cols();
238
239 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "element-wise multiplication");
240
241 eT* out_mem = out.memptr();
242 const uword n_elem = out.n_elem;
243
244 const eT k = x.aux;
245
246 if(Proxy<T1>::prefer_at_accessor == false)
247 {
248 typename Proxy<T1>::ea_type P = x.P.get_ea();
249
250 arma_applier_1(*=);
251 }
252 else
253 {
254 const Proxy<T1>& P = x.P;
255
256 arma_applier_2(*=);
257 }
258 }
259
260
261
262 template<typename eop_type>
263 template<typename T1>
264 arma_hot
265 inline
266 void
267 eop_core<eop_type>::apply_inplace_div(Mat<typename T1::elem_type>& out, const eOp<T1, eop_type>& x)
268 {
269 arma_extra_debug_sigprint();
270
271 typedef typename T1::elem_type eT;
272
273 const uword n_rows = x.get_n_rows();
274 const uword n_cols = x.get_n_cols();
275
276 arma_debug_assert_same_size(out.n_rows, out.n_cols, n_rows, n_cols, "element-wise division");
277
278 eT* out_mem = out.memptr();
279 const uword n_elem = out.n_elem;
280
281 const eT k = x.aux;
282
283 if(Proxy<T1>::prefer_at_accessor == false)
284 {
285 typename Proxy<T1>::ea_type P = x.P.get_ea();
286
287 arma_applier_1(/=);
288 }
289 else
290 {
291 const Proxy<T1>& P = x.P;
292
293 arma_applier_2(/=);
294 }
295 }
296
297
298
299 //
300 // cubes
301
302
303
304 template<typename eop_type>
305 template<typename T1>
306 arma_hot
307 inline
308 void
309 eop_core<eop_type>::apply(Cube<typename T1::elem_type>& out, const eOpCube<T1, eop_type>& x)
310 {
311 arma_extra_debug_sigprint();
312
313 typedef typename T1::elem_type eT;
314
315 const uword n_rows = out.n_rows;
316 const uword n_cols = out.n_cols;
317 const uword n_slices = out.n_slices;
318 const uword n_elem = out.n_elem;
319
320 // NOTE: we're assuming that the matrix has already been set to the correct size and there is no aliasing;
321 // size setting and alias checking is done by either the Mat contructor or operator=()
322
323 const eT k = x.aux;
324 eT* out_mem = out.memptr();
325
326 if(ProxyCube<T1>::prefer_at_accessor == false)
327 {
328 typename ProxyCube<T1>::ea_type P = x.P.get_ea();
329
330 arma_applier_1(=);
331 }
332 else
333 {
334 const ProxyCube<T1>& P = x.P;
335
336 arma_applier_3(=);
337 }
338 }
339
340
341
342 template<typename eop_type>
343 template<typename T1>
344 arma_hot
345 inline
346 void
347 eop_core<eop_type>::apply_inplace_plus(Cube<typename T1::elem_type>& out, const eOpCube<T1, eop_type>& x)
348 {
349 arma_extra_debug_sigprint();
350
351 typedef typename T1::elem_type eT;
352
353 const uword n_rows = x.get_n_rows();
354 const uword n_cols = x.get_n_cols();
355 const uword n_slices = x.get_n_slices();
356
357 arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "addition");
358
359 eT* out_mem = out.memptr();
360 const uword n_elem = out.n_elem;
361
362 const eT k = x.aux;
363
364 if(ProxyCube<T1>::prefer_at_accessor == false)
365 {
366 typename ProxyCube<T1>::ea_type P = x.P.get_ea();
367
368 arma_applier_1(+=);
369 }
370 else
371 {
372 const ProxyCube<T1>& P = x.P;
373
374 arma_applier_3(+=);
375 }
376 }
377
378
379
380 template<typename eop_type>
381 template<typename T1>
382 arma_hot
383 inline
384 void
385 eop_core<eop_type>::apply_inplace_minus(Cube<typename T1::elem_type>& out, const eOpCube<T1, eop_type>& x)
386 {
387 arma_extra_debug_sigprint();
388
389 typedef typename T1::elem_type eT;
390
391 const uword n_rows = x.get_n_rows();
392 const uword n_cols = x.get_n_cols();
393 const uword n_slices = x.get_n_slices();
394
395 arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "subtraction");
396
397 eT* out_mem = out.memptr();
398 const uword n_elem = out.n_elem;
399
400 const eT k = x.aux;
401
402 if(ProxyCube<T1>::prefer_at_accessor == false)
403 {
404 typename ProxyCube<T1>::ea_type P = x.P.get_ea();
405
406 arma_applier_1(-=);
407 }
408 else
409 {
410 const ProxyCube<T1>& P = x.P;
411
412 arma_applier_3(-=);
413 }
414 }
415
416
417
418 template<typename eop_type>
419 template<typename T1>
420 arma_hot
421 inline
422 void
423 eop_core<eop_type>::apply_inplace_schur(Cube<typename T1::elem_type>& out, const eOpCube<T1, eop_type>& x)
424 {
425 arma_extra_debug_sigprint();
426
427 typedef typename T1::elem_type eT;
428
429 const uword n_rows = x.get_n_rows();
430 const uword n_cols = x.get_n_cols();
431 const uword n_slices = x.get_n_slices();
432
433 arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise multiplication");
434
435 eT* out_mem = out.memptr();
436 const uword n_elem = out.n_elem;
437
438 const eT k = x.aux;
439
440 if(ProxyCube<T1>::prefer_at_accessor == false)
441 {
442 typename ProxyCube<T1>::ea_type P = x.P.get_ea();
443
444 arma_applier_1(*=);
445 }
446 else
447 {
448 const ProxyCube<T1>& P = x.P;
449
450 arma_applier_3(*=);
451 }
452 }
453
454
455
456 template<typename eop_type>
457 template<typename T1>
458 arma_hot
459 inline
460 void
461 eop_core<eop_type>::apply_inplace_div(Cube<typename T1::elem_type>& out, const eOpCube<T1, eop_type>& x)
462 {
463 arma_extra_debug_sigprint();
464
465 typedef typename T1::elem_type eT;
466
467 const uword n_rows = x.get_n_rows();
468 const uword n_cols = x.get_n_cols();
469 const uword n_slices = x.get_n_slices();
470
471 arma_debug_assert_same_size(out.n_rows, out.n_cols, out.n_slices, n_rows, n_cols, n_slices, "element-wise division");
472
473 eT* out_mem = out.memptr();
474 const uword n_elem = out.n_elem;
475
476 const eT k = x.aux;
477
478 if(ProxyCube<T1>::prefer_at_accessor == false)
479 {
480 typename ProxyCube<T1>::ea_type P = x.P.get_ea();
481
482 arma_applier_1(/=);
483 }
484 else
485 {
486 const ProxyCube<T1>& P = x.P;
487
488 arma_applier_3(/=);
489 }
490 }
491
492
493
494 //
495 // common
496
497
498
499 template<typename eop_type>
500 template<typename eT>
501 arma_hot
502 arma_pure
503 arma_inline
504 eT
505 eop_core<eop_type>::process(const eT val, const eT k)
506 {
507 arma_ignore(val);
508 arma_ignore(k);
509
510 arma_stop("eop_core::process(): unhandled eop_type");
511 return eT(0);
512 }
513
514
515
516 template<> template<typename eT> arma_hot arma_const arma_inline eT
517 eop_core<eop_scalar_plus >::process(const eT val, const eT k) { return val + k; }
518
519 template<> template<typename eT> arma_hot arma_const arma_inline eT
520 eop_core<eop_scalar_minus_pre >::process(const eT val, const eT k) { return k - val; }
521
522 template<> template<typename eT> arma_hot arma_const arma_inline eT
523 eop_core<eop_scalar_minus_post>::process(const eT val, const eT k) { return val - k; }
524
525 template<> template<typename eT> arma_hot arma_const arma_inline eT
526 eop_core<eop_scalar_times >::process(const eT val, const eT k) { return val * k; }
527
528 template<> template<typename eT> arma_hot arma_const arma_inline eT
529 eop_core<eop_scalar_div_pre >::process(const eT val, const eT k) { return k / val; }
530
531 template<> template<typename eT> arma_hot arma_const arma_inline eT
532 eop_core<eop_scalar_div_post >::process(const eT val, const eT k) { return val / k; }
533
534 template<> template<typename eT> arma_hot arma_const arma_inline eT
535 eop_core<eop_square >::process(const eT val, const eT ) { return val*val; }
536
537 template<> template<typename eT> arma_hot arma_const arma_inline eT
538 eop_core<eop_neg >::process(const eT val, const eT ) { return eop_aux::neg(val); }
539
540 template<> template<typename eT> arma_hot arma_pure arma_inline eT
541 eop_core<eop_sqrt >::process(const eT val, const eT ) { return eop_aux::sqrt(val); }
542
543 template<> template<typename eT> arma_hot arma_pure arma_inline eT
544 eop_core<eop_log >::process(const eT val, const eT ) { return eop_aux::log(val); }
545
546 template<> template<typename eT> arma_hot arma_pure arma_inline eT
547 eop_core<eop_log2 >::process(const eT val, const eT ) { return eop_aux::log2(val); }
548
549 template<> template<typename eT> arma_hot arma_pure arma_inline eT
550 eop_core<eop_log10 >::process(const eT val, const eT ) { return eop_aux::log10(val); }
551
552 template<> template<typename eT> arma_hot arma_pure arma_inline eT
553 eop_core<eop_trunc_log >::process(const eT val, const eT ) { return arma::trunc_log(val); }
554
555 template<> template<typename eT> arma_hot arma_pure arma_inline eT
556 eop_core<eop_exp >::process(const eT val, const eT ) { return eop_aux::exp(val); }
557
558 template<> template<typename eT> arma_hot arma_pure arma_inline eT
559 eop_core<eop_exp2 >::process(const eT val, const eT ) { return eop_aux::exp2(val); }
560
561 template<> template<typename eT> arma_hot arma_pure arma_inline eT
562 eop_core<eop_exp10 >::process(const eT val, const eT ) { return eop_aux::exp10(val); }
563
564 template<> template<typename eT> arma_hot arma_pure arma_inline eT
565 eop_core<eop_trunc_exp >::process(const eT val, const eT ) { return arma::trunc_exp(val); }
566
567 template<> template<typename eT> arma_hot arma_pure arma_inline eT
568 eop_core<eop_cos >::process(const eT val, const eT ) { return eop_aux::cos(val); }
569
570 template<> template<typename eT> arma_hot arma_pure arma_inline eT
571 eop_core<eop_sin >::process(const eT val, const eT ) { return eop_aux::sin(val); }
572
573 template<> template<typename eT> arma_hot arma_pure arma_inline eT
574 eop_core<eop_tan >::process(const eT val, const eT ) { return eop_aux::tan(val); }
575
576 template<> template<typename eT> arma_hot arma_pure arma_inline eT
577 eop_core<eop_acos >::process(const eT val, const eT ) { return eop_aux::acos(val); }
578
579 template<> template<typename eT> arma_hot arma_pure arma_inline eT
580 eop_core<eop_asin >::process(const eT val, const eT ) { return eop_aux::asin(val); }
581
582 template<> template<typename eT> arma_hot arma_pure arma_inline eT
583 eop_core<eop_atan >::process(const eT val, const eT ) { return eop_aux::atan(val); }
584
585 template<> template<typename eT> arma_hot arma_pure arma_inline eT
586 eop_core<eop_cosh >::process(const eT val, const eT ) { return eop_aux::cosh(val); }
587
588 template<> template<typename eT> arma_hot arma_pure arma_inline eT
589 eop_core<eop_sinh >::process(const eT val, const eT ) { return eop_aux::sinh(val); }
590
591 template<> template<typename eT> arma_hot arma_pure arma_inline eT
592 eop_core<eop_tanh >::process(const eT val, const eT ) { return eop_aux::tanh(val); }
593
594 template<> template<typename eT> arma_hot arma_pure arma_inline eT
595 eop_core<eop_acosh >::process(const eT val, const eT ) { return eop_aux::acosh(val); }
596
597 template<> template<typename eT> arma_hot arma_pure arma_inline eT
598 eop_core<eop_asinh >::process(const eT val, const eT ) { return eop_aux::asinh(val); }
599
600 template<> template<typename eT> arma_hot arma_pure arma_inline eT
601 eop_core<eop_atanh >::process(const eT val, const eT ) { return eop_aux::atanh(val); }
602
603 template<> template<typename eT> arma_hot arma_pure arma_inline eT
604 eop_core<eop_eps >::process(const eT val, const eT ) { return eop_aux::direct_eps(val); }
605
606 template<> template<typename eT> arma_hot arma_pure arma_inline eT
607 eop_core<eop_abs >::process(const eT val, const eT ) { return eop_aux::arma_abs(val); }
608
609 template<> template<typename eT> arma_hot arma_pure arma_inline eT
610 eop_core<eop_conj >::process(const eT val, const eT ) { return eop_aux::conj(val); }
611
612 template<> template<typename eT> arma_hot arma_pure arma_inline eT
613 eop_core<eop_pow >::process(const eT val, const eT k) { return eop_aux::pow(val, k); }
614
615 template<> template<typename eT> arma_hot arma_pure arma_inline eT
616 eop_core<eop_floor >::process(const eT val, const eT ) { return eop_aux::floor(val); }
617
618 template<> template<typename eT> arma_hot arma_pure arma_inline eT
619 eop_core<eop_ceil >::process(const eT val, const eT ) { return eop_aux::ceil(val); }
620
621
622
623 #undef arma_applier_1
624 #undef arma_applier_2
625 #undef arma_applier_3
626
627
628
629 //! @}