/usr/include/gmm/gmm_def.h is in libgmm-dev 4.0.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 | // -*- c++ -*- (enables emacs c++ mode)
//===========================================================================
//
// Copyright (C) 2002-2008 Yves Renard
//
// This file is a part of GETFEM++
//
// Getfem++ is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// As a special exception, you may use this file as it is a part of a free
// software library without restriction. Specifically, if other files
// instantiate templates or use macros or inline functions from this file,
// or you compile this file and link it with other files to produce an
// executable, this file does not by itself cause the resulting executable
// to be covered by the GNU Lesser General Public License. This exception
// does not however invalidate any other reasons why the executable file
// might be covered by the GNU Lesser General Public License.
//
//===========================================================================
/**@file gmm_def.h
@author Yves Renard <Yves.Renard@insa-lyon.fr>
@date October 13, 2002.
@brief Basic definitions and tools of GMM.
*/
#ifndef GMM_DEF_H__
#define GMM_DEF_H__
#include "gmm_ref.h"
#include <complex>
#ifndef M_PI
# define M_E 2.7182818284590452354 /* e */
# define M_LOG2E 1.4426950408889634074 /* 1/ln(2) */
# define M_LOG10E 0.43429448190325182765 /* 1/ln(10) */
# define M_LN2 0.69314718055994530942 /* ln(2) */
# define M_LN10 2.30258509299404568402 /* ln(10) */
# define M_PI 3.14159265358979323846 /* pi */
# define M_PI_2 1.57079632679489661923 /* pi/2 */
# define M_PI_4 0.78539816339744830962 /* pi/4 */
# define M_1_PI 0.31830988618379067154 /* 1/pi */
# define M_2_PI 0.63661977236758134308 /* 2/pi */
# define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
# define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
# define M_SQRT1_2 0.70710678118654752440 /* sqrt(2)/2 */
#endif
#ifndef M_PIl
# define M_PIl 3.1415926535897932384626433832795029L /* pi */
# define M_PI_2l 1.5707963267948966192313216916397514L /* pi/2 */
# define M_PI_4l 0.7853981633974483096156608458198757L /* pi/4 */
# define M_1_PIl 0.3183098861837906715377675267450287L /* 1/pi */
# define M_2_PIl 0.6366197723675813430755350534900574L /* 2/pi */
# define M_2_SQRTPIl 1.1283791670955125738961589031215452L /* 2/sqrt(pi) */
#endif
namespace gmm {
typedef size_t size_type;
/* ******************************************************************** */
/* Specifier types */
/* ******************************************************************** */
/* not perfectly null, required by aCC 3.33 */
struct abstract_null_type {
abstract_null_type(int=0) {}
template <typename A,typename B,typename C> void operator()(A,B,C) {}
}; // specify an information lake.
struct linalg_true {};
struct linalg_false {};
template <typename V, typename W> struct linalg_and
{ typedef linalg_false bool_type; };
template <> struct linalg_and<linalg_true, linalg_true>
{ typedef linalg_true bool_type; };
template <typename V, typename W> struct linalg_or
{ typedef linalg_true bool_type; };
template <> struct linalg_and<linalg_false, linalg_false>
{ typedef linalg_false bool_type; };
struct linalg_const {}; // A reference is either linalg_const,
struct linalg_modifiable {}; // linalg_modifiable or linalg_false.
struct abstract_vector {}; // The object is a vector
struct abstract_matrix {}; // The object is a matrix
struct abstract_sparse {}; // sparse matrix or vector
struct abstract_skyline {}; // 'sky-line' matrix or vector
struct abstract_dense {}; // dense matrix or vector
struct abstract_indirect {}; // matrix given by the product with a vector
struct row_major {}; // matrix with a row access.
struct col_major {}; // matrix with a column access
struct row_and_col {}; // both accesses but row preference
struct col_and_row {}; // both accesses but column preference
template <typename T> struct transposed_type;
template<> struct transposed_type<row_major> {typedef col_major t_type;};
template<> struct transposed_type<col_major> {typedef row_major t_type;};
template<> struct transposed_type<row_and_col> {typedef col_and_row t_type;};
template<> struct transposed_type<col_and_row> {typedef row_and_col t_type;};
template <typename T> struct principal_orientation_type
{ typedef abstract_null_type potype; };
template<> struct principal_orientation_type<row_major>
{ typedef row_major potype; };
template<> struct principal_orientation_type<col_major>
{ typedef col_major potype; };
template<> struct principal_orientation_type<row_and_col>
{ typedef row_major potype; };
template<> struct principal_orientation_type<col_and_row>
{ typedef col_major potype; };
// template <typename V> struct linalg_traits;
template <typename V> struct linalg_traits {
typedef abstract_null_type this_type;
typedef abstract_null_type linalg_type;
typedef abstract_null_type value_type;
typedef abstract_null_type is_reference;
typedef abstract_null_type& reference;
typedef abstract_null_type* iterator;
typedef const abstract_null_type* const_iterator;
typedef abstract_null_type index_sorted;
typedef abstract_null_type storage_type;
typedef abstract_null_type origin_type;
typedef abstract_null_type const_sub_row_type;
typedef abstract_null_type sub_row_type;
typedef abstract_null_type const_row_iterator;
typedef abstract_null_type row_iterator;
typedef abstract_null_type const_sub_col_type;
typedef abstract_null_type sub_col_type;
typedef abstract_null_type const_col_iterator;
typedef abstract_null_type col_iterator;
typedef abstract_null_type sub_orientation;
};
template <typename PT, typename V> struct vect_ref_type;
template <typename P, typename V> struct vect_ref_type<P *, V> {
typedef typename linalg_traits<V>::reference access_type;
typedef typename linalg_traits<V>::iterator iterator;
};
template <typename P, typename V> struct vect_ref_type<const P *, V> {
typedef typename linalg_traits<V>::value_type access_type;
typedef typename linalg_traits<V>::const_iterator iterator;
};
template <typename PT> struct const_pointer;
template <typename P> struct const_pointer<P *>
{ typedef const P* pointer; };
template <typename P> struct const_pointer<const P *>
{ typedef const P* pointer; };
template <typename PT> struct modifiable_pointer;
template <typename P> struct modifiable_pointer<P *>
{ typedef P* pointer; };
template <typename P> struct modifiable_pointer<const P *>
{ typedef P* pointer; };
template <typename R> struct const_reference;
template <typename R> struct const_reference<R &>
{ typedef const R &reference; };
template <typename R> struct const_reference<const R &>
{ typedef const R &reference; };
inline bool is_sparse(abstract_sparse) { return true; }
inline bool is_sparse(abstract_dense) { return false; }
inline bool is_sparse(abstract_skyline) { return true; }
inline bool is_sparse(abstract_indirect) { return false; }
template <typename L> inline bool is_sparse(const L &)
{ return is_sparse(typename linalg_traits<L>::storage_type()); }
inline bool is_row_matrix_(row_major) { return true; }
inline bool is_row_matrix_(col_major) { return false; }
inline bool is_row_matrix_(row_and_col) { return true; }
inline bool is_row_matrix_(col_and_row) { return true; }
template <typename L> inline bool is_row_matrix(const L &)
{ return is_row_matrix_(typename linalg_traits<L>::sub_orientation()); }
inline bool is_col_matrix_(row_major) { return false; }
inline bool is_col_matrix_(col_major) { return true; }
inline bool is_col_matrix_(row_and_col) { return true; }
inline bool is_col_matrix_(col_and_row) { return true; }
template <typename L> inline bool is_col_matrix(const L &)
{ return is_col_matrix_(typename linalg_traits<L>::sub_orientation()); }
inline bool is_col_matrix(row_major) { return false; }
inline bool is_col_matrix(col_major) { return true; }
inline bool is_row_matrix(row_major) { return true; }
inline bool is_row_matrix(col_major) { return false; }
template <typename L> inline bool is_const_reference(L) { return false; }
inline bool is_const_reference(linalg_const) { return true; }
template <typename T> struct is_gmm_interfaced_ {
typedef linalg_true result;
};
template<> struct is_gmm_interfaced_<abstract_null_type> {
typedef linalg_false result;
};
template <typename T> struct is_gmm_interfaced {
typedef typename is_gmm_interfaced_<typename gmm::linalg_traits<T>::this_type >::result result;
};
/* ******************************************************************** */
/* types to deal with const object representing a modifiable reference */
/* ******************************************************************** */
template <typename PT, typename R> struct mref_type_
{ typedef abstract_null_type return_type; };
template <typename L, typename R> struct mref_type_<L *, R>
{ typedef L & return_type; };
template <typename L, typename R> struct mref_type_<const L *, R>
{ typedef const L & return_type; };
template <typename L> struct mref_type_<L *, linalg_const>
{ typedef const L & return_type; };
template <typename L> struct mref_type_<const L *, linalg_const>
{ typedef const L & return_type; };
template <typename L> struct mref_type_<const L *, linalg_modifiable>
{ typedef L & return_type; };
template <typename L> struct mref_type_<L *, linalg_modifiable>
{ typedef L & return_type; };
template <typename PT> struct mref_type {
typedef typename std::iterator_traits<PT>::value_type L;
typedef typename mref_type_<PT,
typename linalg_traits<L>::is_reference>::return_type return_type;
};
template <typename L> typename mref_type<const L *>::return_type
linalg_cast(const L &l)
{ return const_cast<typename mref_type<const L *>::return_type>(l); }
template <typename L> typename mref_type<L *>::return_type linalg_cast(L &l)
{ return const_cast<typename mref_type<L *>::return_type>(l); }
template <typename L, typename R> struct cref_type_
{ typedef abstract_null_type return_type; };
template <typename L> struct cref_type_<L, linalg_modifiable>
{ typedef L & return_type; };
template <typename L> struct cref_type {
typedef typename cref_type_<L,
typename linalg_traits<L>::is_reference>::return_type return_type;
};
template <typename L> typename cref_type<L>::return_type
linalg_const_cast(const L &l)
{ return const_cast<typename cref_type<L>::return_type>(l); }
// To be used to select between a reference or a const refercence for
// the return type of a function
// select_return<C1, C2, L *> return C1 if L is a const reference,
// C2 otherwise.
// select_return<C1, C2, const L *> return C2 if L is a modifiable reference
// C1 otherwise.
template <typename C1, typename C2, typename REF> struct select_return_ {
typedef abstract_null_type return_type;
};
template <typename C1, typename C2, typename L>
struct select_return_<C1, C2, const L &> { typedef C1 return_type; };
template <typename C1, typename C2, typename L>
struct select_return_<C1, C2, L &> { typedef C2 return_type; };
template <typename C1, typename C2, typename PT> struct select_return {
typedef typename std::iterator_traits<PT>::value_type L;
typedef typename select_return_<C1, C2,
typename mref_type<PT>::return_type>::return_type return_type;
};
// To be used to select between a reference or a const refercence inside
// a structure or a linagl_traits
// select_ref<C1, C2, L *> return C1 if L is a const reference,
// C2 otherwise.
// select_ref<C1, C2, const L *> return C2 in any case.
template <typename C1, typename C2, typename REF> struct select_ref_
{ typedef abstract_null_type ref_type; };
template <typename C1, typename C2, typename L>
struct select_ref_<C1, C2, const L &> { typedef C1 ref_type; };
template <typename C1, typename C2, typename L>
struct select_ref_<C1, C2, L &> { typedef C2 ref_type; };
template <typename C1, typename C2, typename PT> struct select_ref {
typedef typename std::iterator_traits<PT>::value_type L;
typedef typename select_ref_<C1, C2,
typename mref_type<PT>::return_type>::ref_type ref_type;
};
template <typename C1, typename C2, typename L>
struct select_ref<C1, C2, const L *>
{ typedef C1 ref_type; };
template<typename R> struct is_a_reference_
{ typedef linalg_true reference; };
template<> struct is_a_reference_<linalg_false>
{ typedef linalg_false reference; };
template<typename L> struct is_a_reference {
typedef typename is_a_reference_<typename linalg_traits<L>::is_reference>
::reference reference;
};
template <typename L> inline bool is_original_linalg(const L &)
{ return is_original_linalg(typename is_a_reference<L>::reference()); }
inline bool is_original_linalg(linalg_false) { return true; }
inline bool is_original_linalg(linalg_true) { return false; }
template <typename PT> struct which_reference
{ typedef abstract_null_type is_reference; };
template <typename PT> struct which_reference<PT *>
{ typedef linalg_modifiable is_reference; };
template <typename PT> struct which_reference<const PT *>
{ typedef linalg_const is_reference; };
template <typename C1, typename C2, typename R> struct select_orientation_
{ typedef abstract_null_type return_type; };
template <typename C1, typename C2>
struct select_orientation_<C1, C2, row_major>
{ typedef C1 return_type; };
template <typename C1, typename C2>
struct select_orientation_<C1, C2, col_major>
{ typedef C2 return_type; };
template <typename C1, typename C2, typename L> struct select_orientation {
typedef typename select_orientation_<C1, C2,
typename principal_orientation_type<typename
linalg_traits<L>::sub_orientation>::potype>::return_type return_type;
};
/* ******************************************************************** */
/* Operations on scalars */
/* ******************************************************************** */
template <typename T> inline T sqr(T a) { return T(a * a); }
template <typename T> inline T abs(T a) { return (a < T(0)) ? T(-a) : a; }
template <typename T> inline T abs(std::complex<T> a)
{ T x = a.real(), y = a.imag(); return T(::sqrt(x*x+y*y)); }
template <typename T> inline T abs_sqr(T a) { return T(a*a); }
template <typename T> inline T abs_sqr(std::complex<T> a)
{ return gmm::sqr(a.real()) + gmm::sqr(a.imag()); }
template <typename T> inline T pos(T a) { return (a < T(0)) ? T(0) : a; }
template <typename T> inline T neg(T a) { return (a < T(0)) ? T(-a) : T(0); }
template <typename T> inline T sgn(T a) { return (a < T(0)) ? T(-1) : T(1); }
inline double random() { return double(rand())/(RAND_MAX+0.5); }
template <typename T> inline T random(T)
{ return T(rand()*2.0)/(T(RAND_MAX)+T(1)/T(2)) - T(1); }
template <typename T> inline std::complex<T> random(std::complex<T>)
{ return std::complex<T>(gmm::random(T()), gmm::random(T())); }
template <typename T> inline T irandom(T max)
{ return T(gmm::random() * double(max)); }
template <typename T> inline T conj(T a) { return a; }
template <typename T> inline std::complex<T> conj(std::complex<T> a)
{ return std::conj(a); }
template <typename T> inline T real(T a) { return a; }
template <typename T> inline T real(std::complex<T> a) { return a.real(); }
template <typename T> inline T imag(T ) { return T(0); }
template <typename T> inline T imag(std::complex<T> a) { return a.imag(); }
template <typename T> inline T sqrt(T a) { return T(::sqrt(a)); }
template <typename T> inline std::complex<T> sqrt(std::complex<T> a) {
T x = a.real(), y = a.imag();
if (x == T(0)) {
T t = T(::sqrt(gmm::abs(y) / T(2)));
return std::complex<T>(t, y < T(0) ? -t : t);
}
T t = T(::sqrt(T(2) * (gmm::abs(a) + gmm::abs(x)))), u = t / T(2);
return x > T(0) ? std::complex<T>(u, y / t)
: std::complex<T>(gmm::abs(y) / t, y < T(0) ? -u : u);
}
using std::swap;
template <typename T> struct number_traits {
typedef T magnitude_type;
};
template <typename T> struct number_traits<std::complex<T> > {
typedef T magnitude_type;
};
template <typename T> inline T conj_product(T a, T b) { return a * b; }
template <typename T> inline
std::complex<T> conj_product(std::complex<T> a, std::complex<T> b)
{ return std::conj(a) * b; } // to be optimized ?
template <typename T> inline bool is_complex(T) { return false; }
template <typename T> inline bool is_complex(std::complex<T> )
{ return true; }
# define magnitude_of_linalg(M) typename number_traits<typename \
linalg_traits<M>::value_type>::magnitude_type
template<typename T> inline std::complex<T> operator*(const std::complex<T>& a, int b) {
return a*T(b);
}
template<typename T> inline std::complex<T> operator*(int b, const std::complex<T>& a) {
return a*T(b);
}
/* ******************************************************************** */
/* types promotion */
/* ******************************************************************** */
/* should be completed for more specific cases <unsigned int, float> etc */
template <typename T1, typename T2, bool c>
struct strongest_numeric_type_aux {
typedef T1 T;
};
template <typename T1, typename T2>
struct strongest_numeric_type_aux<T1,T2,false> {
typedef T2 T;
};
template <typename T1, typename T2>
struct strongest_numeric_type {
typedef typename
strongest_numeric_type_aux<T1,T2,(sizeof(T1)>sizeof(T2))>::T T;
};
template <typename T1, typename T2>
struct strongest_numeric_type<T1,std::complex<T2> > {
typedef typename number_traits<T1>::magnitude_type R1;
typedef std::complex<typename strongest_numeric_type<R1,T2>::T > T;
};
template <typename T1, typename T2>
struct strongest_numeric_type<std::complex<T1>,T2 > {
typedef typename number_traits<T2>::magnitude_type R2;
typedef std::complex<typename strongest_numeric_type<T1,R2>::T > T;
};
template <typename T1, typename T2>
struct strongest_numeric_type<std::complex<T1>,std::complex<T2> > {
typedef std::complex<typename strongest_numeric_type<T1,T2>::T > T;
};
template<> struct strongest_numeric_type<int,float> { typedef float T; };
template<> struct strongest_numeric_type<float,int> { typedef float T; };
template<> struct strongest_numeric_type<long,float> { typedef float T; };
template<> struct strongest_numeric_type<float,long> { typedef float T; };
template<> struct strongest_numeric_type<long,double> { typedef double T; };
template<> struct strongest_numeric_type<double,long> { typedef double T; };
template <typename V1, typename V2>
struct strongest_value_type {
typedef typename
strongest_numeric_type<typename linalg_traits<V1>::value_type,
typename linalg_traits<V2>::value_type>::T
value_type;
};
template <typename V1, typename V2, typename V3>
struct strongest_value_type3 {
typedef typename
strongest_value_type<V1, typename
strongest_value_type<V2,V3>::value_type>::value_type
value_type;
};
/* ******************************************************************** */
/* Basic vectors used */
/* ******************************************************************** */
template<typename T> struct dense_vector_type
{ typedef std::vector<T> vector_type; };
template <typename T> class wsvector;
template <typename T> class rsvector;
template<typename T> struct sparse_vector_type
{ typedef wsvector<T> vector_type; };
template <typename T> class slvector;
template <typename T> class dense_matrix;
template <typename VECT> class row_matrix;
template <typename VECT> class col_matrix;
/* ******************************************************************** */
/* Selects a temporary vector type */
/* V if V is a valid vector type, */
/* wsvector if V is a reference on a sparse vector, */
/* std::vector if V is a reference on a dense vector. */
/* ******************************************************************** */
template <typename R, typename S, typename L, typename V>
struct temporary_vector_ {
typedef abstract_null_type vector_type;
};
template <typename V, typename L>
struct temporary_vector_<linalg_true, abstract_sparse, L, V>
{ typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V, typename L>
struct temporary_vector_<linalg_true, abstract_skyline, L, V>
{ typedef slvector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V, typename L>
struct temporary_vector_<linalg_true, abstract_dense, L, V>
{ typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
template <typename S, typename V>
struct temporary_vector_<linalg_false, S, abstract_vector, V>
{ typedef V vector_type; };
template <typename V>
struct temporary_vector_<linalg_false, abstract_dense, abstract_matrix, V>
{ typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V>
struct temporary_vector_<linalg_false, abstract_sparse, abstract_matrix, V>
{ typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V> struct temporary_vector {
typedef typename temporary_vector_<typename is_a_reference<V>::reference,
typename linalg_traits<V>::storage_type,
typename linalg_traits<V>::linalg_type,
V>::vector_type vector_type;
};
/* ******************************************************************** */
/* Selects a temporary matrix type */
/* M if M is a valid matrix type, */
/* row_matrix<wsvector> if M is a reference on a sparse matrix, */
/* dense_matrix if M is a reference on a dense matrix. */
/* ******************************************************************** */
template <typename R, typename S, typename L, typename V>
struct temporary_matrix_ { typedef abstract_null_type matrix_type; };
template <typename V, typename L>
struct temporary_matrix_<linalg_true, abstract_sparse, L, V> {
typedef typename linalg_traits<V>::value_type T;
typedef row_matrix<wsvector<T> > matrix_type;
};
template <typename V, typename L>
struct temporary_matrix_<linalg_true, abstract_skyline, L, V> {
typedef typename linalg_traits<V>::value_type T;
typedef row_matrix<slvector<T> > matrix_type;
};
template <typename V, typename L>
struct temporary_matrix_<linalg_true, abstract_dense, L, V>
{ typedef dense_matrix<typename linalg_traits<V>::value_type> matrix_type; };
template <typename S, typename V>
struct temporary_matrix_<linalg_false, S, abstract_matrix, V>
{ typedef V matrix_type; };
template <typename V> struct temporary_matrix {
typedef typename temporary_matrix_<typename is_a_reference<V>::reference,
typename linalg_traits<V>::storage_type,
typename linalg_traits<V>::linalg_type,
V>::matrix_type matrix_type;
};
template <typename S, typename L, typename V>
struct temporary_col_matrix_ { typedef abstract_null_type matrix_type; };
template <typename V, typename L>
struct temporary_col_matrix_<abstract_sparse, L, V> {
typedef typename linalg_traits<V>::value_type T;
typedef col_matrix<wsvector<T> > matrix_type;
};
template <typename V, typename L>
struct temporary_col_matrix_<abstract_skyline, L, V> {
typedef typename linalg_traits<V>::value_type T;
typedef col_matrix<slvector<T> > matrix_type;
};
template <typename V, typename L>
struct temporary_col_matrix_<abstract_dense, L, V>
{ typedef dense_matrix<typename linalg_traits<V>::value_type> matrix_type; };
template <typename V> struct temporary_col_matrix {
typedef typename temporary_col_matrix_<
typename linalg_traits<V>::storage_type,
typename linalg_traits<V>::linalg_type,
V>::matrix_type matrix_type;
};
template <typename S, typename L, typename V>
struct temporary_row_matrix_ { typedef abstract_null_type matrix_type; };
template <typename V, typename L>
struct temporary_row_matrix_<abstract_sparse, L, V> {
typedef typename linalg_traits<V>::value_type T;
typedef row_matrix<wsvector<T> > matrix_type;
};
template <typename V, typename L>
struct temporary_row_matrix_<abstract_skyline, L, V> {
typedef typename linalg_traits<V>::value_type T;
typedef row_matrix<slvector<T> > matrix_type;
};
template <typename V, typename L>
struct temporary_row_matrix_<abstract_dense, L, V>
{ typedef dense_matrix<typename linalg_traits<V>::value_type> matrix_type; };
template <typename V> struct temporary_row_matrix {
typedef typename temporary_row_matrix_<
typename linalg_traits<V>::storage_type,
typename linalg_traits<V>::linalg_type,
V>::matrix_type matrix_type;
};
/* ******************************************************************** */
/* Selects a temporary dense vector type */
/* V if V is a valid dense vector type, */
/* std::vector if V is a reference or another type of vector */
/* ******************************************************************** */
template <typename R, typename S, typename V>
struct temporary_dense_vector_ { typedef abstract_null_type vector_type; };
template <typename S, typename V>
struct temporary_dense_vector_<linalg_true, S, V>
{ typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V>
struct temporary_dense_vector_<linalg_false, abstract_sparse, V>
{ typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V>
struct temporary_dense_vector_<linalg_false, abstract_skyline, V>
{ typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V>
struct temporary_dense_vector_<linalg_false, abstract_dense, V>
{ typedef V vector_type; };
template <typename V> struct temporary_dense_vector {
typedef typename temporary_dense_vector_<typename
is_a_reference<V>::reference,
typename linalg_traits<V>::storage_type, V>::vector_type vector_type;
};
/* ******************************************************************** */
/* Selects a temporary sparse vector type */
/* V if V is a valid sparse vector type, */
/* wsvector if V is a reference or another type of vector */
/* ******************************************************************** */
template <typename R, typename S, typename V>
struct temporary_sparse_vector_ { typedef abstract_null_type vector_type; };
template <typename S, typename V>
struct temporary_sparse_vector_<linalg_true, S, V>
{ typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V>
struct temporary_sparse_vector_<linalg_false, abstract_sparse, V>
{ typedef V vector_type; };
template <typename V>
struct temporary_sparse_vector_<linalg_false, abstract_dense, V>
{ typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V>
struct temporary_sparse_vector_<linalg_false, abstract_skyline, V>
{ typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V> struct temporary_sparse_vector {
typedef typename temporary_sparse_vector_<typename
is_a_reference<V>::reference,
typename linalg_traits<V>::storage_type, V>::vector_type vector_type;
};
/* ******************************************************************** */
/* Selects a temporary sky-line vector type */
/* V if V is a valid sky-line vector type, */
/* slvector if V is a reference or another type of vector */
/* ******************************************************************** */
template <typename R, typename S, typename V>
struct temporary_skyline_vector_
{ typedef abstract_null_type vector_type; };
template <typename S, typename V>
struct temporary_skyline_vector_<linalg_true, S, V>
{ typedef slvector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V>
struct temporary_skyline_vector_<linalg_false, abstract_skyline, V>
{ typedef V vector_type; };
template <typename V>
struct temporary_skyline_vector_<linalg_false, abstract_dense, V>
{ typedef slvector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V>
struct temporary_skyline_vector_<linalg_false, abstract_sparse, V>
{ typedef slvector<typename linalg_traits<V>::value_type> vector_type; };
template <typename V> struct temporary_skylines_vector {
typedef typename temporary_skyline_vector_<typename
is_a_reference<V>::reference,
typename linalg_traits<V>::storage_type, V>::vector_type vector_type;
};
/* ********************************************************************* */
/* Definition & Comparison of origins. */
/* ********************************************************************* */
template <typename L>
typename select_return<const typename linalg_traits<L>::origin_type *,
typename linalg_traits<L>::origin_type *,
L *>::return_type
linalg_origin(L &l)
{ return linalg_traits<L>::origin(linalg_cast(l)); }
template <typename L>
typename select_return<const typename linalg_traits<L>::origin_type *,
typename linalg_traits<L>::origin_type *,
const L *>::return_type
linalg_origin(const L &l)
{ return linalg_traits<L>::origin(linalg_cast(l)); }
template <typename PT1, typename PT2>
bool same_porigin(PT1, PT2) { return false; }
template <typename PT>
bool same_porigin(PT pt1, PT pt2) { return (pt1 == pt2); }
template <typename L1, typename L2>
bool same_origin(const L1 &l1, const L2 &l2)
{ return same_porigin(linalg_origin(l1), linalg_origin(l2)); }
/* ******************************************************************** */
/* Miscellaneous */
/* ******************************************************************** */
template <typename V> inline size_type vect_size(const V &v)
{ return linalg_traits<V>::size(v); }
template <typename MAT> inline size_type mat_nrows(const MAT &m)
{ return linalg_traits<MAT>::nrows(m); }
template <typename MAT> inline size_type mat_ncols(const MAT &m)
{ return linalg_traits<MAT>::ncols(m); }
template <typename V> inline
typename select_return<typename linalg_traits<V>::const_iterator,
typename linalg_traits<V>::iterator, V *>::return_type
vect_begin(V &v)
{ return linalg_traits<V>::begin(linalg_cast(v)); }
template <typename V> inline
typename select_return<typename linalg_traits<V>::const_iterator,
typename linalg_traits<V>::iterator, const V *>::return_type
vect_begin(const V &v)
{ return linalg_traits<V>::begin(linalg_cast(v)); }
template <typename V> inline
typename linalg_traits<V>::const_iterator
vect_const_begin(const V &v)
{ return linalg_traits<V>::begin(v); }
template <typename V> inline
typename select_return<typename linalg_traits<V>::const_iterator,
typename linalg_traits<V>::iterator, V *>::return_type
vect_end(V &v)
{ return linalg_traits<V>::end(linalg_cast(v)); }
template <typename V> inline
typename select_return<typename linalg_traits<V>::const_iterator,
typename linalg_traits<V>::iterator, const V *>::return_type
vect_end(const V &v)
{ return linalg_traits<V>::end(linalg_cast(v)); }
template <typename V> inline
typename linalg_traits<V>::const_iterator
vect_const_end(const V &v)
{ return linalg_traits<V>::end(v); }
template <typename M> inline
typename select_return<typename linalg_traits<M>::const_row_iterator,
typename linalg_traits<M>::row_iterator, M *>::return_type
mat_row_begin(M &m) { return linalg_traits<M>::row_begin(linalg_cast(m)); }
template <typename M> inline
typename select_return<typename linalg_traits<M>::const_row_iterator,
typename linalg_traits<M>::row_iterator, const M *>::return_type
mat_row_begin(const M &m)
{ return linalg_traits<M>::row_begin(linalg_cast(m)); }
template <typename M> inline typename linalg_traits<M>::const_row_iterator
mat_row_const_begin(const M &m)
{ return linalg_traits<M>::row_begin(m); }
template <typename M> inline
typename select_return<typename linalg_traits<M>::const_row_iterator,
typename linalg_traits<M>::row_iterator, M *>::return_type
mat_row_end(M &v) {
return linalg_traits<M>::row_end(linalg_cast(v));
}
template <typename M> inline
typename select_return<typename linalg_traits<M>::const_row_iterator,
typename linalg_traits<M>::row_iterator, const M *>::return_type
mat_row_end(const M &v) {
return linalg_traits<M>::row_end(linalg_cast(v));
}
template <typename M> inline
typename linalg_traits<M>::const_row_iterator
mat_row_const_end(const M &v)
{ return linalg_traits<M>::row_end(v); }
template <typename M> inline
typename select_return<typename linalg_traits<M>::const_col_iterator,
typename linalg_traits<M>::col_iterator, M *>::return_type
mat_col_begin(M &v) {
return linalg_traits<M>::col_begin(linalg_cast(v));
}
template <typename M> inline
typename select_return<typename linalg_traits<M>::const_col_iterator,
typename linalg_traits<M>::col_iterator, const M *>::return_type
mat_col_begin(const M &v) {
return linalg_traits<M>::col_begin(linalg_cast(v));
}
template <typename M> inline
typename linalg_traits<M>::const_col_iterator
mat_col_const_begin(const M &v)
{ return linalg_traits<M>::col_begin(v); }
template <typename M> inline
typename linalg_traits<M>::const_col_iterator
mat_col_const_end(const M &v)
{ return linalg_traits<M>::col_end(v); }
template <typename M> inline
typename select_return<typename linalg_traits<M>::const_col_iterator,
typename linalg_traits<M>::col_iterator,
M *>::return_type
mat_col_end(M &m)
{ return linalg_traits<M>::col_end(linalg_cast(m)); }
template <typename M> inline
typename select_return<typename linalg_traits<M>::const_col_iterator,
typename linalg_traits<M>::col_iterator,
const M *>::return_type
mat_col_end(const M &m)
{ return linalg_traits<M>::col_end(linalg_cast(m)); }
template <typename MAT> inline
typename select_return<typename linalg_traits<MAT>::const_sub_row_type,
typename linalg_traits<MAT>::sub_row_type,
const MAT *>::return_type
mat_row(const MAT &m, size_type i)
{ return linalg_traits<MAT>::row(mat_row_begin(m) + i); }
template <typename MAT> inline
typename select_return<typename linalg_traits<MAT>::const_sub_row_type,
typename linalg_traits<MAT>::sub_row_type,
MAT *>::return_type
mat_row(MAT &m, size_type i)
{ return linalg_traits<MAT>::row(mat_row_begin(m) + i); }
template <typename MAT> inline
typename linalg_traits<MAT>::const_sub_row_type
mat_const_row(const MAT &m, size_type i)
{ return linalg_traits<MAT>::row(mat_row_const_begin(m) + i); }
template <typename MAT> inline
typename select_return<typename linalg_traits<MAT>::const_sub_col_type,
typename linalg_traits<MAT>::sub_col_type,
const MAT *>::return_type
mat_col(const MAT &m, size_type i)
{ return linalg_traits<MAT>::col(mat_col_begin(m) + i); }
template <typename MAT> inline
typename select_return<typename linalg_traits<MAT>::const_sub_col_type,
typename linalg_traits<MAT>::sub_col_type,
MAT *>::return_type
mat_col(MAT &m, size_type i)
{ return linalg_traits<MAT>::col(mat_col_begin(m) + i); }
template <typename MAT> inline
typename linalg_traits<MAT>::const_sub_col_type
mat_const_col(const MAT &m, size_type i)
{ return linalg_traits<MAT>::col(mat_col_const_begin(m) + i); }
/* ********************************************************************* */
/* Set to begin end set to end for iterators on non-const sparse vectors.*/
/* ********************************************************************* */
template <typename IT, typename ORG, typename VECT> inline
void set_to_begin(IT &it, ORG o, VECT *, linalg_false)
{ it = vect_begin(*o); }
template <typename IT, typename ORG, typename VECT> inline
void set_to_begin(IT &it, ORG o, const VECT *, linalg_false)
{ it = vect_const_begin(*o); }
template <typename IT, typename ORG, typename VECT> inline
void set_to_end(IT &it, ORG o, VECT *, linalg_false)
{ it = vect_end(*o); }
template <typename IT, typename ORG, typename VECT> inline
void set_to_end(IT &it, ORG o, const VECT *, linalg_false)
{ it = vect_const_end(*o); }
template <typename IT, typename ORG, typename VECT> inline
void set_to_begin(IT &, ORG, VECT *, linalg_const) { }
template <typename IT, typename ORG, typename VECT> inline
void set_to_begin(IT &, ORG, const VECT *, linalg_const) { }
template <typename IT, typename ORG, typename VECT> inline
void set_to_end(IT &, ORG, VECT *, linalg_const) { }
template <typename IT, typename ORG, typename VECT> inline
void set_to_end(IT &, ORG, const VECT *, linalg_const) { }
template <typename IT, typename ORG, typename VECT> inline
void set_to_begin(IT &, ORG, VECT *v, linalg_modifiable)
{ GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; }
template <typename IT, typename ORG, typename VECT> inline
void set_to_begin(IT &, ORG, const VECT *v, linalg_modifiable)
{ GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; }
template <typename IT, typename ORG, typename VECT> inline
void set_to_end(IT &, ORG, VECT *v, linalg_modifiable)
{ GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; }
template <typename IT, typename ORG, typename VECT> inline
void set_to_end(IT &, ORG, const VECT *v, linalg_modifiable)
{ GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; }
/* ******************************************************************** */
/* General index for certain algorithms. */
/* ******************************************************************** */
template<class IT>
size_type index_of_it(const IT &it, size_type, abstract_sparse)
{ return it.index(); }
template<class IT>
size_type index_of_it(const IT &it, size_type, abstract_skyline)
{ return it.index(); }
template<class IT>
size_type index_of_it(const IT &, size_type k, abstract_dense)
{ return k; }
/* ********************************************************************* */
/* Numeric limits. */
/* ********************************************************************* */
template<typename T> inline T default_tol(T) {
using namespace std;
static T tol(10);
if (tol == T(10)) {
if (numeric_limits<T>::is_specialized)
tol = numeric_limits<T>::epsilon();
else {
int i=sizeof(T)/4; while(i-- > 0) tol*=T(1E-8);
GMM_WARNING1("The numeric type " << typeid(T).name()
<< " has no numeric_limits defined !!\n"
<< "Taking " << tol << " as default tolerance");
}
}
return tol;
}
template<typename T> inline T default_tol(std::complex<T>)
{ return default_tol(T()); }
template<typename T> inline T default_min(T) {
using namespace std;
static T mi(10);
if (mi == T(10)) {
if (numeric_limits<T>::is_specialized)
mi = std::numeric_limits<T>::min();
else {
mi = T(0);
GMM_WARNING1("The numeric type " << typeid(T).name()
<< " has no numeric_limits defined !!\n"
<< "Taking 0 as default minimum");
}
}
return mi;
}
template<typename T> inline T default_min(std::complex<T>)
{ return default_min(T()); }
template<typename T> inline T default_max(T) {
using namespace std;
static T mi(10);
if (mi == T(10)) {
if (numeric_limits<T>::is_specialized)
mi = std::numeric_limits<T>::max();
else {
mi = T(1);
GMM_WARNING1("The numeric type " << typeid(T).name()
<< " has no numeric_limits defined !!\n"
<< "Taking 1 as default maximum !");
}
}
return mi;
}
template<typename T> inline T default_max(std::complex<T>)
{ return default_max(T()); }
/*
use safe_divide to avoid NaNs when dividing very small complex
numbers, for example
std::complex<float>(1e-23,1e-30)/std::complex<float>(1e-23,1e-30)
*/
template<typename T> inline T safe_divide(T a, T b) { return a/b; }
template<typename T> inline std::complex<T>
safe_divide(std::complex<T> a, std::complex<T> b) {
T m = std::max(gmm::abs(b.real()), gmm::abs(b.imag()));
a = std::complex<T>(a.real()/m, a.imag()/m);
b = std::complex<T>(b.real()/m, b.imag()/m);
return a / b;
}
/* ******************************************************************** */
/* Write */
/* ******************************************************************** */
template <typename T> struct cast_char_type { typedef T return_type; };
template <> struct cast_char_type<signed char> { typedef int return_type; };
template <> struct cast_char_type<unsigned char>
{ typedef unsigned int return_type; };
template <typename T> inline typename cast_char_type<T>::return_type
cast_char(const T &c) { return typename cast_char_type<T>::return_type(c); }
template <typename L> inline void write(std::ostream &o, const L &l)
{ write(o, l, typename linalg_traits<L>::linalg_type()); }
template <typename L> void write(std::ostream &o, const L &l,
abstract_vector) {
o << "vector(" << vect_size(l) << ") [";
write(o, l, typename linalg_traits<L>::storage_type());
o << " ]";
}
template <typename L> void write(std::ostream &o, const L &l,
abstract_sparse) {
typename linalg_traits<L>::const_iterator it = vect_const_begin(l),
ite = vect_const_end(l);
for (; it != ite; ++it)
o << " (r" << it.index() << "," << cast_char(*it) << ")";
}
template <typename L> void write(std::ostream &o, const L &l,
abstract_dense) {
typename linalg_traits<L>::const_iterator it = vect_const_begin(l),
ite = vect_const_end(l);
if (it != ite) o << " " << cast_char(*it++);
for (; it != ite; ++it) o << ", " << cast_char(*it);
}
template <typename L> void write(std::ostream &o, const L &l,
abstract_skyline) {
typedef typename linalg_traits<L>::const_iterator const_iterator;
const_iterator it = vect_const_begin(l), ite = vect_const_end(l);
if (it != ite) {
o << "<r+" << it.index() << ">";
if (it != ite) o << " " << cast_char(*it++);
for (; it != ite; ++it) { o << ", " << cast_char(*it); }
}
}
template <typename L> inline void write(std::ostream &o, const L &l,
abstract_matrix) {
write(o, l, typename linalg_traits<L>::sub_orientation());
}
template <typename L> void write(std::ostream &o, const L &l,
row_major) {
o << "matrix(" << mat_nrows(l) << ", " << mat_ncols(l) << ")" << endl;
for (size_type i = 0; i < mat_nrows(l); ++i) {
o << "(";
write(o, mat_const_row(l, i), typename linalg_traits<L>::storage_type());
o << " )\n";
}
}
template <typename L> inline
void write(std::ostream &o, const L &l, row_and_col)
{ write(o, l, row_major()); }
template <typename L> inline
void write(std::ostream &o, const L &l, col_and_row)
{ write(o, l, row_major()); }
template <typename L> void write(std::ostream &o, const L &l, col_major) {
o << "matrix(" << mat_nrows(l) << ", " << mat_ncols(l) << ")" << endl;
for (size_type i = 0; i < mat_nrows(l); ++i) {
o << "(";
if (is_sparse(l)) { // not optimized ...
for (size_type j = 0; j < mat_ncols(l); ++j)
if (l(i,j) != typename linalg_traits<L>::value_type(0))
o << " (r" << j << ", " << l(i,j) << ")";
}
else {
if (mat_ncols(l) != 0) o << ' ' << l(i, 0);
for (size_type j = 1; j < mat_ncols(l); ++j) o << ", " << l(i, j);
}
o << " )\n";
}
}
}
#endif // GMM_DEF_H__
|