comparison armadillo-2.4.4/include/armadillo_bits/arrayops_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) 2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 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 arrayops
15 //! @{
16
17
18
19 template<typename eT>
20 arma_hot
21 arma_inline
22 void
23 arrayops::copy(eT* dest, const eT* src, const uword n_elem)
24 {
25 switch(n_elem)
26 {
27 default:
28 arrayops::copy_big(dest, src, n_elem);
29 break;
30 case 8:
31 dest[7] = src[7];
32 case 7:
33 dest[6] = src[6];
34 case 6:
35 dest[5] = src[5];
36 case 5:
37 dest[4] = src[4];
38 case 4:
39 dest[3] = src[3];
40 case 3:
41 dest[2] = src[2];
42 case 2:
43 dest[1] = src[1];
44 case 1:
45 dest[0] = src[0];
46 }
47 }
48
49
50
51 template<typename eT>
52 inline
53 void
54 arrayops::copy_big(eT* dest, const eT* src, const uword n_elem)
55 {
56 switch(n_elem)
57 {
58 default:
59 std::memcpy(dest, src, n_elem*sizeof(eT));
60 break;
61 case 32:
62 dest[31] = src[31];
63 case 31:
64 dest[30] = src[30];
65 case 30:
66 dest[29] = src[29];
67 case 29:
68 dest[28] = src[28];
69 case 28:
70 dest[27] = src[27];
71 case 27:
72 dest[26] = src[26];
73 case 26:
74 dest[25] = src[25];
75 case 25:
76 dest[24] = src[24];
77 case 24:
78 dest[23] = src[23];
79 case 23:
80 dest[22] = src[22];
81 case 22:
82 dest[21] = src[21];
83 case 21:
84 dest[20] = src[20];
85 case 20:
86 dest[19] = src[19];
87 case 19:
88 dest[18] = src[18];
89 case 18:
90 dest[17] = src[17];
91 case 17:
92 dest[16] = src[16];
93 case 16:
94 dest[15] = src[15];
95 case 15:
96 dest[14] = src[14];
97 case 14:
98 dest[13] = src[13];
99 case 13:
100 dest[12] = src[12];
101 case 12:
102 dest[11] = src[11];
103 case 11:
104 dest[10] = src[10];
105 case 10:
106 dest[9] = src[9];
107 case 9:
108 dest[8] = src[8];
109 case 8:
110 dest[7] = src[7];
111 case 7:
112 dest[6] = src[6];
113 case 6:
114 dest[5] = src[5];
115 case 5:
116 dest[4] = src[4];
117 case 4:
118 dest[3] = src[3];
119 case 3:
120 dest[2] = src[2];
121 case 2:
122 dest[1] = src[1];
123 case 1:
124 dest[0] = src[0];
125 }
126 }
127
128
129
130 template<typename out_eT, typename in_eT>
131 arma_hot
132 arma_inline
133 void
134 arrayops::convert_cx_scalar
135 (
136 out_eT& out,
137 const in_eT& in,
138 const typename arma_not_cx<out_eT>::result* junk1,
139 const typename arma_not_cx< in_eT>::result* junk2
140 )
141 {
142 arma_ignore(junk1);
143 arma_ignore(junk2);
144
145 out = out_eT(in);
146 }
147
148
149
150 template<typename out_eT, typename in_T>
151 arma_hot
152 arma_inline
153 void
154 arrayops::convert_cx_scalar
155 (
156 out_eT& out,
157 const std::complex<in_T>& in,
158 const typename arma_not_cx<out_eT>::result* junk
159 )
160 {
161 arma_ignore(junk);
162
163 out = out_eT( in.real() );
164 }
165
166
167
168 template<typename out_T, typename in_T>
169 arma_hot
170 arma_inline
171 void
172 arrayops::convert_cx_scalar
173 (
174 std::complex<out_T>& out,
175 const std::complex< in_T>& in
176 )
177 {
178 typedef std::complex<out_T> out_eT;
179
180 out = out_eT(in);
181 }
182
183
184
185 template<typename out_eT, typename in_eT>
186 arma_hot
187 inline
188 void
189 arrayops::convert(out_eT* dest, const in_eT* src, const uword n_elem)
190 {
191 uword i,j;
192
193 for(i=0, j=1; j<n_elem; i+=2, j+=2)
194 {
195 dest[i] = out_eT( src[i] );
196 dest[j] = out_eT( src[j] );
197 }
198
199 if(i < n_elem)
200 {
201 dest[i] = out_eT( src[i] );
202 }
203 }
204
205
206
207 template<typename out_eT, typename in_eT>
208 arma_hot
209 inline
210 void
211 arrayops::convert_cx(out_eT* dest, const in_eT* src, const uword n_elem)
212 {
213 uword i,j;
214
215 for(i=0, j=1; j<n_elem; i+=2, j+=2)
216 {
217 arrayops::convert_cx_scalar( dest[i], src[i] );
218 arrayops::convert_cx_scalar( dest[j], src[j] );
219 }
220
221 if(i < n_elem)
222 {
223 arrayops::convert_cx_scalar( dest[i], src[i] );
224 }
225 }
226
227
228
229 template<typename eT>
230 arma_hot
231 inline
232 void
233 arrayops::inplace_plus(eT* dest, const eT* src, const uword n_elem)
234 {
235 uword i,j;
236
237 for(i=0, j=1; j<n_elem; i+=2, j+=2)
238 {
239 dest[i] += src[i];
240 dest[j] += src[j];
241 }
242
243 if(i < n_elem)
244 {
245 dest[i] += src[i];
246 }
247 }
248
249
250
251 template<typename eT>
252 arma_hot
253 inline
254 void
255 arrayops::inplace_minus(eT* dest, const eT* src, const uword n_elem)
256 {
257 uword i,j;
258
259 for(i=0, j=1; j<n_elem; i+=2, j+=2)
260 {
261 dest[i] -= src[i];
262 dest[j] -= src[j];
263 }
264
265 if(i < n_elem)
266 {
267 dest[i] -= src[i];
268 }
269 }
270
271
272
273 template<typename eT>
274 arma_hot
275 inline
276 void
277 arrayops::inplace_mul(eT* dest, const eT* src, const uword n_elem)
278 {
279 uword i,j;
280
281 for(i=0, j=1; j<n_elem; i+=2, j+=2)
282 {
283 dest[i] *= src[i];
284 dest[j] *= src[j];
285 }
286
287 if(i < n_elem)
288 {
289 dest[i] *= src[i];
290 }
291 }
292
293
294
295 template<typename eT>
296 arma_hot
297 inline
298 void
299 arrayops::inplace_div(eT* dest, const eT* src, const uword n_elem)
300 {
301 uword i,j;
302
303 for(i=0, j=1; j<n_elem; i+=2, j+=2)
304 {
305 dest[i] /= src[i];
306 dest[j] /= src[j];
307 }
308
309 if(i < n_elem)
310 {
311 dest[i] /= src[i];
312 }
313 }
314
315
316
317 template<typename eT>
318 arma_hot
319 inline
320 void
321 arrayops::inplace_set(eT* dest, const eT val, const uword n_elem)
322 {
323 uword i,j;
324
325 for(i=0, j=1; j<n_elem; i+=2, j+=2)
326 {
327 dest[i] = val;
328 dest[j] = val;
329 }
330
331 if(i < n_elem)
332 {
333 dest[i] = val;
334 }
335 }
336
337
338
339 template<typename eT>
340 arma_hot
341 inline
342 void
343 arrayops::inplace_plus(eT* dest, const eT val, const uword n_elem)
344 {
345 uword i,j;
346
347 for(i=0, j=1; j<n_elem; i+=2, j+=2)
348 {
349 dest[i] += val;
350 dest[j] += val;
351 }
352
353 if(i < n_elem)
354 {
355 dest[i] += val;
356 }
357 }
358
359
360
361 template<typename eT>
362 arma_hot
363 inline
364 void
365 arrayops::inplace_minus(eT* dest, const eT val, const uword n_elem)
366 {
367 uword i,j;
368
369 for(i=0, j=1; j<n_elem; i+=2, j+=2)
370 {
371 dest[i] -= val;
372 dest[j] -= val;
373 }
374
375 if(i < n_elem)
376 {
377 dest[i] -= val;
378 }
379 }
380
381
382
383 template<typename eT>
384 arma_hot
385 inline
386 void
387 arrayops::inplace_mul(eT* dest, const eT val, const uword n_elem)
388 {
389 uword i,j;
390
391 for(i=0, j=1; j<n_elem; i+=2, j+=2)
392 {
393 dest[i] *= val;
394 dest[j] *= val;
395 }
396
397 if(i < n_elem)
398 {
399 dest[i] *= val;
400 }
401 }
402
403
404
405 template<typename eT>
406 arma_hot
407 inline
408 void
409 arrayops::inplace_div(eT* dest, const eT val, const uword n_elem)
410 {
411 uword i,j;
412
413 for(i=0, j=1; j<n_elem; i+=2, j+=2)
414 {
415 dest[i] /= val;
416 dest[j] /= val;
417 }
418
419 if(i < n_elem)
420 {
421 dest[i] /= val;
422 }
423 }
424
425
426
427 template<typename eT>
428 arma_hot
429 arma_pure
430 inline
431 eT
432 arrayops::accumulate(const eT* src, const uword n_elem)
433 {
434 uword i,j;
435
436 eT acc1 = eT(0);
437 eT acc2 = eT(0);
438
439 for(i=0, j=1; j<n_elem; i+=2, j+=2)
440 {
441 acc1 += src[i];
442 acc2 += src[j];
443 }
444
445 if(i < n_elem)
446 {
447 acc1 += src[i];
448 }
449
450 return acc1 + acc2;
451 }
452
453
454
455 template<typename eT>
456 arma_hot
457 arma_pure
458 inline
459 eT
460 arrayops::product(const eT* src, const uword n_elem)
461 {
462 eT val1 = eT(1);
463 eT val2 = eT(1);
464
465 uword i,j;
466
467 for(i=0, j=1; j<n_elem; i+=2, j+=2)
468 {
469 val1 *= src[i];
470 val2 *= src[j];
471 }
472
473 if(i < n_elem)
474 {
475 val1 *= src[i];
476 }
477
478 return val1 * val2;
479 }
480
481
482
483 template<typename eT>
484 arma_hot
485 arma_pure
486 inline
487 bool
488 arrayops::is_finite(const eT* src, const uword n_elem)
489 {
490 uword i,j;
491
492 for(i=0, j=1; j<n_elem; i+=2, j+=2)
493 {
494 const eT val_i = src[i];
495 const eT val_j = src[j];
496
497 if( (arma_isfinite(val_i) == false) || (arma_isfinite(val_j) == false) )
498 {
499 return false;
500 }
501 }
502
503 if(i < n_elem)
504 {
505 if(arma_isfinite(src[i]) == false)
506 {
507 return false;
508 }
509 }
510
511 return true;
512 }
513
514
515
516 // TODO: this function is currently not used
517 template<typename eT>
518 arma_hot
519 arma_pure
520 inline
521 typename get_pod_type<eT>::result
522 arrayops::norm_1(const eT* src, const uword n_elem)
523 {
524 typedef typename get_pod_type<eT>::result T;
525
526 T acc = T(0);
527
528 uword i,j;
529
530 for(i=0, j=1; j<n_elem; i+=2, j+=2)
531 {
532 acc += std::abs(src[i]);
533 acc += std::abs(src[j]);
534 }
535
536 if(i < n_elem)
537 {
538 acc += std::abs(src[i]);
539 }
540
541 return acc;
542 }
543
544
545
546 // TODO: this function is currently not used
547 template<typename eT>
548 arma_hot
549 arma_pure
550 inline
551 eT
552 arrayops::norm_2(const eT* src, const uword n_elem, const typename arma_not_cx<eT>::result* junk)
553 {
554 arma_ignore(junk);
555
556 eT acc = eT(0);
557
558 uword i,j;
559
560 for(i=0, j=1; j<n_elem; i+=2, j+=2)
561 {
562 const eT tmp_i = src[i];
563 const eT tmp_j = src[j];
564
565 acc += tmp_i * tmp_i;
566 acc += tmp_j * tmp_j;
567 }
568
569 if(i < n_elem)
570 {
571 const eT tmp_i = src[i];
572
573 acc += tmp_i * tmp_i;
574 }
575
576 return std::sqrt(acc);
577 }
578
579
580
581 // TODO: this function is currently not used
582 template<typename T>
583 arma_hot
584 arma_pure
585 inline
586 T
587 arrayops::norm_2(const std::complex<T>* src, const uword n_elem)
588 {
589 T acc = T(0);
590
591 uword i,j;
592
593 for(i=0, j=1; j<n_elem; i+=2, j+=2)
594 {
595 const T tmp_i = std::abs(src[i]);
596 const T tmp_j = std::abs(src[j]);
597
598 acc += tmp_i * tmp_i;
599 acc += tmp_j * tmp_j;
600 }
601
602 if(i < n_elem)
603 {
604 const T tmp_i = std::abs(src[i]);
605
606 acc += tmp_i * tmp_i;
607 }
608
609 return std::sqrt(acc);
610 }
611
612
613
614 // TODO: this function is currently not used
615 template<typename eT>
616 arma_hot
617 arma_pure
618 inline
619 typename get_pod_type<eT>::result
620 arrayops::norm_k(const eT* src, const uword n_elem, const int k)
621 {
622 typedef typename get_pod_type<eT>::result T;
623
624 T acc = T(0);
625
626 uword i,j;
627
628 for(i=0, j=1; j<n_elem; i+=2, j+=2)
629 {
630 acc += std::pow(std::abs(src[i]), k);
631 acc += std::pow(std::abs(src[j]), k);
632 }
633
634 if(i < n_elem)
635 {
636 acc += std::pow(std::abs(src[i]), k);
637 }
638
639 return std::pow(acc, T(1)/T(k));
640 }
641
642
643
644 // TODO: this function is currently not used
645 template<typename eT>
646 arma_hot
647 arma_pure
648 inline
649 typename get_pod_type<eT>::result
650 arrayops::norm_max(const eT* src, const uword n_elem)
651 {
652 typedef typename get_pod_type<eT>::result T;
653
654 T max_val = std::abs(src[0]);
655
656 uword i,j;
657
658 for(i=1, j=2; j<n_elem; i+=2, j+=2)
659 {
660 const T tmp_i = std::abs(src[i]);
661 const T tmp_j = std::abs(src[j]);
662
663 if(max_val < tmp_i) { max_val = tmp_i; }
664 if(max_val < tmp_j) { max_val = tmp_j; }
665 }
666
667 if(i < n_elem)
668 {
669 const T tmp_i = std::abs(src[i]);
670
671 if(max_val < tmp_i) { max_val = tmp_i; }
672 }
673
674 return max_val;
675 }
676
677
678
679 // TODO: this function is currently not used
680 template<typename eT>
681 arma_hot
682 arma_pure
683 inline
684 typename get_pod_type<eT>::result
685 arrayops::norm_min(const eT* src, const uword n_elem)
686 {
687 typedef typename get_pod_type<eT>::result T;
688
689 T min_val = std::abs(src[0]);
690
691 uword i,j;
692
693 for(i=1, j=2; j<n_elem; i+=2, j+=2)
694 {
695 const T tmp_i = std::abs(src[i]);
696 const T tmp_j = std::abs(src[j]);
697
698 if(min_val > tmp_i) { min_val = tmp_i; }
699 if(min_val > tmp_j) { min_val = tmp_j; }
700 }
701
702 if(i < n_elem)
703 {
704 const T tmp_i = std::abs(src[i]);
705
706 if(min_val > tmp_i) { min_val = tmp_i; }
707 }
708
709 return min_val;
710 }
711
712
713
714 //! @}