You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1119 lines
45 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2015 Yves Renard
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /**@file gmm_def.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 13, 2002.
  29. @brief Basic definitions and tools of GMM.
  30. */
  31. #ifndef GMM_DEF_H__
  32. #define GMM_DEF_H__
  33. #include "gmm_ref.h"
  34. #include <complex>
  35. #ifndef M_PI
  36. # define M_E 2.7182818284590452354 /* e */
  37. # define M_LOG2E 1.4426950408889634074 /* 1/ln(2) */
  38. # define M_LOG10E 0.43429448190325182765 /* 1/ln(10) */
  39. # define M_LN2 0.69314718055994530942 /* ln(2) */
  40. # define M_LN10 2.30258509299404568402 /* ln(10) */
  41. # define M_PI 3.14159265358979323846 /* pi */
  42. # define M_PI_2 1.57079632679489661923 /* pi/2 */
  43. # define M_PI_4 0.78539816339744830962 /* pi/4 */
  44. # define M_1_PI 0.31830988618379067154 /* 1/pi */
  45. # define M_2_PI 0.63661977236758134308 /* 2/pi */
  46. # define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
  47. # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  48. # define M_SQRT1_2 0.70710678118654752440 /* sqrt(2)/2 */
  49. #endif
  50. #ifndef M_PIl
  51. # define M_PIl 3.1415926535897932384626433832795029L /* pi */
  52. # define M_PI_2l 1.5707963267948966192313216916397514L /* pi/2 */
  53. # define M_PI_4l 0.7853981633974483096156608458198757L /* pi/4 */
  54. # define M_1_PIl 0.3183098861837906715377675267450287L /* 1/pi */
  55. # define M_2_PIl 0.6366197723675813430755350534900574L /* 2/pi */
  56. # define M_2_SQRTPIl 1.1283791670955125738961589031215452L /* 2/sqrt(pi) */
  57. #endif
  58. namespace gmm {
  59. typedef size_t size_type;
  60. /* ******************************************************************** */
  61. /* Specifier types */
  62. /* ******************************************************************** */
  63. /* not perfectly null, required by aCC 3.33 */
  64. struct abstract_null_type {
  65. abstract_null_type(int=0) {}
  66. template <typename A,typename B,typename C> void operator()(A,B,C) {}
  67. }; // specify an information lake.
  68. struct linalg_true {};
  69. struct linalg_false {};
  70. template <typename V, typename W> struct linalg_and
  71. { typedef linalg_false bool_type; };
  72. template <> struct linalg_and<linalg_true, linalg_true>
  73. { typedef linalg_true bool_type; };
  74. template <typename V, typename W> struct linalg_or
  75. { typedef linalg_true bool_type; };
  76. template <> struct linalg_and<linalg_false, linalg_false>
  77. { typedef linalg_false bool_type; };
  78. struct linalg_const {}; // A reference is either linalg_const,
  79. struct linalg_modifiable {}; // linalg_modifiable or linalg_false.
  80. struct abstract_vector {}; // The object is a vector
  81. struct abstract_matrix {}; // The object is a matrix
  82. struct abstract_sparse {}; // sparse matrix or vector
  83. struct abstract_skyline {}; // 'sky-line' matrix or vector
  84. struct abstract_dense {}; // dense matrix or vector
  85. struct abstract_indirect {}; // matrix given by the product with a vector
  86. struct row_major {}; // matrix with a row access.
  87. struct col_major {}; // matrix with a column access
  88. struct row_and_col {}; // both accesses but row preference
  89. struct col_and_row {}; // both accesses but column preference
  90. template <typename T> struct transposed_type;
  91. template<> struct transposed_type<row_major> {typedef col_major t_type;};
  92. template<> struct transposed_type<col_major> {typedef row_major t_type;};
  93. template<> struct transposed_type<row_and_col> {typedef col_and_row t_type;};
  94. template<> struct transposed_type<col_and_row> {typedef row_and_col t_type;};
  95. template <typename T> struct principal_orientation_type
  96. { typedef abstract_null_type potype; };
  97. template<> struct principal_orientation_type<row_major>
  98. { typedef row_major potype; };
  99. template<> struct principal_orientation_type<col_major>
  100. { typedef col_major potype; };
  101. template<> struct principal_orientation_type<row_and_col>
  102. { typedef row_major potype; };
  103. template<> struct principal_orientation_type<col_and_row>
  104. { typedef col_major potype; };
  105. // template <typename V> struct linalg_traits;
  106. template <typename V> struct linalg_traits {
  107. typedef abstract_null_type this_type;
  108. typedef abstract_null_type linalg_type;
  109. typedef abstract_null_type value_type;
  110. typedef abstract_null_type is_reference;
  111. typedef abstract_null_type& reference;
  112. typedef abstract_null_type* iterator;
  113. typedef const abstract_null_type* const_iterator;
  114. typedef abstract_null_type index_sorted;
  115. typedef abstract_null_type storage_type;
  116. typedef abstract_null_type origin_type;
  117. typedef abstract_null_type const_sub_row_type;
  118. typedef abstract_null_type sub_row_type;
  119. typedef abstract_null_type const_row_iterator;
  120. typedef abstract_null_type row_iterator;
  121. typedef abstract_null_type const_sub_col_type;
  122. typedef abstract_null_type sub_col_type;
  123. typedef abstract_null_type const_col_iterator;
  124. typedef abstract_null_type col_iterator;
  125. typedef abstract_null_type sub_orientation;
  126. };
  127. template <typename PT, typename V> struct vect_ref_type;
  128. template <typename P, typename V> struct vect_ref_type<P *, V> {
  129. typedef typename linalg_traits<V>::reference access_type;
  130. typedef typename linalg_traits<V>::iterator iterator;
  131. };
  132. template <typename P, typename V> struct vect_ref_type<const P *, V> {
  133. typedef typename linalg_traits<V>::value_type access_type;
  134. typedef typename linalg_traits<V>::const_iterator iterator;
  135. };
  136. template <typename PT> struct const_pointer;
  137. template <typename P> struct const_pointer<P *>
  138. { typedef const P* pointer; };
  139. template <typename P> struct const_pointer<const P *>
  140. { typedef const P* pointer; };
  141. template <typename PT> struct modifiable_pointer;
  142. template <typename P> struct modifiable_pointer<P *>
  143. { typedef P* pointer; };
  144. template <typename P> struct modifiable_pointer<const P *>
  145. { typedef P* pointer; };
  146. template <typename R> struct const_reference;
  147. template <typename R> struct const_reference<R &>
  148. { typedef const R &reference; };
  149. template <typename R> struct const_reference<const R &>
  150. { typedef const R &reference; };
  151. inline bool is_sparse(abstract_sparse) { return true; }
  152. inline bool is_sparse(abstract_dense) { return false; }
  153. inline bool is_sparse(abstract_skyline) { return true; }
  154. inline bool is_sparse(abstract_indirect) { return false; }
  155. template <typename L> inline bool is_sparse(const L &)
  156. { return is_sparse(typename linalg_traits<L>::storage_type()); }
  157. inline bool is_row_matrix_(row_major) { return true; }
  158. inline bool is_row_matrix_(col_major) { return false; }
  159. inline bool is_row_matrix_(row_and_col) { return true; }
  160. inline bool is_row_matrix_(col_and_row) { return true; }
  161. template <typename L> inline bool is_row_matrix(const L &)
  162. { return is_row_matrix_(typename linalg_traits<L>::sub_orientation()); }
  163. inline bool is_col_matrix_(row_major) { return false; }
  164. inline bool is_col_matrix_(col_major) { return true; }
  165. inline bool is_col_matrix_(row_and_col) { return true; }
  166. inline bool is_col_matrix_(col_and_row) { return true; }
  167. template <typename L> inline bool is_col_matrix(const L &)
  168. { return is_col_matrix_(typename linalg_traits<L>::sub_orientation()); }
  169. inline bool is_col_matrix(row_major) { return false; }
  170. inline bool is_col_matrix(col_major) { return true; }
  171. inline bool is_row_matrix(row_major) { return true; }
  172. inline bool is_row_matrix(col_major) { return false; }
  173. template <typename L> inline bool is_const_reference(L) { return false; }
  174. inline bool is_const_reference(linalg_const) { return true; }
  175. template <typename T> struct is_gmm_interfaced_ {
  176. typedef linalg_true result;
  177. };
  178. template<> struct is_gmm_interfaced_<abstract_null_type> {
  179. typedef linalg_false result;
  180. };
  181. template <typename T> struct is_gmm_interfaced {
  182. typedef typename is_gmm_interfaced_<typename gmm::linalg_traits<T>::this_type >::result result;
  183. };
  184. /* ******************************************************************** */
  185. /* types to deal with const object representing a modifiable reference */
  186. /* ******************************************************************** */
  187. template <typename PT, typename R> struct mref_type_
  188. { typedef abstract_null_type return_type; };
  189. template <typename L, typename R> struct mref_type_<L *, R>
  190. { typedef L & return_type; };
  191. template <typename L, typename R> struct mref_type_<const L *, R>
  192. { typedef const L & return_type; };
  193. template <typename L> struct mref_type_<L *, linalg_const>
  194. { typedef const L & return_type; };
  195. template <typename L> struct mref_type_<const L *, linalg_const>
  196. { typedef const L & return_type; };
  197. template <typename L> struct mref_type_<const L *, linalg_modifiable>
  198. { typedef L & return_type; };
  199. template <typename L> struct mref_type_<L *, linalg_modifiable>
  200. { typedef L & return_type; };
  201. template <typename PT> struct mref_type {
  202. typedef typename std::iterator_traits<PT>::value_type L;
  203. typedef typename mref_type_<PT,
  204. typename linalg_traits<L>::is_reference>::return_type return_type;
  205. };
  206. template <typename L> typename mref_type<const L *>::return_type
  207. linalg_cast(const L &l)
  208. { return const_cast<typename mref_type<const L *>::return_type>(l); }
  209. template <typename L> typename mref_type<L *>::return_type linalg_cast(L &l)
  210. { return const_cast<typename mref_type<L *>::return_type>(l); }
  211. template <typename L, typename R> struct cref_type_
  212. { typedef abstract_null_type return_type; };
  213. template <typename L> struct cref_type_<L, linalg_modifiable>
  214. { typedef L & return_type; };
  215. template <typename L> struct cref_type {
  216. typedef typename cref_type_<L,
  217. typename linalg_traits<L>::is_reference>::return_type return_type;
  218. };
  219. template <typename L> typename cref_type<L>::return_type
  220. linalg_const_cast(const L &l)
  221. { return const_cast<typename cref_type<L>::return_type>(l); }
  222. // To be used to select between a reference or a const refercence for
  223. // the return type of a function
  224. // select_return<C1, C2, L *> return C1 if L is a const reference,
  225. // C2 otherwise.
  226. // select_return<C1, C2, const L *> return C2 if L is a modifiable reference
  227. // C1 otherwise.
  228. template <typename C1, typename C2, typename REF> struct select_return_ {
  229. typedef abstract_null_type return_type;
  230. };
  231. template <typename C1, typename C2, typename L>
  232. struct select_return_<C1, C2, const L &> { typedef C1 return_type; };
  233. template <typename C1, typename C2, typename L>
  234. struct select_return_<C1, C2, L &> { typedef C2 return_type; };
  235. template <typename C1, typename C2, typename PT> struct select_return {
  236. typedef typename std::iterator_traits<PT>::value_type L;
  237. typedef typename select_return_<C1, C2,
  238. typename mref_type<PT>::return_type>::return_type return_type;
  239. };
  240. // To be used to select between a reference or a const refercence inside
  241. // a structure or a linagl_traits
  242. // select_ref<C1, C2, L *> return C1 if L is a const reference,
  243. // C2 otherwise.
  244. // select_ref<C1, C2, const L *> return C2 in any case.
  245. template <typename C1, typename C2, typename REF> struct select_ref_
  246. { typedef abstract_null_type ref_type; };
  247. template <typename C1, typename C2, typename L>
  248. struct select_ref_<C1, C2, const L &> { typedef C1 ref_type; };
  249. template <typename C1, typename C2, typename L>
  250. struct select_ref_<C1, C2, L &> { typedef C2 ref_type; };
  251. template <typename C1, typename C2, typename PT> struct select_ref {
  252. typedef typename std::iterator_traits<PT>::value_type L;
  253. typedef typename select_ref_<C1, C2,
  254. typename mref_type<PT>::return_type>::ref_type ref_type;
  255. };
  256. template <typename C1, typename C2, typename L>
  257. struct select_ref<C1, C2, const L *>
  258. { typedef C1 ref_type; };
  259. template<typename R> struct is_a_reference_
  260. { typedef linalg_true reference; };
  261. template<> struct is_a_reference_<linalg_false>
  262. { typedef linalg_false reference; };
  263. template<typename L> struct is_a_reference {
  264. typedef typename is_a_reference_<typename linalg_traits<L>::is_reference>
  265. ::reference reference;
  266. };
  267. template <typename L> inline bool is_original_linalg(const L &)
  268. { return is_original_linalg(typename is_a_reference<L>::reference()); }
  269. inline bool is_original_linalg(linalg_false) { return true; }
  270. inline bool is_original_linalg(linalg_true) { return false; }
  271. template <typename PT> struct which_reference
  272. { typedef abstract_null_type is_reference; };
  273. template <typename PT> struct which_reference<PT *>
  274. { typedef linalg_modifiable is_reference; };
  275. template <typename PT> struct which_reference<const PT *>
  276. { typedef linalg_const is_reference; };
  277. template <typename C1, typename C2, typename R> struct select_orientation_
  278. { typedef abstract_null_type return_type; };
  279. template <typename C1, typename C2>
  280. struct select_orientation_<C1, C2, row_major>
  281. { typedef C1 return_type; };
  282. template <typename C1, typename C2>
  283. struct select_orientation_<C1, C2, col_major>
  284. { typedef C2 return_type; };
  285. template <typename C1, typename C2, typename L> struct select_orientation {
  286. typedef typename select_orientation_<C1, C2,
  287. typename principal_orientation_type<typename
  288. linalg_traits<L>::sub_orientation>::potype>::return_type return_type;
  289. };
  290. /* ******************************************************************** */
  291. /* Operations on scalars */
  292. /* ******************************************************************** */
  293. template <typename T> inline T sqr(T a) { return T(a * a); }
  294. template <typename T> inline T abs(T a) { return (a < T(0)) ? T(-a) : a; }
  295. template <typename T> inline T abs(std::complex<T> a)
  296. { T x = a.real(), y = a.imag(); return T(::sqrt(x*x+y*y)); }
  297. template <typename T> inline T abs_sqr(T a) { return T(a*a); }
  298. template <typename T> inline T abs_sqr(std::complex<T> a)
  299. { return gmm::sqr(a.real()) + gmm::sqr(a.imag()); }
  300. template <typename T> inline T pos(T a) { return (a < T(0)) ? T(0) : a; }
  301. template <typename T> inline T neg(T a) { return (a < T(0)) ? T(-a) : T(0); }
  302. template <typename T> inline T sgn(T a) { return (a < T(0)) ? T(-1) : T(1); }
  303. template <typename T> inline T Heaviside(T a) { return (a < T(0)) ? T(0) : T(1); }
  304. inline double random() { return double(rand())/(RAND_MAX+0.5); }
  305. template <typename T> inline T random(T)
  306. { return T(rand()*2.0)/(T(RAND_MAX)+T(1)/T(2)) - T(1); }
  307. template <typename T> inline std::complex<T> random(std::complex<T>)
  308. { return std::complex<T>(gmm::random(T()), gmm::random(T())); }
  309. template <typename T> inline T irandom(T max)
  310. { return T(gmm::random() * double(max)); }
  311. template <typename T> inline T conj(T a) { return a; }
  312. template <typename T> inline std::complex<T> conj(std::complex<T> a)
  313. { return std::conj(a); }
  314. template <typename T> inline T real(T a) { return a; }
  315. template <typename T> inline T real(std::complex<T> a) { return a.real(); }
  316. template <typename T> inline T imag(T ) { return T(0); }
  317. template <typename T> inline T imag(std::complex<T> a) { return a.imag(); }
  318. template <typename T> inline T sqrt(T a) { return T(::sqrt(a)); }
  319. template <typename T> inline std::complex<T> sqrt(std::complex<T> a) {
  320. T x = a.real(), y = a.imag();
  321. if (x == T(0)) {
  322. T t = T(::sqrt(gmm::abs(y) / T(2)));
  323. return std::complex<T>(t, y < T(0) ? -t : t);
  324. }
  325. T t = T(::sqrt(T(2) * (gmm::abs(a) + gmm::abs(x)))), u = t / T(2);
  326. return x > T(0) ? std::complex<T>(u, y / t)
  327. : std::complex<T>(gmm::abs(y) / t, y < T(0) ? -u : u);
  328. }
  329. using std::swap;
  330. template <typename T> struct number_traits {
  331. typedef T magnitude_type;
  332. };
  333. template <typename T> struct number_traits<std::complex<T> > {
  334. typedef T magnitude_type;
  335. };
  336. template <typename T> inline T conj_product(T a, T b) { return a * b; }
  337. template <typename T> inline
  338. std::complex<T> conj_product(std::complex<T> a, std::complex<T> b)
  339. { return std::conj(a) * b; } // to be optimized ?
  340. template <typename T> inline bool is_complex(T) { return false; }
  341. template <typename T> inline bool is_complex(std::complex<T> )
  342. { return true; }
  343. # define magnitude_of_linalg(M) typename number_traits<typename \
  344. linalg_traits<M>::value_type>::magnitude_type
  345. template<typename T> inline std::complex<T> operator*(const std::complex<T>& a, int b) {
  346. return a*T(b);
  347. }
  348. template<typename T> inline std::complex<T> operator*(int b, const std::complex<T>& a) {
  349. return a*T(b);
  350. }
  351. /* ******************************************************************** */
  352. /* types promotion */
  353. /* ******************************************************************** */
  354. /* should be completed for more specific cases <unsigned int, float> etc */
  355. template <typename T1, typename T2, bool c>
  356. struct strongest_numeric_type_aux {
  357. typedef T1 T;
  358. };
  359. template <typename T1, typename T2>
  360. struct strongest_numeric_type_aux<T1,T2,false> {
  361. typedef T2 T;
  362. };
  363. template <typename T1, typename T2>
  364. struct strongest_numeric_type {
  365. typedef typename
  366. strongest_numeric_type_aux<T1,T2,(sizeof(T1)>sizeof(T2))>::T T;
  367. };
  368. template <typename T1, typename T2>
  369. struct strongest_numeric_type<T1,std::complex<T2> > {
  370. typedef typename number_traits<T1>::magnitude_type R1;
  371. typedef std::complex<typename strongest_numeric_type<R1,T2>::T > T;
  372. };
  373. template <typename T1, typename T2>
  374. struct strongest_numeric_type<std::complex<T1>,T2 > {
  375. typedef typename number_traits<T2>::magnitude_type R2;
  376. typedef std::complex<typename strongest_numeric_type<T1,R2>::T > T;
  377. };
  378. template <typename T1, typename T2>
  379. struct strongest_numeric_type<std::complex<T1>,std::complex<T2> > {
  380. typedef std::complex<typename strongest_numeric_type<T1,T2>::T > T;
  381. };
  382. template<> struct strongest_numeric_type<int,float> { typedef float T; };
  383. template<> struct strongest_numeric_type<float,int> { typedef float T; };
  384. template<> struct strongest_numeric_type<long,float> { typedef float T; };
  385. template<> struct strongest_numeric_type<float,long> { typedef float T; };
  386. template<> struct strongest_numeric_type<long,double> { typedef double T; };
  387. template<> struct strongest_numeric_type<double,long> { typedef double T; };
  388. template <typename V1, typename V2>
  389. struct strongest_value_type {
  390. typedef typename
  391. strongest_numeric_type<typename linalg_traits<V1>::value_type,
  392. typename linalg_traits<V2>::value_type>::T
  393. value_type;
  394. };
  395. template <typename V1, typename V2, typename V3>
  396. struct strongest_value_type3 {
  397. typedef typename
  398. strongest_value_type<V1, typename
  399. strongest_value_type<V2,V3>::value_type>::value_type
  400. value_type;
  401. };
  402. /* ******************************************************************** */
  403. /* Basic vectors used */
  404. /* ******************************************************************** */
  405. template<typename T> struct dense_vector_type
  406. { typedef std::vector<T> vector_type; };
  407. template <typename T> class wsvector;
  408. template <typename T> class rsvector;
  409. template<typename T> struct sparse_vector_type
  410. { typedef wsvector<T> vector_type; };
  411. template <typename T> class slvector;
  412. template <typename T> class dense_matrix;
  413. template <typename VECT> class row_matrix;
  414. template <typename VECT> class col_matrix;
  415. /* ******************************************************************** */
  416. /* Selects a temporary vector type */
  417. /* V if V is a valid vector type, */
  418. /* wsvector if V is a reference on a sparse vector, */
  419. /* std::vector if V is a reference on a dense vector. */
  420. /* ******************************************************************** */
  421. template <typename R, typename S, typename L, typename V>
  422. struct temporary_vector_ {
  423. typedef abstract_null_type vector_type;
  424. };
  425. template <typename V, typename L>
  426. struct temporary_vector_<linalg_true, abstract_sparse, L, V>
  427. { typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
  428. template <typename V, typename L>
  429. struct temporary_vector_<linalg_true, abstract_skyline, L, V>
  430. { typedef slvector<typename linalg_traits<V>::value_type> vector_type; };
  431. template <typename V, typename L>
  432. struct temporary_vector_<linalg_true, abstract_dense, L, V>
  433. { typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
  434. template <typename S, typename V>
  435. struct temporary_vector_<linalg_false, S, abstract_vector, V>
  436. { typedef V vector_type; };
  437. template <typename V>
  438. struct temporary_vector_<linalg_false, abstract_dense, abstract_matrix, V>
  439. { typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
  440. template <typename V>
  441. struct temporary_vector_<linalg_false, abstract_sparse, abstract_matrix, V>
  442. { typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
  443. template <typename V> struct temporary_vector {
  444. typedef typename temporary_vector_<typename is_a_reference<V>::reference,
  445. typename linalg_traits<V>::storage_type,
  446. typename linalg_traits<V>::linalg_type,
  447. V>::vector_type vector_type;
  448. };
  449. /* ******************************************************************** */
  450. /* Selects a temporary matrix type */
  451. /* M if M is a valid matrix type, */
  452. /* row_matrix<wsvector> if M is a reference on a sparse matrix, */
  453. /* dense_matrix if M is a reference on a dense matrix. */
  454. /* ******************************************************************** */
  455. template <typename R, typename S, typename L, typename V>
  456. struct temporary_matrix_ { typedef abstract_null_type matrix_type; };
  457. template <typename V, typename L>
  458. struct temporary_matrix_<linalg_true, abstract_sparse, L, V> {
  459. typedef typename linalg_traits<V>::value_type T;
  460. typedef row_matrix<wsvector<T> > matrix_type;
  461. };
  462. template <typename V, typename L>
  463. struct temporary_matrix_<linalg_true, abstract_skyline, L, V> {
  464. typedef typename linalg_traits<V>::value_type T;
  465. typedef row_matrix<slvector<T> > matrix_type;
  466. };
  467. template <typename V, typename L>
  468. struct temporary_matrix_<linalg_true, abstract_dense, L, V>
  469. { typedef dense_matrix<typename linalg_traits<V>::value_type> matrix_type; };
  470. template <typename S, typename V>
  471. struct temporary_matrix_<linalg_false, S, abstract_matrix, V>
  472. { typedef V matrix_type; };
  473. template <typename V> struct temporary_matrix {
  474. typedef typename temporary_matrix_<typename is_a_reference<V>::reference,
  475. typename linalg_traits<V>::storage_type,
  476. typename linalg_traits<V>::linalg_type,
  477. V>::matrix_type matrix_type;
  478. };
  479. template <typename S, typename L, typename V>
  480. struct temporary_col_matrix_ { typedef abstract_null_type matrix_type; };
  481. template <typename V, typename L>
  482. struct temporary_col_matrix_<abstract_sparse, L, V> {
  483. typedef typename linalg_traits<V>::value_type T;
  484. typedef col_matrix<wsvector<T> > matrix_type;
  485. };
  486. template <typename V, typename L>
  487. struct temporary_col_matrix_<abstract_skyline, L, V> {
  488. typedef typename linalg_traits<V>::value_type T;
  489. typedef col_matrix<slvector<T> > matrix_type;
  490. };
  491. template <typename V, typename L>
  492. struct temporary_col_matrix_<abstract_dense, L, V>
  493. { typedef dense_matrix<typename linalg_traits<V>::value_type> matrix_type; };
  494. template <typename V> struct temporary_col_matrix {
  495. typedef typename temporary_col_matrix_<
  496. typename linalg_traits<V>::storage_type,
  497. typename linalg_traits<V>::linalg_type,
  498. V>::matrix_type matrix_type;
  499. };
  500. template <typename S, typename L, typename V>
  501. struct temporary_row_matrix_ { typedef abstract_null_type matrix_type; };
  502. template <typename V, typename L>
  503. struct temporary_row_matrix_<abstract_sparse, L, V> {
  504. typedef typename linalg_traits<V>::value_type T;
  505. typedef row_matrix<wsvector<T> > matrix_type;
  506. };
  507. template <typename V, typename L>
  508. struct temporary_row_matrix_<abstract_skyline, L, V> {
  509. typedef typename linalg_traits<V>::value_type T;
  510. typedef row_matrix<slvector<T> > matrix_type;
  511. };
  512. template <typename V, typename L>
  513. struct temporary_row_matrix_<abstract_dense, L, V>
  514. { typedef dense_matrix<typename linalg_traits<V>::value_type> matrix_type; };
  515. template <typename V> struct temporary_row_matrix {
  516. typedef typename temporary_row_matrix_<
  517. typename linalg_traits<V>::storage_type,
  518. typename linalg_traits<V>::linalg_type,
  519. V>::matrix_type matrix_type;
  520. };
  521. /* ******************************************************************** */
  522. /* Selects a temporary dense vector type */
  523. /* V if V is a valid dense vector type, */
  524. /* std::vector if V is a reference or another type of vector */
  525. /* ******************************************************************** */
  526. template <typename R, typename S, typename V>
  527. struct temporary_dense_vector_ { typedef abstract_null_type vector_type; };
  528. template <typename S, typename V>
  529. struct temporary_dense_vector_<linalg_true, S, V>
  530. { typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
  531. template <typename V>
  532. struct temporary_dense_vector_<linalg_false, abstract_sparse, V>
  533. { typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
  534. template <typename V>
  535. struct temporary_dense_vector_<linalg_false, abstract_skyline, V>
  536. { typedef std::vector<typename linalg_traits<V>::value_type> vector_type; };
  537. template <typename V>
  538. struct temporary_dense_vector_<linalg_false, abstract_dense, V>
  539. { typedef V vector_type; };
  540. template <typename V> struct temporary_dense_vector {
  541. typedef typename temporary_dense_vector_<typename
  542. is_a_reference<V>::reference,
  543. typename linalg_traits<V>::storage_type, V>::vector_type vector_type;
  544. };
  545. /* ******************************************************************** */
  546. /* Selects a temporary sparse vector type */
  547. /* V if V is a valid sparse vector type, */
  548. /* wsvector if V is a reference or another type of vector */
  549. /* ******************************************************************** */
  550. template <typename R, typename S, typename V>
  551. struct temporary_sparse_vector_ { typedef abstract_null_type vector_type; };
  552. template <typename S, typename V>
  553. struct temporary_sparse_vector_<linalg_true, S, V>
  554. { typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
  555. template <typename V>
  556. struct temporary_sparse_vector_<linalg_false, abstract_sparse, V>
  557. { typedef V vector_type; };
  558. template <typename V>
  559. struct temporary_sparse_vector_<linalg_false, abstract_dense, V>
  560. { typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
  561. template <typename V>
  562. struct temporary_sparse_vector_<linalg_false, abstract_skyline, V>
  563. { typedef wsvector<typename linalg_traits<V>::value_type> vector_type; };
  564. template <typename V> struct temporary_sparse_vector {
  565. typedef typename temporary_sparse_vector_<typename
  566. is_a_reference<V>::reference,
  567. typename linalg_traits<V>::storage_type, V>::vector_type vector_type;
  568. };
  569. /* ******************************************************************** */
  570. /* Selects a temporary sky-line vector type */
  571. /* V if V is a valid sky-line vector type, */
  572. /* slvector if V is a reference or another type of vector */
  573. /* ******************************************************************** */
  574. template <typename R, typename S, typename V>
  575. struct temporary_skyline_vector_
  576. { typedef abstract_null_type vector_type; };
  577. template <typename S, typename V>
  578. struct temporary_skyline_vector_<linalg_true, S, V>
  579. { typedef slvector<typename linalg_traits<V>::value_type> vector_type; };
  580. template <typename V>
  581. struct temporary_skyline_vector_<linalg_false, abstract_skyline, V>
  582. { typedef V vector_type; };
  583. template <typename V>
  584. struct temporary_skyline_vector_<linalg_false, abstract_dense, V>
  585. { typedef slvector<typename linalg_traits<V>::value_type> vector_type; };
  586. template <typename V>
  587. struct temporary_skyline_vector_<linalg_false, abstract_sparse, V>
  588. { typedef slvector<typename linalg_traits<V>::value_type> vector_type; };
  589. template <typename V> struct temporary_skylines_vector {
  590. typedef typename temporary_skyline_vector_<typename
  591. is_a_reference<V>::reference,
  592. typename linalg_traits<V>::storage_type, V>::vector_type vector_type;
  593. };
  594. /* ********************************************************************* */
  595. /* Definition & Comparison of origins. */
  596. /* ********************************************************************* */
  597. template <typename L>
  598. typename select_return<const typename linalg_traits<L>::origin_type *,
  599. typename linalg_traits<L>::origin_type *,
  600. L *>::return_type
  601. linalg_origin(L &l)
  602. { return linalg_traits<L>::origin(linalg_cast(l)); }
  603. template <typename L>
  604. typename select_return<const typename linalg_traits<L>::origin_type *,
  605. typename linalg_traits<L>::origin_type *,
  606. const L *>::return_type
  607. linalg_origin(const L &l)
  608. { return linalg_traits<L>::origin(linalg_cast(l)); }
  609. template <typename PT1, typename PT2>
  610. bool same_porigin(PT1, PT2) { return false; }
  611. template <typename PT>
  612. bool same_porigin(PT pt1, PT pt2) { return (pt1 == pt2); }
  613. template <typename L1, typename L2>
  614. bool same_origin(const L1 &l1, const L2 &l2)
  615. { return same_porigin(linalg_origin(l1), linalg_origin(l2)); }
  616. /* ******************************************************************** */
  617. /* Miscellaneous */
  618. /* ******************************************************************** */
  619. template <typename V> inline size_type vect_size(const V &v)
  620. { return linalg_traits<V>::size(v); }
  621. template <typename MAT> inline size_type mat_nrows(const MAT &m)
  622. { return linalg_traits<MAT>::nrows(m); }
  623. template <typename MAT> inline size_type mat_ncols(const MAT &m)
  624. { return linalg_traits<MAT>::ncols(m); }
  625. template <typename V> inline
  626. typename select_return<typename linalg_traits<V>::const_iterator,
  627. typename linalg_traits<V>::iterator, V *>::return_type
  628. vect_begin(V &v)
  629. { return linalg_traits<V>::begin(linalg_cast(v)); }
  630. template <typename V> inline
  631. typename select_return<typename linalg_traits<V>::const_iterator,
  632. typename linalg_traits<V>::iterator, const V *>::return_type
  633. vect_begin(const V &v)
  634. { return linalg_traits<V>::begin(linalg_cast(v)); }
  635. template <typename V> inline
  636. typename linalg_traits<V>::const_iterator
  637. vect_const_begin(const V &v)
  638. { return linalg_traits<V>::begin(v); }
  639. template <typename V> inline
  640. typename select_return<typename linalg_traits<V>::const_iterator,
  641. typename linalg_traits<V>::iterator, V *>::return_type
  642. vect_end(V &v)
  643. { return linalg_traits<V>::end(linalg_cast(v)); }
  644. template <typename V> inline
  645. typename select_return<typename linalg_traits<V>::const_iterator,
  646. typename linalg_traits<V>::iterator, const V *>::return_type
  647. vect_end(const V &v)
  648. { return linalg_traits<V>::end(linalg_cast(v)); }
  649. template <typename V> inline
  650. typename linalg_traits<V>::const_iterator
  651. vect_const_end(const V &v)
  652. { return linalg_traits<V>::end(v); }
  653. template <typename M> inline
  654. typename select_return<typename linalg_traits<M>::const_row_iterator,
  655. typename linalg_traits<M>::row_iterator, M *>::return_type
  656. mat_row_begin(M &m) { return linalg_traits<M>::row_begin(linalg_cast(m)); }
  657. template <typename M> inline
  658. typename select_return<typename linalg_traits<M>::const_row_iterator,
  659. typename linalg_traits<M>::row_iterator, const M *>::return_type
  660. mat_row_begin(const M &m)
  661. { return linalg_traits<M>::row_begin(linalg_cast(m)); }
  662. template <typename M> inline typename linalg_traits<M>::const_row_iterator
  663. mat_row_const_begin(const M &m)
  664. { return linalg_traits<M>::row_begin(m); }
  665. template <typename M> inline
  666. typename select_return<typename linalg_traits<M>::const_row_iterator,
  667. typename linalg_traits<M>::row_iterator, M *>::return_type
  668. mat_row_end(M &v) {
  669. return linalg_traits<M>::row_end(linalg_cast(v));
  670. }
  671. template <typename M> inline
  672. typename select_return<typename linalg_traits<M>::const_row_iterator,
  673. typename linalg_traits<M>::row_iterator, const M *>::return_type
  674. mat_row_end(const M &v) {
  675. return linalg_traits<M>::row_end(linalg_cast(v));
  676. }
  677. template <typename M> inline
  678. typename linalg_traits<M>::const_row_iterator
  679. mat_row_const_end(const M &v)
  680. { return linalg_traits<M>::row_end(v); }
  681. template <typename M> inline
  682. typename select_return<typename linalg_traits<M>::const_col_iterator,
  683. typename linalg_traits<M>::col_iterator, M *>::return_type
  684. mat_col_begin(M &v) {
  685. return linalg_traits<M>::col_begin(linalg_cast(v));
  686. }
  687. template <typename M> inline
  688. typename select_return<typename linalg_traits<M>::const_col_iterator,
  689. typename linalg_traits<M>::col_iterator, const M *>::return_type
  690. mat_col_begin(const M &v) {
  691. return linalg_traits<M>::col_begin(linalg_cast(v));
  692. }
  693. template <typename M> inline
  694. typename linalg_traits<M>::const_col_iterator
  695. mat_col_const_begin(const M &v)
  696. { return linalg_traits<M>::col_begin(v); }
  697. template <typename M> inline
  698. typename linalg_traits<M>::const_col_iterator
  699. mat_col_const_end(const M &v)
  700. { return linalg_traits<M>::col_end(v); }
  701. template <typename M> inline
  702. typename select_return<typename linalg_traits<M>::const_col_iterator,
  703. typename linalg_traits<M>::col_iterator,
  704. M *>::return_type
  705. mat_col_end(M &m)
  706. { return linalg_traits<M>::col_end(linalg_cast(m)); }
  707. template <typename M> inline
  708. typename select_return<typename linalg_traits<M>::const_col_iterator,
  709. typename linalg_traits<M>::col_iterator,
  710. const M *>::return_type
  711. mat_col_end(const M &m)
  712. { return linalg_traits<M>::col_end(linalg_cast(m)); }
  713. template <typename MAT> inline
  714. typename select_return<typename linalg_traits<MAT>::const_sub_row_type,
  715. typename linalg_traits<MAT>::sub_row_type,
  716. const MAT *>::return_type
  717. mat_row(const MAT &m, size_type i)
  718. { return linalg_traits<MAT>::row(mat_row_begin(m) + i); }
  719. template <typename MAT> inline
  720. typename select_return<typename linalg_traits<MAT>::const_sub_row_type,
  721. typename linalg_traits<MAT>::sub_row_type,
  722. MAT *>::return_type
  723. mat_row(MAT &m, size_type i)
  724. { return linalg_traits<MAT>::row(mat_row_begin(m) + i); }
  725. template <typename MAT> inline
  726. typename linalg_traits<MAT>::const_sub_row_type
  727. mat_const_row(const MAT &m, size_type i)
  728. { return linalg_traits<MAT>::row(mat_row_const_begin(m) + i); }
  729. template <typename MAT> inline
  730. typename select_return<typename linalg_traits<MAT>::const_sub_col_type,
  731. typename linalg_traits<MAT>::sub_col_type,
  732. const MAT *>::return_type
  733. mat_col(const MAT &m, size_type i)
  734. { return linalg_traits<MAT>::col(mat_col_begin(m) + i); }
  735. template <typename MAT> inline
  736. typename select_return<typename linalg_traits<MAT>::const_sub_col_type,
  737. typename linalg_traits<MAT>::sub_col_type,
  738. MAT *>::return_type
  739. mat_col(MAT &m, size_type i)
  740. { return linalg_traits<MAT>::col(mat_col_begin(m) + i); }
  741. template <typename MAT> inline
  742. typename linalg_traits<MAT>::const_sub_col_type
  743. mat_const_col(const MAT &m, size_type i)
  744. { return linalg_traits<MAT>::col(mat_col_const_begin(m) + i); }
  745. /* ********************************************************************* */
  746. /* Set to begin end set to end for iterators on non-const sparse vectors.*/
  747. /* ********************************************************************* */
  748. template <typename IT, typename ORG, typename VECT> inline
  749. void set_to_begin(IT &it, ORG o, VECT *, linalg_false)
  750. { it = vect_begin(*o); }
  751. template <typename IT, typename ORG, typename VECT> inline
  752. void set_to_begin(IT &it, ORG o, const VECT *, linalg_false)
  753. { it = vect_const_begin(*o); }
  754. template <typename IT, typename ORG, typename VECT> inline
  755. void set_to_end(IT &it, ORG o, VECT *, linalg_false)
  756. { it = vect_end(*o); }
  757. template <typename IT, typename ORG, typename VECT> inline
  758. void set_to_end(IT &it, ORG o, const VECT *, linalg_false)
  759. { it = vect_const_end(*o); }
  760. template <typename IT, typename ORG, typename VECT> inline
  761. void set_to_begin(IT &, ORG, VECT *, linalg_const) { }
  762. template <typename IT, typename ORG, typename VECT> inline
  763. void set_to_begin(IT &, ORG, const VECT *, linalg_const) { }
  764. template <typename IT, typename ORG, typename VECT> inline
  765. void set_to_end(IT &, ORG, VECT *, linalg_const) { }
  766. template <typename IT, typename ORG, typename VECT> inline
  767. void set_to_end(IT &, ORG, const VECT *, linalg_const) { }
  768. template <typename IT, typename ORG, typename VECT> inline
  769. void set_to_begin(IT &, ORG, VECT *v, linalg_modifiable)
  770. { GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; }
  771. template <typename IT, typename ORG, typename VECT> inline
  772. void set_to_begin(IT &, ORG, const VECT *v, linalg_modifiable)
  773. { GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; }
  774. template <typename IT, typename ORG, typename VECT> inline
  775. void set_to_end(IT &, ORG, VECT *v, linalg_modifiable)
  776. { GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; }
  777. template <typename IT, typename ORG, typename VECT> inline
  778. void set_to_end(IT &, ORG, const VECT *v, linalg_modifiable)
  779. { GMM_ASSERT3(!is_sparse(*v), "internal_error"); v = 0; }
  780. /* ******************************************************************** */
  781. /* General index for certain algorithms. */
  782. /* ******************************************************************** */
  783. template<class IT>
  784. size_type index_of_it(const IT &it, size_type, abstract_sparse)
  785. { return it.index(); }
  786. template<class IT>
  787. size_type index_of_it(const IT &it, size_type, abstract_skyline)
  788. { return it.index(); }
  789. template<class IT>
  790. size_type index_of_it(const IT &, size_type k, abstract_dense)
  791. { return k; }
  792. /* ********************************************************************* */
  793. /* Numeric limits. */
  794. /* ********************************************************************* */
  795. template<typename T> inline T default_tol(T) {
  796. using namespace std;
  797. static T tol(10);
  798. if (tol == T(10)) {
  799. if (numeric_limits<T>::is_specialized)
  800. tol = numeric_limits<T>::epsilon();
  801. else {
  802. int i=int(sizeof(T)/4); while(i-- > 0) tol*=T(1E-8);
  803. GMM_WARNING1("The numeric type " << typeid(T).name()
  804. << " has no numeric_limits defined !!\n"
  805. << "Taking " << tol << " as default tolerance");
  806. }
  807. }
  808. return tol;
  809. }
  810. template<typename T> inline T default_tol(std::complex<T>)
  811. { return default_tol(T()); }
  812. template<typename T> inline T default_min(T) {
  813. using namespace std;
  814. static T mi(10);
  815. if (mi == T(10)) {
  816. if (numeric_limits<T>::is_specialized)
  817. mi = std::numeric_limits<T>::min();
  818. else {
  819. mi = T(0);
  820. GMM_WARNING1("The numeric type " << typeid(T).name()
  821. << " has no numeric_limits defined !!\n"
  822. << "Taking 0 as default minimum");
  823. }
  824. }
  825. return mi;
  826. }
  827. template<typename T> inline T default_min(std::complex<T>)
  828. { return default_min(T()); }
  829. template<typename T> inline T default_max(T) {
  830. using namespace std;
  831. static T mi(10);
  832. if (mi == T(10)) {
  833. if (numeric_limits<T>::is_specialized)
  834. mi = std::numeric_limits<T>::max();
  835. else {
  836. mi = T(1);
  837. GMM_WARNING1("The numeric type " << typeid(T).name()
  838. << " has no numeric_limits defined !!\n"
  839. << "Taking 1 as default maximum !");
  840. }
  841. }
  842. return mi;
  843. }
  844. template<typename T> inline T default_max(std::complex<T>)
  845. { return default_max(T()); }
  846. /*
  847. use safe_divide to avoid NaNs when dividing very small complex
  848. numbers, for example
  849. std::complex<float>(1e-23,1e-30)/std::complex<float>(1e-23,1e-30)
  850. */
  851. template<typename T> inline T safe_divide(T a, T b) { return a/b; }
  852. template<typename T> inline std::complex<T>
  853. safe_divide(std::complex<T> a, std::complex<T> b) {
  854. T m = std::max(gmm::abs(b.real()), gmm::abs(b.imag()));
  855. a = std::complex<T>(a.real()/m, a.imag()/m);
  856. b = std::complex<T>(b.real()/m, b.imag()/m);
  857. return a / b;
  858. }
  859. /* ******************************************************************** */
  860. /* Write */
  861. /* ******************************************************************** */
  862. template <typename T> struct cast_char_type { typedef T return_type; };
  863. template <> struct cast_char_type<signed char> { typedef int return_type; };
  864. template <> struct cast_char_type<unsigned char>
  865. { typedef unsigned int return_type; };
  866. template <typename T> inline typename cast_char_type<T>::return_type
  867. cast_char(const T &c) { return typename cast_char_type<T>::return_type(c); }
  868. template <typename L> inline void write(std::ostream &o, const L &l)
  869. { write(o, l, typename linalg_traits<L>::linalg_type()); }
  870. template <typename L> void write(std::ostream &o, const L &l,
  871. abstract_vector) {
  872. o << "vector(" << vect_size(l) << ") [";
  873. write(o, l, typename linalg_traits<L>::storage_type());
  874. o << " ]";
  875. }
  876. template <typename L> void write(std::ostream &o, const L &l,
  877. abstract_sparse) {
  878. typename linalg_traits<L>::const_iterator it = vect_const_begin(l),
  879. ite = vect_const_end(l);
  880. for (; it != ite; ++it)
  881. o << " (r" << it.index() << ", " << cast_char(*it) << ")";
  882. }
  883. template <typename L> void write(std::ostream &o, const L &l,
  884. abstract_dense) {
  885. typename linalg_traits<L>::const_iterator it = vect_const_begin(l),
  886. ite = vect_const_end(l);
  887. if (it != ite) o << " " << cast_char(*it++);
  888. for (; it != ite; ++it) o << ", " << cast_char(*it);
  889. }
  890. template <typename L> void write(std::ostream &o, const L &l,
  891. abstract_skyline) {
  892. typedef typename linalg_traits<L>::const_iterator const_iterator;
  893. const_iterator it = vect_const_begin(l), ite = vect_const_end(l);
  894. if (it != ite) {
  895. o << "<r+" << it.index() << ">";
  896. if (it != ite) o << " " << cast_char(*it++);
  897. for (; it != ite; ++it) { o << ", " << cast_char(*it); }
  898. }
  899. }
  900. template <typename L> inline void write(std::ostream &o, const L &l,
  901. abstract_matrix) {
  902. write(o, l, typename linalg_traits<L>::sub_orientation());
  903. }
  904. template <typename L> void write(std::ostream &o, const L &l,
  905. row_major) {
  906. o << "matrix(" << mat_nrows(l) << ", " << mat_ncols(l) << ")" << endl;
  907. for (size_type i = 0; i < mat_nrows(l); ++i) {
  908. o << "(";
  909. write(o, mat_const_row(l, i), typename linalg_traits<L>::storage_type());
  910. o << " )\n";
  911. }
  912. }
  913. template <typename L> inline
  914. void write(std::ostream &o, const L &l, row_and_col)
  915. { write(o, l, row_major()); }
  916. template <typename L> inline
  917. void write(std::ostream &o, const L &l, col_and_row)
  918. { write(o, l, row_major()); }
  919. template <typename L> void write(std::ostream &o, const L &l, col_major) {
  920. o << "matrix(" << mat_nrows(l) << ", " << mat_ncols(l) << ")" << endl;
  921. for (size_type i = 0; i < mat_nrows(l); ++i) {
  922. o << "(";
  923. if (is_sparse(l)) { // not optimized ...
  924. for (size_type j = 0; j < mat_ncols(l); ++j)
  925. if (l(i,j) != typename linalg_traits<L>::value_type(0))
  926. o << " (r" << j << ", " << l(i,j) << ")";
  927. }
  928. else {
  929. if (mat_ncols(l) != 0) o << ' ' << l(i, 0);
  930. for (size_type j = 1; j < mat_ncols(l); ++j) o << ", " << l(i, j);
  931. }
  932. o << " )\n";
  933. }
  934. }
  935. }
  936. #endif // GMM_DEF_H__