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