/* -*- c++ -*- (enables emacs c++ mode) */ /*=========================================================================== Copyright (C) 2002-2012 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 3 of the License, or (at your option) any later version along with the GCC Runtime Library Exception either version 3.1 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 and GCC Runtime Library Exception 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 @date October 13, 2002. @brief Basic definitions and tools of GMM. */ #ifndef GMM_DEF_H__ #define GMM_DEF_H__ #include "gmm_ref.h" #include #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 void operator()(A,B,C) {} }; // specify an information lake. struct linalg_true {}; struct linalg_false {}; template struct linalg_and { typedef linalg_false bool_type; }; template <> struct linalg_and { typedef linalg_true bool_type; }; template struct linalg_or { typedef linalg_true bool_type; }; template <> struct linalg_and { 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 struct transposed_type; template<> struct transposed_type {typedef col_major t_type;}; template<> struct transposed_type {typedef row_major t_type;}; template<> struct transposed_type {typedef col_and_row t_type;}; template<> struct transposed_type {typedef row_and_col t_type;}; template struct principal_orientation_type { typedef abstract_null_type potype; }; template<> struct principal_orientation_type { typedef row_major potype; }; template<> struct principal_orientation_type { typedef col_major potype; }; template<> struct principal_orientation_type { typedef row_major potype; }; template<> struct principal_orientation_type { typedef col_major potype; }; // template struct linalg_traits; template 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 struct vect_ref_type; template struct vect_ref_type

{ typedef typename linalg_traits::reference access_type; typedef typename linalg_traits::iterator iterator; }; template struct vect_ref_type { typedef typename linalg_traits::value_type access_type; typedef typename linalg_traits::const_iterator iterator; }; template struct const_pointer; template struct const_pointer

{ typedef const P* pointer; }; template struct const_pointer { typedef const P* pointer; }; template struct modifiable_pointer; template struct modifiable_pointer

{ typedef P* pointer; }; template struct modifiable_pointer { typedef P* pointer; }; template struct const_reference; template struct const_reference { typedef const R &reference; }; template struct const_reference { 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 inline bool is_sparse(const L &) { return is_sparse(typename linalg_traits::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 inline bool is_row_matrix(const L &) { return is_row_matrix_(typename linalg_traits::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 inline bool is_col_matrix(const L &) { return is_col_matrix_(typename linalg_traits::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 inline bool is_const_reference(L) { return false; } inline bool is_const_reference(linalg_const) { return true; } template struct is_gmm_interfaced_ { typedef linalg_true result; }; template<> struct is_gmm_interfaced_ { typedef linalg_false result; }; template struct is_gmm_interfaced { typedef typename is_gmm_interfaced_::this_type >::result result; }; /* ******************************************************************** */ /* types to deal with const object representing a modifiable reference */ /* ******************************************************************** */ template struct mref_type_ { typedef abstract_null_type return_type; }; template struct mref_type_ { typedef L & return_type; }; template struct mref_type_ { typedef const L & return_type; }; template struct mref_type_ { typedef const L & return_type; }; template struct mref_type_ { typedef const L & return_type; }; template struct mref_type_ { typedef L & return_type; }; template struct mref_type_ { typedef L & return_type; }; template struct mref_type { typedef typename std::iterator_traits::value_type L; typedef typename mref_type_::is_reference>::return_type return_type; }; template typename mref_type::return_type linalg_cast(const L &l) { return const_cast::return_type>(l); } template typename mref_type::return_type linalg_cast(L &l) { return const_cast::return_type>(l); } template struct cref_type_ { typedef abstract_null_type return_type; }; template struct cref_type_ { typedef L & return_type; }; template struct cref_type { typedef typename cref_type_::is_reference>::return_type return_type; }; template typename cref_type::return_type linalg_const_cast(const L &l) { return const_cast::return_type>(l); } // To be used to select between a reference or a const refercence for // the return type of a function // select_return return C1 if L is a const reference, // C2 otherwise. // select_return return C2 if L is a modifiable reference // C1 otherwise. template struct select_return_ { typedef abstract_null_type return_type; }; template struct select_return_ { typedef C1 return_type; }; template struct select_return_ { typedef C2 return_type; }; template struct select_return { typedef typename std::iterator_traits::value_type L; typedef typename select_return_::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 return C1 if L is a const reference, // C2 otherwise. // select_ref return C2 in any case. template struct select_ref_ { typedef abstract_null_type ref_type; }; template struct select_ref_ { typedef C1 ref_type; }; template struct select_ref_ { typedef C2 ref_type; }; template struct select_ref { typedef typename std::iterator_traits::value_type L; typedef typename select_ref_::return_type>::ref_type ref_type; }; template struct select_ref { typedef C1 ref_type; }; template struct is_a_reference_ { typedef linalg_true reference; }; template<> struct is_a_reference_ { typedef linalg_false reference; }; template struct is_a_reference { typedef typename is_a_reference_::is_reference> ::reference reference; }; template inline bool is_original_linalg(const L &) { return is_original_linalg(typename is_a_reference::reference()); } inline bool is_original_linalg(linalg_false) { return true; } inline bool is_original_linalg(linalg_true) { return false; } template struct which_reference { typedef abstract_null_type is_reference; }; template struct which_reference { typedef linalg_modifiable is_reference; }; template struct which_reference { typedef linalg_const is_reference; }; template struct select_orientation_ { typedef abstract_null_type return_type; }; template struct select_orientation_ { typedef C1 return_type; }; template struct select_orientation_ { typedef C2 return_type; }; template struct select_orientation { typedef typename select_orientation_::sub_orientation>::potype>::return_type return_type; }; /* ******************************************************************** */ /* Operations on scalars */ /* ******************************************************************** */ template inline T sqr(T a) { return T(a * a); } template inline T abs(T a) { return (a < T(0)) ? T(-a) : a; } template inline T abs(std::complex a) { T x = a.real(), y = a.imag(); return T(::sqrt(x*x+y*y)); } template inline T abs_sqr(T a) { return T(a*a); } template inline T abs_sqr(std::complex a) { return gmm::sqr(a.real()) + gmm::sqr(a.imag()); } template inline T pos(T a) { return (a < T(0)) ? T(0) : a; } template inline T neg(T a) { return (a < T(0)) ? T(-a) : T(0); } template inline T sgn(T a) { return (a < T(0)) ? T(-1) : T(1); } template inline T Heaviside(T a) { return (a < T(0)) ? T(0) : T(1); } inline double random() { return double(rand())/(RAND_MAX+0.5); } template inline T random(T) { return T(rand()*2.0)/(T(RAND_MAX)+T(1)/T(2)) - T(1); } template inline std::complex random(std::complex) { return std::complex(gmm::random(T()), gmm::random(T())); } template inline T irandom(T max) { return T(gmm::random() * double(max)); } template inline T conj(T a) { return a; } template inline std::complex conj(std::complex a) { return std::conj(a); } template inline T real(T a) { return a; } template inline T real(std::complex a) { return a.real(); } template inline T imag(T ) { return T(0); } template inline T imag(std::complex a) { return a.imag(); } template inline T sqrt(T a) { return T(::sqrt(a)); } template inline std::complex sqrt(std::complex 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, 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(u, y / t) : std::complex(gmm::abs(y) / t, y < T(0) ? -u : u); } using std::swap; template struct number_traits { typedef T magnitude_type; }; template struct number_traits > { typedef T magnitude_type; }; template inline T conj_product(T a, T b) { return a * b; } template inline std::complex conj_product(std::complex a, std::complex b) { return std::conj(a) * b; } // to be optimized ? template inline bool is_complex(T) { return false; } template inline bool is_complex(std::complex ) { return true; } # define magnitude_of_linalg(M) typename number_traits::value_type>::magnitude_type template inline std::complex operator*(const std::complex& a, int b) { return a*T(b); } template inline std::complex operator*(int b, const std::complex& a) { return a*T(b); } /* ******************************************************************** */ /* types promotion */ /* ******************************************************************** */ /* should be completed for more specific cases etc */ template struct strongest_numeric_type_aux { typedef T1 T; }; template struct strongest_numeric_type_aux { typedef T2 T; }; template struct strongest_numeric_type { typedef typename strongest_numeric_type_auxsizeof(T2))>::T T; }; template struct strongest_numeric_type > { typedef typename number_traits::magnitude_type R1; typedef std::complex::T > T; }; template struct strongest_numeric_type,T2 > { typedef typename number_traits::magnitude_type R2; typedef std::complex::T > T; }; template struct strongest_numeric_type,std::complex > { typedef std::complex::T > T; }; template<> struct strongest_numeric_type { typedef float T; }; template<> struct strongest_numeric_type { typedef float T; }; template<> struct strongest_numeric_type { typedef float T; }; template<> struct strongest_numeric_type { typedef float T; }; template<> struct strongest_numeric_type { typedef double T; }; template<> struct strongest_numeric_type { typedef double T; }; template struct strongest_value_type { typedef typename strongest_numeric_type::value_type, typename linalg_traits::value_type>::T value_type; }; template struct strongest_value_type3 { typedef typename strongest_value_type::value_type>::value_type value_type; }; /* ******************************************************************** */ /* Basic vectors used */ /* ******************************************************************** */ template struct dense_vector_type { typedef std::vector vector_type; }; template class wsvector; template class rsvector; template struct sparse_vector_type { typedef wsvector vector_type; }; template class slvector; template class dense_matrix; template class row_matrix; template 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 struct temporary_vector_ { typedef abstract_null_type vector_type; }; template struct temporary_vector_ { typedef wsvector::value_type> vector_type; }; template struct temporary_vector_ { typedef slvector::value_type> vector_type; }; template struct temporary_vector_ { typedef std::vector::value_type> vector_type; }; template struct temporary_vector_ { typedef V vector_type; }; template struct temporary_vector_ { typedef std::vector::value_type> vector_type; }; template struct temporary_vector_ { typedef wsvector::value_type> vector_type; }; template struct temporary_vector { typedef typename temporary_vector_::reference, typename linalg_traits::storage_type, typename linalg_traits::linalg_type, V>::vector_type vector_type; }; /* ******************************************************************** */ /* Selects a temporary matrix type */ /* M if M is a valid matrix type, */ /* row_matrix if M is a reference on a sparse matrix, */ /* dense_matrix if M is a reference on a dense matrix. */ /* ******************************************************************** */ template struct temporary_matrix_ { typedef abstract_null_type matrix_type; }; template struct temporary_matrix_ { typedef typename linalg_traits::value_type T; typedef row_matrix > matrix_type; }; template struct temporary_matrix_ { typedef typename linalg_traits::value_type T; typedef row_matrix > matrix_type; }; template struct temporary_matrix_ { typedef dense_matrix::value_type> matrix_type; }; template struct temporary_matrix_ { typedef V matrix_type; }; template struct temporary_matrix { typedef typename temporary_matrix_::reference, typename linalg_traits::storage_type, typename linalg_traits::linalg_type, V>::matrix_type matrix_type; }; template struct temporary_col_matrix_ { typedef abstract_null_type matrix_type; }; template struct temporary_col_matrix_ { typedef typename linalg_traits::value_type T; typedef col_matrix > matrix_type; }; template struct temporary_col_matrix_ { typedef typename linalg_traits::value_type T; typedef col_matrix > matrix_type; }; template struct temporary_col_matrix_ { typedef dense_matrix::value_type> matrix_type; }; template struct temporary_col_matrix { typedef typename temporary_col_matrix_< typename linalg_traits::storage_type, typename linalg_traits::linalg_type, V>::matrix_type matrix_type; }; template struct temporary_row_matrix_ { typedef abstract_null_type matrix_type; }; template struct temporary_row_matrix_ { typedef typename linalg_traits::value_type T; typedef row_matrix > matrix_type; }; template struct temporary_row_matrix_ { typedef typename linalg_traits::value_type T; typedef row_matrix > matrix_type; }; template struct temporary_row_matrix_ { typedef dense_matrix::value_type> matrix_type; }; template struct temporary_row_matrix { typedef typename temporary_row_matrix_< typename linalg_traits::storage_type, typename linalg_traits::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 struct temporary_dense_vector_ { typedef abstract_null_type vector_type; }; template struct temporary_dense_vector_ { typedef std::vector::value_type> vector_type; }; template struct temporary_dense_vector_ { typedef std::vector::value_type> vector_type; }; template struct temporary_dense_vector_ { typedef std::vector::value_type> vector_type; }; template struct temporary_dense_vector_ { typedef V vector_type; }; template struct temporary_dense_vector { typedef typename temporary_dense_vector_::reference, typename linalg_traits::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 struct temporary_sparse_vector_ { typedef abstract_null_type vector_type; }; template struct temporary_sparse_vector_ { typedef wsvector::value_type> vector_type; }; template struct temporary_sparse_vector_ { typedef V vector_type; }; template struct temporary_sparse_vector_ { typedef wsvector::value_type> vector_type; }; template struct temporary_sparse_vector_ { typedef wsvector::value_type> vector_type; }; template struct temporary_sparse_vector { typedef typename temporary_sparse_vector_::reference, typename linalg_traits::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 struct temporary_skyline_vector_ { typedef abstract_null_type vector_type; }; template struct temporary_skyline_vector_ { typedef slvector::value_type> vector_type; }; template struct temporary_skyline_vector_ { typedef V vector_type; }; template struct temporary_skyline_vector_ { typedef slvector::value_type> vector_type; }; template struct temporary_skyline_vector_ { typedef slvector::value_type> vector_type; }; template struct temporary_skylines_vector { typedef typename temporary_skyline_vector_::reference, typename linalg_traits::storage_type, V>::vector_type vector_type; }; /* ********************************************************************* */ /* Definition & Comparison of origins. */ /* ********************************************************************* */ template typename select_return::origin_type *, typename linalg_traits::origin_type *, L *>::return_type linalg_origin(L &l) { return linalg_traits::origin(linalg_cast(l)); } template typename select_return::origin_type *, typename linalg_traits::origin_type *, const L *>::return_type linalg_origin(const L &l) { return linalg_traits::origin(linalg_cast(l)); } template bool same_porigin(PT1, PT2) { return false; } template bool same_porigin(PT pt1, PT pt2) { return (pt1 == pt2); } template bool same_origin(const L1 &l1, const L2 &l2) { return same_porigin(linalg_origin(l1), linalg_origin(l2)); } /* ******************************************************************** */ /* Miscellaneous */ /* ******************************************************************** */ template inline size_type vect_size(const V &v) { return linalg_traits::size(v); } template inline size_type mat_nrows(const MAT &m) { return linalg_traits::nrows(m); } template inline size_type mat_ncols(const MAT &m) { return linalg_traits::ncols(m); } template inline typename select_return::const_iterator, typename linalg_traits::iterator, V *>::return_type vect_begin(V &v) { return linalg_traits::begin(linalg_cast(v)); } template inline typename select_return::const_iterator, typename linalg_traits::iterator, const V *>::return_type vect_begin(const V &v) { return linalg_traits::begin(linalg_cast(v)); } template inline typename linalg_traits::const_iterator vect_const_begin(const V &v) { return linalg_traits::begin(v); } template inline typename select_return::const_iterator, typename linalg_traits::iterator, V *>::return_type vect_end(V &v) { return linalg_traits::end(linalg_cast(v)); } template inline typename select_return::const_iterator, typename linalg_traits::iterator, const V *>::return_type vect_end(const V &v) { return linalg_traits::end(linalg_cast(v)); } template inline typename linalg_traits::const_iterator vect_const_end(const V &v) { return linalg_traits::end(v); } template inline typename select_return::const_row_iterator, typename linalg_traits::row_iterator, M *>::return_type mat_row_begin(M &m) { return linalg_traits::row_begin(linalg_cast(m)); } template inline typename select_return::const_row_iterator, typename linalg_traits::row_iterator, const M *>::return_type mat_row_begin(const M &m) { return linalg_traits::row_begin(linalg_cast(m)); } template inline typename linalg_traits::const_row_iterator mat_row_const_begin(const M &m) { return linalg_traits::row_begin(m); } template inline typename select_return::const_row_iterator, typename linalg_traits::row_iterator, M *>::return_type mat_row_end(M &v) { return linalg_traits::row_end(linalg_cast(v)); } template inline typename select_return::const_row_iterator, typename linalg_traits::row_iterator, const M *>::return_type mat_row_end(const M &v) { return linalg_traits::row_end(linalg_cast(v)); } template inline typename linalg_traits::const_row_iterator mat_row_const_end(const M &v) { return linalg_traits::row_end(v); } template inline typename select_return::const_col_iterator, typename linalg_traits::col_iterator, M *>::return_type mat_col_begin(M &v) { return linalg_traits::col_begin(linalg_cast(v)); } template inline typename select_return::const_col_iterator, typename linalg_traits::col_iterator, const M *>::return_type mat_col_begin(const M &v) { return linalg_traits::col_begin(linalg_cast(v)); } template inline typename linalg_traits::const_col_iterator mat_col_const_begin(const M &v) { return linalg_traits::col_begin(v); } template inline typename linalg_traits::const_col_iterator mat_col_const_end(const M &v) { return linalg_traits::col_end(v); } template inline typename select_return::const_col_iterator, typename linalg_traits::col_iterator, M *>::return_type mat_col_end(M &m) { return linalg_traits::col_end(linalg_cast(m)); } template inline typename select_return::const_col_iterator, typename linalg_traits::col_iterator, const M *>::return_type mat_col_end(const M &m) { return linalg_traits::col_end(linalg_cast(m)); } template inline typename select_return::const_sub_row_type, typename linalg_traits::sub_row_type, const MAT *>::return_type mat_row(const MAT &m, size_type i) { return linalg_traits::row(mat_row_begin(m) + i); } template inline typename select_return::const_sub_row_type, typename linalg_traits::sub_row_type, MAT *>::return_type mat_row(MAT &m, size_type i) { return linalg_traits::row(mat_row_begin(m) + i); } template inline typename linalg_traits::const_sub_row_type mat_const_row(const MAT &m, size_type i) { return linalg_traits::row(mat_row_const_begin(m) + i); } template inline typename select_return::const_sub_col_type, typename linalg_traits::sub_col_type, const MAT *>::return_type mat_col(const MAT &m, size_type i) { return linalg_traits::col(mat_col_begin(m) + i); } template inline typename select_return::const_sub_col_type, typename linalg_traits::sub_col_type, MAT *>::return_type mat_col(MAT &m, size_type i) { return linalg_traits::col(mat_col_begin(m) + i); } template inline typename linalg_traits::const_sub_col_type mat_const_col(const MAT &m, size_type i) { return linalg_traits::col(mat_col_const_begin(m) + i); } /* ********************************************************************* */ /* Set to begin end set to end for iterators on non-const sparse vectors.*/ /* ********************************************************************* */ template inline void set_to_begin(IT &it, ORG o, VECT *, linalg_false) { it = vect_begin(*o); } template inline void set_to_begin(IT &it, ORG o, const VECT *, linalg_false) { it = vect_const_begin(*o); } template inline void set_to_end(IT &it, ORG o, VECT *, linalg_false) { it = vect_end(*o); } template inline void set_to_end(IT &it, ORG o, const VECT *, linalg_false) { it = vect_const_end(*o); } template inline void set_to_begin(IT &, ORG, VECT *, linalg_const) { } template inline void set_to_begin(IT &, ORG, const VECT *, linalg_const) { } template inline void set_to_end(IT &, ORG, VECT *, linalg_const) { } template inline void set_to_end(IT &, ORG, const VECT *, linalg_const) { } template inline void set_to_begin(IT &, ORG, VECT *v, linalg_modifiable) { GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; } template inline void set_to_begin(IT &, ORG, const VECT *v, linalg_modifiable) { GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; } template inline void set_to_end(IT &, ORG, VECT *v, linalg_modifiable) { GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; } template 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 size_type index_of_it(const IT &it, size_type, abstract_sparse) { return it.index(); } template size_type index_of_it(const IT &it, size_type, abstract_skyline) { return it.index(); } template size_type index_of_it(const IT &, size_type k, abstract_dense) { return k; } /* ********************************************************************* */ /* Numeric limits. */ /* ********************************************************************* */ template inline T default_tol(T) { using namespace std; static T tol(10); if (tol == T(10)) { if (numeric_limits::is_specialized) tol = numeric_limits::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 inline T default_tol(std::complex) { return default_tol(T()); } template inline T default_min(T) { using namespace std; static T mi(10); if (mi == T(10)) { if (numeric_limits::is_specialized) mi = std::numeric_limits::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 inline T default_min(std::complex) { return default_min(T()); } template inline T default_max(T) { using namespace std; static T mi(10); if (mi == T(10)) { if (numeric_limits::is_specialized) mi = std::numeric_limits::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 inline T default_max(std::complex) { return default_max(T()); } /* use safe_divide to avoid NaNs when dividing very small complex numbers, for example std::complex(1e-23,1e-30)/std::complex(1e-23,1e-30) */ template inline T safe_divide(T a, T b) { return a/b; } template inline std::complex safe_divide(std::complex a, std::complex b) { T m = std::max(gmm::abs(b.real()), gmm::abs(b.imag())); a = std::complex(a.real()/m, a.imag()/m); b = std::complex(b.real()/m, b.imag()/m); return a / b; } /* ******************************************************************** */ /* Write */ /* ******************************************************************** */ template struct cast_char_type { typedef T return_type; }; template <> struct cast_char_type { typedef int return_type; }; template <> struct cast_char_type { typedef unsigned int return_type; }; template inline typename cast_char_type::return_type cast_char(const T &c) { return typename cast_char_type::return_type(c); } template inline void write(std::ostream &o, const L &l) { write(o, l, typename linalg_traits::linalg_type()); } template void write(std::ostream &o, const L &l, abstract_vector) { o << "vector(" << vect_size(l) << ") ["; write(o, l, typename linalg_traits::storage_type()); o << " ]"; } template void write(std::ostream &o, const L &l, abstract_sparse) { typename linalg_traits::const_iterator it = vect_const_begin(l), ite = vect_const_end(l); for (; it != ite; ++it) o << " (r" << it.index() << "," << cast_char(*it) << ")"; } template void write(std::ostream &o, const L &l, abstract_dense) { typename linalg_traits::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 void write(std::ostream &o, const L &l, abstract_skyline) { typedef typename linalg_traits::const_iterator const_iterator; const_iterator it = vect_const_begin(l), ite = vect_const_end(l); if (it != ite) { o << ""; if (it != ite) o << " " << cast_char(*it++); for (; it != ite; ++it) { o << ", " << cast_char(*it); } } } template inline void write(std::ostream &o, const L &l, abstract_matrix) { write(o, l, typename linalg_traits::sub_orientation()); } template 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::storage_type()); o << " )\n"; } } template inline void write(std::ostream &o, const L &l, row_and_col) { write(o, l, row_major()); } template inline void write(std::ostream &o, const L &l, col_and_row) { write(o, l, row_major()); } template 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::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__