GetFEM  5.4.3
gmm_conjugated.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2003-2020 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file gmm_conjugated.h
33  @author Yves Renard <Yves.Renard@insa-lyon.fr>
34  @date September 18, 2003.
35  @brief handle conjugation of complex matrices/vectors.
36 */
37 #ifndef GMM_CONJUGATED_H__
38 #define GMM_CONJUGATED_H__
39 
40 #include "gmm_def.h"
41 
42 namespace gmm {
43  ///@cond DOXY_SHOW_ALL_FUNCTIONS
44 
45  /* ********************************************************************* */
46  /* Conjugated references on vectors */
47  /* ********************************************************************* */
48 
49  template <typename IT> struct conjugated_const_iterator {
50  typedef typename std::iterator_traits<IT>::value_type value_type;
51  typedef typename std::iterator_traits<IT>::pointer pointer;
52  typedef typename std::iterator_traits<IT>::reference reference;
53  typedef typename std::iterator_traits<IT>::difference_type difference_type;
54  typedef typename std::iterator_traits<IT>::iterator_category
55  iterator_category;
56 
57  IT it;
58 
59  conjugated_const_iterator(void) {}
60  conjugated_const_iterator(const IT &i) : it(i) {}
61 
62  inline size_type index(void) const { return it.index(); }
63  conjugated_const_iterator operator ++(int)
64  { conjugated_const_iterator tmp = *this; ++it; return tmp; }
65  conjugated_const_iterator operator --(int)
66  { conjugated_const_iterator tmp = *this; --it; return tmp; }
67  conjugated_const_iterator &operator ++() { ++it; return *this; }
68  conjugated_const_iterator &operator --() { --it; return *this; }
69  conjugated_const_iterator &operator +=(difference_type i)
70  { it += i; return *this; }
71  conjugated_const_iterator &operator -=(difference_type i)
72  { it -= i; return *this; }
73  conjugated_const_iterator operator +(difference_type i) const
74  { conjugated_const_iterator itb = *this; return (itb += i); }
75  conjugated_const_iterator operator -(difference_type i) const
76  { conjugated_const_iterator itb = *this; return (itb -= i); }
77  difference_type operator -(const conjugated_const_iterator &i) const
78  { return difference_type(it - i.it); }
79 
80  value_type operator *() const { return gmm::conj(*it); }
81  value_type operator [](size_type ii) const { return gmm::conj(it[ii]); }
82 
83  bool operator ==(const conjugated_const_iterator &i) const
84  { return (i.it == it); }
85  bool operator !=(const conjugated_const_iterator &i) const
86  { return (i.it != it); }
87  bool operator < (const conjugated_const_iterator &i) const
88  { return (it < i.it); }
89  bool operator > (const conjugated_const_iterator &i) const
90  { return (it > i.it); }
91  bool operator >= (const conjugated_const_iterator &i) const
92  { return (it >= i.it); }
93  };
94 
95  template <typename V> struct conjugated_vector_const_ref {
96  typedef conjugated_vector_const_ref<V> this_type;
97  typedef typename linalg_traits<V>::value_type value_type;
98  typedef typename linalg_traits<V>::const_iterator iterator;
99  typedef typename linalg_traits<this_type>::reference reference;
100  typedef typename linalg_traits<this_type>::origin_type origin_type;
101 
102  iterator begin_, end_;
103  const origin_type *origin;
104  size_type size_;
105 
106  conjugated_vector_const_ref(const V &v)
107  : begin_(vect_const_begin(v)), end_(vect_const_end(v)),
108  origin(linalg_origin(v)),
109  size_(vect_size(v)) {}
110 
111  reference operator[](size_type i) const
112  { return gmm::conj(linalg_traits<V>::access(origin, begin_, end_, i)); }
113  };
114 
115  template <typename V> struct linalg_traits<conjugated_vector_const_ref<V> > {
116  typedef conjugated_vector_const_ref<V> this_type;
117  typedef typename linalg_traits<V>::origin_type origin_type;
118  typedef linalg_const is_reference;
119  typedef abstract_vector linalg_type;
120  typedef typename linalg_traits<V>::value_type value_type;
121  typedef value_type reference;
122  typedef abstract_null_type iterator;
123  typedef conjugated_const_iterator<typename
124  linalg_traits<V>::const_iterator> const_iterator;
125  typedef typename linalg_traits<V>::storage_type storage_type;
126  typedef typename linalg_traits<V>::index_sorted index_sorted;
127  static size_type size(const this_type &v) { return v.size_; }
128  static iterator begin(this_type &v) { return iterator(v.begin_); }
129  static const_iterator begin(const this_type &v)
130  { return const_iterator(v.begin_); }
131  static iterator end(this_type &v)
132  { return iterator(v.end_); }
133  static const_iterator end(const this_type &v)
134  { return const_iterator(v.end_); }
135  static value_type access(const origin_type *o, const const_iterator &it,
136  const const_iterator &ite, size_type i)
137  { return gmm::conj(linalg_traits<V>::access(o, it.it, ite.it, i)); }
138  static const origin_type* origin(const this_type &v) { return v.origin; }
139  };
140 
141  template<typename V> std::ostream &operator <<
142  (std::ostream &o, const conjugated_vector_const_ref<V>& m)
143  { gmm::write(o,m); return o; }
144 
145  /* ********************************************************************* */
146  /* Conjugated references on matrices */
147  /* ********************************************************************* */
148 
149  template <typename M> struct conjugated_row_const_iterator {
150  typedef conjugated_row_const_iterator<M> iterator;
151  typedef typename linalg_traits<M>::const_row_iterator ITER;
152  typedef typename linalg_traits<M>::value_type value_type;
153  typedef ptrdiff_t difference_type;
154  typedef size_t size_type;
155 
156  ITER it;
157 
158  iterator operator ++(int) { iterator tmp = *this; it++; return tmp; }
159  iterator operator --(int) { iterator tmp = *this; it--; return tmp; }
160  iterator &operator ++() { it++; return *this; }
161  iterator &operator --() { it--; return *this; }
162  iterator &operator +=(difference_type i) { it += i; return *this; }
163  iterator &operator -=(difference_type i) { it -= i; return *this; }
164  iterator operator +(difference_type i) const
165  { iterator itt = *this; return (itt += i); }
166  iterator operator -(difference_type i) const
167  { iterator itt = *this; return (itt -= i); }
168  difference_type operator -(const iterator &i) const
169  { return it - i.it; }
170 
171  ITER operator *() const { return it; }
172  ITER operator [](int i) { return it + i; }
173 
174  bool operator ==(const iterator &i) const { return (it == i.it); }
175  bool operator !=(const iterator &i) const { return !(i == *this); }
176  bool operator < (const iterator &i) const { return (it < i.it); }
177  bool operator > (const iterator &i) const { return (it > i.it); }
178  bool operator >=(const iterator &i) const { return (it >= i.it); }
179 
180  conjugated_row_const_iterator(void) {}
181  conjugated_row_const_iterator(const ITER &i) : it(i) { }
182 
183  };
184 
185  template <typename M> struct conjugated_row_matrix_const_ref {
186 
187  typedef conjugated_row_matrix_const_ref<M> this_type;
188  typedef typename linalg_traits<M>::const_row_iterator iterator;
189  typedef typename linalg_traits<M>::value_type value_type;
190  typedef typename linalg_traits<this_type>::origin_type origin_type;
191 
192  iterator begin_, end_;
193  const origin_type *origin;
194  size_type nr, nc;
195 
196  conjugated_row_matrix_const_ref(const M &m)
197  : begin_(mat_row_begin(m)), end_(mat_row_end(m)),
198  origin(linalg_origin(m)), nr(mat_ncols(m)), nc(mat_nrows(m)) {}
199 
200  value_type operator()(size_type i, size_type j) const
201  { return gmm::conj(linalg_traits<M>::access(begin_+j, i)); }
202  };
203 
204  template<typename M> std::ostream &operator <<
205  (std::ostream &o, const conjugated_row_matrix_const_ref<M>& m)
206  { gmm::write(o,m); return o; }
207 
208 
209  template <typename M> struct conjugated_col_const_iterator {
210  typedef conjugated_col_const_iterator<M> iterator;
211  typedef typename linalg_traits<M>::const_col_iterator ITER;
212  typedef typename linalg_traits<M>::value_type value_type;
213  typedef ptrdiff_t difference_type;
214  typedef size_t size_type;
215 
216  ITER it;
217 
218  iterator operator ++(int) { iterator tmp = *this; it++; return tmp; }
219  iterator operator --(int) { iterator tmp = *this; it--; return tmp; }
220  iterator &operator ++() { it++; return *this; }
221  iterator &operator --() { it--; return *this; }
222  iterator &operator +=(difference_type i) { it += i; return *this; }
223  iterator &operator -=(difference_type i) { it -= i; return *this; }
224  iterator operator +(difference_type i) const
225  { iterator itt = *this; return (itt += i); }
226  iterator operator -(difference_type i) const
227  { iterator itt = *this; return (itt -= i); }
228  difference_type operator -(const iterator &i) const
229  { return it - i.it; }
230 
231  ITER operator *() const { return it; }
232  ITER operator [](int i) { return it + i; }
233 
234  bool operator ==(const iterator &i) const { return (it == i.it); }
235  bool operator !=(const iterator &i) const { return !(i == *this); }
236  bool operator < (const iterator &i) const { return (it < i.it); }
237  bool operator > (const iterator &i) const { return (it > i.it); }
238  bool operator >=(const iterator &i) const { return (it >= i.it); }
239 
240  conjugated_col_const_iterator(void) {}
241  conjugated_col_const_iterator(const ITER &i) : it(i) { }
242 
243  };
244 
245  template <typename M> struct conjugated_col_matrix_const_ref {
246 
247  typedef conjugated_col_matrix_const_ref<M> this_type;
248  typedef typename linalg_traits<M>::const_col_iterator iterator;
249  typedef typename linalg_traits<M>::value_type value_type;
250  typedef typename linalg_traits<this_type>::origin_type origin_type;
251 
252  iterator begin_, end_;
253  const origin_type *origin;
254  size_type nr, nc;
255 
256  conjugated_col_matrix_const_ref(const M &m)
257  : begin_(mat_col_begin(m)), end_(mat_col_end(m)),
258  origin(linalg_origin(m)), nr(mat_ncols(m)), nc(mat_nrows(m)) {}
259 
260  value_type operator()(size_type i, size_type j) const
261  { return gmm::conj(linalg_traits<M>::access(begin_+i, j)); }
262  };
263 
264 
265 
266  template<typename M> std::ostream &operator <<
267  (std::ostream &o, const conjugated_col_matrix_const_ref<M>& m)
268  { gmm::write(o,m); return o; }
269 
270 
271  template <typename L, typename SO> struct conjugated_return__ {
272  typedef conjugated_row_matrix_const_ref<L> return_type;
273  };
274  template <typename L> struct conjugated_return__<L, col_major> {
275  typedef conjugated_col_matrix_const_ref<L> return_type;
276  };
277  template <typename L, typename T, typename LT> struct conjugated_return_ {
278  typedef const L & return_type;
279  };
280  template <typename L, typename T>
281  struct conjugated_return_<L, std::complex<T>, abstract_vector> {
282  typedef conjugated_vector_const_ref<L> return_type;
283  };
284  template <typename L, typename T>
285  struct conjugated_return_<L, T, abstract_matrix> {
286  typedef typename conjugated_return__<L,
287  typename principal_orientation_type<typename
288  linalg_traits<L>::sub_orientation>::potype
289  >::return_type return_type;
290  };
291  template <typename L> struct conjugated_return {
292  typedef typename
293  conjugated_return_<L, typename linalg_traits<L>::value_type,
294  typename linalg_traits<L>::linalg_type
295  >::return_type return_type;
296  };
297 
298  ///@endcond
299  /** return a conjugated view of the input matrix or vector. */
300  template <typename L> inline
301  typename conjugated_return<L>::return_type
302  conjugated(const L &v) {
303  return conjugated(v, typename linalg_traits<L>::value_type(),
304  typename linalg_traits<L>::linalg_type());
305  }
306  ///@cond DOXY_SHOW_ALL_FUNCTIONS
307 
308  template <typename L, typename T, typename LT> inline
309  const L & conjugated(const L &v, T, LT) { return v; }
310 
311  template <typename L, typename T> inline
312  conjugated_vector_const_ref<L> conjugated(const L &v, std::complex<T>,
313  abstract_vector)
314  { return conjugated_vector_const_ref<L>(v); }
315 
316  template <typename L, typename T> inline
317  typename conjugated_return__<L,
318  typename principal_orientation_type<typename
319  linalg_traits<L>::sub_orientation>::potype>::return_type
320  conjugated(const L &v, T, abstract_matrix) {
321  return conjugated(v, typename principal_orientation_type<typename
322  linalg_traits<L>::sub_orientation>::potype());
323  }
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 
329  template <typename L> inline
330  conjugated_col_matrix_const_ref<L> conjugated(const L &v, col_major)
331  { return conjugated_col_matrix_const_ref<L>(v); }
332 
333  template <typename M>
334  struct linalg_traits<conjugated_row_matrix_const_ref<M> > {
335  typedef conjugated_row_matrix_const_ref<M> this_type;
336  typedef typename linalg_traits<M>::origin_type origin_type;
337  typedef linalg_const is_reference;
338  typedef abstract_matrix linalg_type;
339  typedef typename linalg_traits<M>::value_type value_type;
340  typedef value_type reference;
341  typedef typename linalg_traits<M>::storage_type storage_type;
342  typedef typename org_type<typename linalg_traits<M>::const_sub_row_type>::t vector_type;
343  typedef conjugated_vector_const_ref<vector_type> sub_col_type;
344  typedef conjugated_vector_const_ref<vector_type> const_sub_col_type;
345  typedef conjugated_row_const_iterator<M> col_iterator;
346  typedef conjugated_row_const_iterator<M> const_col_iterator;
347  typedef abstract_null_type const_sub_row_type;
348  typedef abstract_null_type sub_row_type;
349  typedef abstract_null_type const_row_iterator;
350  typedef abstract_null_type row_iterator;
351  typedef col_major sub_orientation;
352  typedef typename linalg_traits<M>::index_sorted index_sorted;
353  static inline size_type ncols(const this_type &m) { return m.nc; }
354  static inline size_type nrows(const this_type &m) { return m.nr; }
355  static inline const_sub_col_type col(const const_col_iterator &it)
356  { return conjugated(linalg_traits<M>::row(it.it)); }
357  static inline const_col_iterator col_begin(const this_type &m)
358  { return const_col_iterator(m.begin_); }
359  static inline const_col_iterator col_end(const this_type &m)
360  { return const_col_iterator(m.end_); }
361  static inline const origin_type* origin(const this_type &m)
362  { return m.origin; }
363  static value_type access(const const_col_iterator &it, size_type i)
364  { return gmm::conj(linalg_traits<M>::access(it.it, i)); }
365  };
366 
367  template <typename M>
368  struct linalg_traits<conjugated_col_matrix_const_ref<M> > {
369  typedef conjugated_col_matrix_const_ref<M> this_type;
370  typedef typename linalg_traits<M>::origin_type origin_type;
371  typedef linalg_const is_reference;
372  typedef abstract_matrix linalg_type;
373  typedef typename linalg_traits<M>::value_type value_type;
374  typedef value_type reference;
375  typedef typename linalg_traits<M>::storage_type storage_type;
376  typedef typename org_type<typename linalg_traits<M>::const_sub_col_type>::t vector_type;
377  typedef conjugated_vector_const_ref<vector_type> sub_row_type;
378  typedef conjugated_vector_const_ref<vector_type> const_sub_row_type;
379  typedef conjugated_col_const_iterator<M> row_iterator;
380  typedef conjugated_col_const_iterator<M> const_row_iterator;
381  typedef abstract_null_type const_sub_col_type;
382  typedef abstract_null_type sub_col_type;
383  typedef abstract_null_type const_col_iterator;
384  typedef abstract_null_type col_iterator;
385  typedef row_major sub_orientation;
386  typedef typename linalg_traits<M>::index_sorted index_sorted;
387  static inline size_type nrows(const this_type &m) { return m.nr; }
388  static inline size_type ncols(const this_type &m) { return m.nc; }
389  static inline const_sub_row_type row(const const_row_iterator &it)
390  { return conjugated(linalg_traits<M>::col(it.it)); }
391  static inline const_row_iterator row_begin(const this_type &m)
392  { return const_row_iterator(m.begin_); }
393  static inline const_row_iterator row_end(const this_type &m)
394  { return const_row_iterator(m.end_); }
395  static inline const origin_type* origin(const this_type &m)
396  { return m.origin; }
397  static value_type access(const const_row_iterator &it, size_type i)
398  { return gmm::conj(linalg_traits<M>::access(it.it, i)); }
399  };
400 
401  ///@endcond
402 
403 
404 }
405 
406 #endif // GMM_CONJUGATED_H__
conjugated_return< L >::return_type conjugated(const L &v)
return a conjugated view of the input matrix or vector.
Basic definitions and tools of GMM.
rational_fraction< T > operator-(const polynomial< T > &P, const rational_fraction< T > &Q)
Subtract Q from P.
Definition: bgeot_poly.h:756
rational_fraction< T > operator+(const polynomial< T > &P, const rational_fraction< T > &Q)
Add Q to P.
Definition: bgeot_poly.h:749
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49