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.

398 lines
17 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2003-2017 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_conjugated.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date September 18, 2003.
  29. @brief handle conjugation of complex matrices/vectors.
  30. */
  31. #ifndef GMM_CONJUGATED_H__
  32. #define GMM_CONJUGATED_H__
  33. #include "gmm_def.h"
  34. namespace gmm {
  35. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  36. /* ********************************************************************* */
  37. /* Conjugated references on vectors */
  38. /* ********************************************************************* */
  39. template <typename IT> struct conjugated_const_iterator {
  40. typedef typename std::iterator_traits<IT>::value_type value_type;
  41. typedef typename std::iterator_traits<IT>::pointer pointer;
  42. typedef typename std::iterator_traits<IT>::reference reference;
  43. typedef typename std::iterator_traits<IT>::difference_type difference_type;
  44. typedef typename std::iterator_traits<IT>::iterator_category
  45. iterator_category;
  46. IT it;
  47. conjugated_const_iterator(void) {}
  48. conjugated_const_iterator(const IT &i) : it(i) {}
  49. inline size_type index(void) const { return it.index(); }
  50. conjugated_const_iterator operator ++(int)
  51. { conjugated_const_iterator tmp = *this; ++it; return tmp; }
  52. conjugated_const_iterator operator --(int)
  53. { conjugated_const_iterator tmp = *this; --it; return tmp; }
  54. conjugated_const_iterator &operator ++() { ++it; return *this; }
  55. conjugated_const_iterator &operator --() { --it; return *this; }
  56. conjugated_const_iterator &operator +=(difference_type i)
  57. { it += i; return *this; }
  58. conjugated_const_iterator &operator -=(difference_type i)
  59. { it -= i; return *this; }
  60. conjugated_const_iterator operator +(difference_type i) const
  61. { conjugated_const_iterator itb = *this; return (itb += i); }
  62. conjugated_const_iterator operator -(difference_type i) const
  63. { conjugated_const_iterator itb = *this; return (itb -= i); }
  64. difference_type operator -(const conjugated_const_iterator &i) const
  65. { return difference_type(it - i.it); }
  66. value_type operator *() const { return gmm::conj(*it); }
  67. value_type operator [](size_type ii) const { return gmm::conj(it[ii]); }
  68. bool operator ==(const conjugated_const_iterator &i) const
  69. { return (i.it == it); }
  70. bool operator !=(const conjugated_const_iterator &i) const
  71. { return (i.it != it); }
  72. bool operator < (const conjugated_const_iterator &i) const
  73. { return (it < i.it); }
  74. };
  75. template <typename V> struct conjugated_vector_const_ref {
  76. typedef conjugated_vector_const_ref<V> this_type;
  77. typedef typename linalg_traits<V>::value_type value_type;
  78. typedef typename linalg_traits<V>::const_iterator iterator;
  79. typedef typename linalg_traits<this_type>::reference reference;
  80. typedef typename linalg_traits<this_type>::origin_type origin_type;
  81. iterator begin_, end_;
  82. const origin_type *origin;
  83. size_type size_;
  84. conjugated_vector_const_ref(const V &v)
  85. : begin_(vect_const_begin(v)), end_(vect_const_end(v)),
  86. origin(linalg_origin(v)),
  87. size_(vect_size(v)) {}
  88. reference operator[](size_type i) const
  89. { return gmm::conj(linalg_traits<V>::access(origin, begin_, end_, i)); }
  90. };
  91. template <typename V> struct linalg_traits<conjugated_vector_const_ref<V> > {
  92. typedef conjugated_vector_const_ref<V> this_type;
  93. typedef typename linalg_traits<V>::origin_type origin_type;
  94. typedef linalg_const is_reference;
  95. typedef abstract_vector linalg_type;
  96. typedef typename linalg_traits<V>::value_type value_type;
  97. typedef value_type reference;
  98. typedef abstract_null_type iterator;
  99. typedef conjugated_const_iterator<typename
  100. linalg_traits<V>::const_iterator> const_iterator;
  101. typedef typename linalg_traits<V>::storage_type storage_type;
  102. typedef typename linalg_traits<V>::index_sorted index_sorted;
  103. static size_type size(const this_type &v) { return v.size_; }
  104. static iterator begin(this_type &v) { return iterator(v.begin_); }
  105. static const_iterator begin(const this_type &v)
  106. { return const_iterator(v.begin_); }
  107. static iterator end(this_type &v)
  108. { return iterator(v.end_); }
  109. static const_iterator end(const this_type &v)
  110. { return const_iterator(v.end_); }
  111. static value_type access(const origin_type *o, const const_iterator &it,
  112. const const_iterator &ite, size_type i)
  113. { return gmm::conj(linalg_traits<V>::access(o, it.it, ite.it, i)); }
  114. static const origin_type* origin(const this_type &v) { return v.origin; }
  115. };
  116. template<typename V> std::ostream &operator <<
  117. (std::ostream &o, const conjugated_vector_const_ref<V>& m)
  118. { gmm::write(o,m); return o; }
  119. /* ********************************************************************* */
  120. /* Conjugated references on matrices */
  121. /* ********************************************************************* */
  122. template <typename M> struct conjugated_row_const_iterator {
  123. typedef conjugated_row_const_iterator<M> iterator;
  124. typedef typename linalg_traits<M>::const_row_iterator ITER;
  125. typedef typename linalg_traits<M>::value_type value_type;
  126. typedef ptrdiff_t difference_type;
  127. typedef size_t size_type;
  128. ITER it;
  129. iterator operator ++(int) { iterator tmp = *this; it++; return tmp; }
  130. iterator operator --(int) { iterator tmp = *this; it--; return tmp; }
  131. iterator &operator ++() { it++; return *this; }
  132. iterator &operator --() { it--; return *this; }
  133. iterator &operator +=(difference_type i) { it += i; return *this; }
  134. iterator &operator -=(difference_type i) { it -= i; return *this; }
  135. iterator operator +(difference_type i) const
  136. { iterator itt = *this; return (itt += i); }
  137. iterator operator -(difference_type i) const
  138. { iterator itt = *this; return (itt -= i); }
  139. difference_type operator -(const iterator &i) const
  140. { return it - i.it; }
  141. ITER operator *() const { return it; }
  142. ITER operator [](int i) { return it + i; }
  143. bool operator ==(const iterator &i) const { return (it == i.it); }
  144. bool operator !=(const iterator &i) const { return !(i == *this); }
  145. bool operator < (const iterator &i) const { return (it < i.it); }
  146. conjugated_row_const_iterator(void) {}
  147. conjugated_row_const_iterator(const ITER &i) : it(i) { }
  148. };
  149. template <typename M> struct conjugated_row_matrix_const_ref {
  150. typedef conjugated_row_matrix_const_ref<M> this_type;
  151. typedef typename linalg_traits<M>::const_row_iterator iterator;
  152. typedef typename linalg_traits<M>::value_type value_type;
  153. typedef typename linalg_traits<this_type>::origin_type origin_type;
  154. iterator begin_, end_;
  155. const origin_type *origin;
  156. size_type nr, nc;
  157. conjugated_row_matrix_const_ref(const M &m)
  158. : begin_(mat_row_begin(m)), end_(mat_row_end(m)),
  159. origin(linalg_origin(m)), nr(mat_ncols(m)), nc(mat_nrows(m)) {}
  160. value_type operator()(size_type i, size_type j) const
  161. { return gmm::conj(linalg_traits<M>::access(begin_+j, i)); }
  162. };
  163. template<typename M> std::ostream &operator <<
  164. (std::ostream &o, const conjugated_row_matrix_const_ref<M>& m)
  165. { gmm::write(o,m); return o; }
  166. template <typename M> struct conjugated_col_const_iterator {
  167. typedef conjugated_col_const_iterator<M> iterator;
  168. typedef typename linalg_traits<M>::const_col_iterator ITER;
  169. typedef typename linalg_traits<M>::value_type value_type;
  170. typedef ptrdiff_t difference_type;
  171. typedef size_t size_type;
  172. ITER it;
  173. iterator operator ++(int) { iterator tmp = *this; it++; return tmp; }
  174. iterator operator --(int) { iterator tmp = *this; it--; return tmp; }
  175. iterator &operator ++() { it++; return *this; }
  176. iterator &operator --() { it--; return *this; }
  177. iterator &operator +=(difference_type i) { it += i; return *this; }
  178. iterator &operator -=(difference_type i) { it -= i; return *this; }
  179. iterator operator +(difference_type i) const
  180. { iterator itt = *this; return (itt += i); }
  181. iterator operator -(difference_type i) const
  182. { iterator itt = *this; return (itt -= i); }
  183. difference_type operator -(const iterator &i) const
  184. { return it - i.it; }
  185. ITER operator *() const { return it; }
  186. ITER operator [](int i) { return it + i; }
  187. bool operator ==(const iterator &i) const { return (it == i.it); }
  188. bool operator !=(const iterator &i) const { return !(i == *this); }
  189. bool operator < (const iterator &i) const { return (it < i.it); }
  190. conjugated_col_const_iterator(void) {}
  191. conjugated_col_const_iterator(const ITER &i) : it(i) { }
  192. };
  193. template <typename M> struct conjugated_col_matrix_const_ref {
  194. typedef conjugated_col_matrix_const_ref<M> this_type;
  195. typedef typename linalg_traits<M>::const_col_iterator iterator;
  196. typedef typename linalg_traits<M>::value_type value_type;
  197. typedef typename linalg_traits<this_type>::origin_type origin_type;
  198. iterator begin_, end_;
  199. const origin_type *origin;
  200. size_type nr, nc;
  201. conjugated_col_matrix_const_ref(const M &m)
  202. : begin_(mat_col_begin(m)), end_(mat_col_end(m)),
  203. origin(linalg_origin(m)), nr(mat_ncols(m)), nc(mat_nrows(m)) {}
  204. value_type operator()(size_type i, size_type j) const
  205. { return gmm::conj(linalg_traits<M>::access(begin_+i, j)); }
  206. };
  207. template<typename M> std::ostream &operator <<
  208. (std::ostream &o, const conjugated_col_matrix_const_ref<M>& m)
  209. { gmm::write(o,m); return o; }
  210. template <typename L, typename SO> struct conjugated_return__ {
  211. typedef conjugated_row_matrix_const_ref<L> return_type;
  212. };
  213. template <typename L> struct conjugated_return__<L, col_major> {
  214. typedef conjugated_col_matrix_const_ref<L> return_type;
  215. };
  216. template <typename L, typename T, typename LT> struct conjugated_return_ {
  217. typedef const L & return_type;
  218. };
  219. template <typename L, typename T>
  220. struct conjugated_return_<L, std::complex<T>, abstract_vector> {
  221. typedef conjugated_vector_const_ref<L> return_type;
  222. };
  223. template <typename L, typename T>
  224. struct conjugated_return_<L, T, abstract_matrix> {
  225. typedef typename conjugated_return__<L,
  226. typename principal_orientation_type<typename
  227. linalg_traits<L>::sub_orientation>::potype
  228. >::return_type return_type;
  229. };
  230. template <typename L> struct conjugated_return {
  231. typedef typename
  232. conjugated_return_<L, typename linalg_traits<L>::value_type,
  233. typename linalg_traits<L>::linalg_type
  234. >::return_type return_type;
  235. };
  236. ///@endcond
  237. /** return a conjugated view of the input matrix or vector. */
  238. template <typename L> inline
  239. typename conjugated_return<L>::return_type
  240. conjugated(const L &v) {
  241. return conjugated(v, typename linalg_traits<L>::value_type(),
  242. typename linalg_traits<L>::linalg_type());
  243. }
  244. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  245. template <typename L, typename T, typename LT> inline
  246. const L & conjugated(const L &v, T, LT) { return v; }
  247. template <typename L, typename T> inline
  248. conjugated_vector_const_ref<L> conjugated(const L &v, std::complex<T>,
  249. abstract_vector)
  250. { return conjugated_vector_const_ref<L>(v); }
  251. template <typename L, typename T> inline
  252. typename conjugated_return__<L,
  253. typename principal_orientation_type<typename
  254. linalg_traits<L>::sub_orientation>::potype>::return_type
  255. conjugated(const L &v, T, abstract_matrix) {
  256. return conjugated(v, typename principal_orientation_type<typename
  257. linalg_traits<L>::sub_orientation>::potype());
  258. }
  259. template <typename L> inline
  260. conjugated_row_matrix_const_ref<L> conjugated(const L &v, row_major)
  261. { return conjugated_row_matrix_const_ref<L>(v); }
  262. template <typename L> inline
  263. conjugated_col_matrix_const_ref<L> conjugated(const L &v, col_major)
  264. { return conjugated_col_matrix_const_ref<L>(v); }
  265. template <typename M>
  266. struct linalg_traits<conjugated_row_matrix_const_ref<M> > {
  267. typedef conjugated_row_matrix_const_ref<M> this_type;
  268. typedef typename linalg_traits<M>::origin_type origin_type;
  269. typedef linalg_const is_reference;
  270. typedef abstract_matrix linalg_type;
  271. typedef typename linalg_traits<M>::value_type value_type;
  272. typedef value_type reference;
  273. typedef typename linalg_traits<M>::storage_type storage_type;
  274. typedef typename org_type<typename linalg_traits<M>::const_sub_row_type>::t vector_type;
  275. typedef conjugated_vector_const_ref<vector_type> sub_col_type;
  276. typedef conjugated_vector_const_ref<vector_type> const_sub_col_type;
  277. typedef conjugated_row_const_iterator<M> col_iterator;
  278. typedef conjugated_row_const_iterator<M> const_col_iterator;
  279. typedef abstract_null_type const_sub_row_type;
  280. typedef abstract_null_type sub_row_type;
  281. typedef abstract_null_type const_row_iterator;
  282. typedef abstract_null_type row_iterator;
  283. typedef col_major sub_orientation;
  284. typedef typename linalg_traits<M>::index_sorted index_sorted;
  285. static inline size_type ncols(const this_type &m) { return m.nc; }
  286. static inline size_type nrows(const this_type &m) { return m.nr; }
  287. static inline const_sub_col_type col(const const_col_iterator &it)
  288. { return conjugated(linalg_traits<M>::row(it.it)); }
  289. static inline const_col_iterator col_begin(const this_type &m)
  290. { return const_col_iterator(m.begin_); }
  291. static inline const_col_iterator col_end(const this_type &m)
  292. { return const_col_iterator(m.end_); }
  293. static inline const origin_type* origin(const this_type &m)
  294. { return m.origin; }
  295. static value_type access(const const_col_iterator &it, size_type i)
  296. { return gmm::conj(linalg_traits<M>::access(it.it, i)); }
  297. };
  298. template <typename M>
  299. struct linalg_traits<conjugated_col_matrix_const_ref<M> > {
  300. typedef conjugated_col_matrix_const_ref<M> this_type;
  301. typedef typename linalg_traits<M>::origin_type origin_type;
  302. typedef linalg_const is_reference;
  303. typedef abstract_matrix linalg_type;
  304. typedef typename linalg_traits<M>::value_type value_type;
  305. typedef value_type reference;
  306. typedef typename linalg_traits<M>::storage_type storage_type;
  307. typedef typename org_type<typename linalg_traits<M>::const_sub_col_type>::t vector_type;
  308. typedef conjugated_vector_const_ref<vector_type> sub_row_type;
  309. typedef conjugated_vector_const_ref<vector_type> const_sub_row_type;
  310. typedef conjugated_col_const_iterator<M> row_iterator;
  311. typedef conjugated_col_const_iterator<M> const_row_iterator;
  312. typedef abstract_null_type const_sub_col_type;
  313. typedef abstract_null_type sub_col_type;
  314. typedef abstract_null_type const_col_iterator;
  315. typedef abstract_null_type col_iterator;
  316. typedef row_major sub_orientation;
  317. typedef typename linalg_traits<M>::index_sorted index_sorted;
  318. static inline size_type nrows(const this_type &m) { return m.nr; }
  319. static inline size_type ncols(const this_type &m) { return m.nc; }
  320. static inline const_sub_row_type row(const const_row_iterator &it)
  321. { return conjugated(linalg_traits<M>::col(it.it)); }
  322. static inline const_row_iterator row_begin(const this_type &m)
  323. { return const_row_iterator(m.begin_); }
  324. static inline const_row_iterator row_end(const this_type &m)
  325. { return const_row_iterator(m.end_); }
  326. static inline const origin_type* origin(const this_type &m)
  327. { return m.origin; }
  328. static value_type access(const const_row_iterator &it, size_type i)
  329. { return gmm::conj(linalg_traits<M>::access(it.it, i)); }
  330. };
  331. ///@endcond
  332. }
  333. #endif // GMM_CONJUGATED_H__