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.

396 lines
17 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2003-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_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>
  164. struct linalg_traits<conjugated_row_matrix_const_ref<M> > {
  165. typedef conjugated_row_matrix_const_ref<M> this_type;
  166. typedef typename linalg_traits<M>::origin_type origin_type;
  167. typedef linalg_const is_reference;
  168. typedef abstract_matrix linalg_type;
  169. typedef typename linalg_traits<M>::value_type value_type;
  170. typedef value_type reference;
  171. typedef typename linalg_traits<M>::storage_type storage_type;
  172. typedef typename linalg_traits<M>::const_sub_row_type vector_type;
  173. typedef conjugated_vector_const_ref<vector_type> sub_col_type;
  174. typedef conjugated_vector_const_ref<vector_type> const_sub_col_type;
  175. typedef conjugated_row_const_iterator<M> col_iterator;
  176. typedef conjugated_row_const_iterator<M> const_col_iterator;
  177. typedef abstract_null_type const_sub_row_type;
  178. typedef abstract_null_type sub_row_type;
  179. typedef abstract_null_type const_row_iterator;
  180. typedef abstract_null_type row_iterator;
  181. typedef col_major sub_orientation;
  182. typedef typename linalg_traits<M>::index_sorted index_sorted;
  183. static inline size_type ncols(const this_type &m) { return m.nc; }
  184. static inline size_type nrows(const this_type &m) { return m.nr; }
  185. static inline const_sub_col_type col(const const_col_iterator &it)
  186. { return conjugated(linalg_traits<M>::row(it.it)); }
  187. static inline const_col_iterator col_begin(const this_type &m)
  188. { return const_col_iterator(m.begin_); }
  189. static inline const_col_iterator col_end(const this_type &m)
  190. { return const_col_iterator(m.end_); }
  191. static inline const origin_type* origin(const this_type &m)
  192. { return m.origin; }
  193. static value_type access(const const_col_iterator &it, size_type i)
  194. { return gmm::conj(linalg_traits<M>::access(it.it, i)); }
  195. };
  196. template<typename M> std::ostream &operator <<
  197. (std::ostream &o, const conjugated_row_matrix_const_ref<M>& m)
  198. { gmm::write(o,m); return o; }
  199. template <typename M> struct conjugated_col_const_iterator {
  200. typedef conjugated_col_const_iterator<M> iterator;
  201. typedef typename linalg_traits<M>::const_col_iterator ITER;
  202. typedef typename linalg_traits<M>::value_type value_type;
  203. typedef ptrdiff_t difference_type;
  204. typedef size_t size_type;
  205. ITER it;
  206. iterator operator ++(int) { iterator tmp = *this; it++; return tmp; }
  207. iterator operator --(int) { iterator tmp = *this; it--; return tmp; }
  208. iterator &operator ++() { it++; return *this; }
  209. iterator &operator --() { it--; return *this; }
  210. iterator &operator +=(difference_type i) { it += i; return *this; }
  211. iterator &operator -=(difference_type i) { it -= i; return *this; }
  212. iterator operator +(difference_type i) const
  213. { iterator itt = *this; return (itt += i); }
  214. iterator operator -(difference_type i) const
  215. { iterator itt = *this; return (itt -= i); }
  216. difference_type operator -(const iterator &i) const
  217. { return it - i.it; }
  218. ITER operator *() const { return it; }
  219. ITER operator [](int i) { return it + i; }
  220. bool operator ==(const iterator &i) const { return (it == i.it); }
  221. bool operator !=(const iterator &i) const { return !(i == *this); }
  222. bool operator < (const iterator &i) const { return (it < i.it); }
  223. conjugated_col_const_iterator(void) {}
  224. conjugated_col_const_iterator(const ITER &i) : it(i) { }
  225. };
  226. template <typename M> struct conjugated_col_matrix_const_ref {
  227. typedef conjugated_col_matrix_const_ref<M> this_type;
  228. typedef typename linalg_traits<M>::const_col_iterator iterator;
  229. typedef typename linalg_traits<M>::value_type value_type;
  230. typedef typename linalg_traits<this_type>::origin_type origin_type;
  231. iterator begin_, end_;
  232. const origin_type *origin;
  233. size_type nr, nc;
  234. conjugated_col_matrix_const_ref(const M &m)
  235. : begin_(mat_col_begin(m)), end_(mat_col_end(m)),
  236. origin(linalg_origin(m)), nr(mat_ncols(m)), nc(mat_nrows(m)) {}
  237. value_type operator()(size_type i, size_type j) const
  238. { return gmm::conj(linalg_traits<M>::access(begin_+i, j)); }
  239. };
  240. template <typename M>
  241. struct linalg_traits<conjugated_col_matrix_const_ref<M> > {
  242. typedef conjugated_col_matrix_const_ref<M> this_type;
  243. typedef typename linalg_traits<M>::origin_type origin_type;
  244. typedef linalg_const is_reference;
  245. typedef abstract_matrix linalg_type;
  246. typedef typename linalg_traits<M>::value_type value_type;
  247. typedef value_type reference;
  248. typedef typename linalg_traits<M>::storage_type storage_type;
  249. typedef typename linalg_traits<M>::const_sub_col_type vector_type;
  250. typedef conjugated_vector_const_ref<vector_type> sub_row_type;
  251. typedef conjugated_vector_const_ref<vector_type> const_sub_row_type;
  252. typedef conjugated_col_const_iterator<M> row_iterator;
  253. typedef conjugated_col_const_iterator<M> const_row_iterator;
  254. typedef abstract_null_type const_sub_col_type;
  255. typedef abstract_null_type sub_col_type;
  256. typedef abstract_null_type const_col_iterator;
  257. typedef abstract_null_type col_iterator;
  258. typedef row_major sub_orientation;
  259. typedef typename linalg_traits<M>::index_sorted index_sorted;
  260. static inline size_type nrows(const this_type &m) { return m.nr; }
  261. static inline size_type ncols(const this_type &m) { return m.nc; }
  262. static inline const_sub_row_type row(const const_row_iterator &it)
  263. { return conjugated(linalg_traits<M>::col(it.it)); }
  264. static inline const_row_iterator row_begin(const this_type &m)
  265. { return const_row_iterator(m.begin_); }
  266. static inline const_row_iterator row_end(const this_type &m)
  267. { return const_row_iterator(m.end_); }
  268. static inline const origin_type* origin(const this_type &m)
  269. { return m.origin; }
  270. static value_type access(const const_row_iterator &it, size_type i)
  271. { return gmm::conj(linalg_traits<M>::access(it.it, i)); }
  272. };
  273. template<typename M> std::ostream &operator <<
  274. (std::ostream &o, const conjugated_col_matrix_const_ref<M>& m)
  275. { gmm::write(o,m); return o; }
  276. template <typename L, typename SO> struct conjugated_return__ {
  277. typedef conjugated_row_matrix_const_ref<L> return_type;
  278. };
  279. template <typename L> struct conjugated_return__<L, col_major> {
  280. typedef conjugated_col_matrix_const_ref<L> return_type;
  281. };
  282. template <typename L, typename T, typename LT> struct conjugated_return_ {
  283. typedef const L & return_type;
  284. };
  285. template <typename L, typename T>
  286. struct conjugated_return_<L, std::complex<T>, abstract_vector> {
  287. typedef conjugated_vector_const_ref<L> return_type;
  288. };
  289. template <typename L, typename T>
  290. struct conjugated_return_<L, T, abstract_matrix> {
  291. typedef typename conjugated_return__<L,
  292. typename principal_orientation_type<typename
  293. linalg_traits<L>::sub_orientation>::potype
  294. >::return_type return_type;
  295. };
  296. template <typename L> struct conjugated_return {
  297. typedef typename
  298. conjugated_return_<L, typename linalg_traits<L>::value_type,
  299. typename linalg_traits<L>::linalg_type
  300. >::return_type return_type;
  301. };
  302. ///@endcond
  303. /** return a conjugated view of the input matrix or vector. */
  304. template <typename L> inline
  305. typename conjugated_return<L>::return_type
  306. conjugated(const L &v) {
  307. return conjugated(v, typename linalg_traits<L>::value_type(),
  308. typename linalg_traits<L>::linalg_type());
  309. }
  310. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  311. template <typename L, typename T, typename LT> inline
  312. const L & conjugated(const L &v, T, LT) { return v; }
  313. template <typename L, typename T> inline
  314. conjugated_vector_const_ref<L> conjugated(const L &v, std::complex<T>,
  315. abstract_vector)
  316. { return conjugated_vector_const_ref<L>(v); }
  317. template <typename L, typename T> inline
  318. typename conjugated_return__<L,
  319. typename principal_orientation_type<typename
  320. linalg_traits<L>::sub_orientation>::potype>::return_type
  321. conjugated(const L &v, T, abstract_matrix) {
  322. return conjugated(v, typename principal_orientation_type<typename
  323. linalg_traits<L>::sub_orientation>::potype());
  324. }
  325. template <typename L> inline
  326. conjugated_row_matrix_const_ref<L> conjugated(const L &v, row_major)
  327. { return conjugated_row_matrix_const_ref<L>(v); }
  328. template <typename L> inline
  329. conjugated_col_matrix_const_ref<L> conjugated(const L &v, col_major)
  330. { return conjugated_col_matrix_const_ref<L>(v); }
  331. ///@endcond
  332. }
  333. #endif // GMM_CONJUGATED_H__