GetFEM  5.4.2
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  };
90 
91  template <typename V> struct conjugated_vector_const_ref {
92  typedef conjugated_vector_const_ref<V> this_type;
93  typedef typename linalg_traits<V>::value_type value_type;
94  typedef typename linalg_traits<V>::const_iterator iterator;
95  typedef typename linalg_traits<this_type>::reference reference;
96  typedef typename linalg_traits<this_type>::origin_type origin_type;
97 
98  iterator begin_, end_;
99  const origin_type *origin;
100  size_type size_;
101 
102  conjugated_vector_const_ref(const V &v)
103  : begin_(vect_const_begin(v)), end_(vect_const_end(v)),
104  origin(linalg_origin(v)),
105  size_(vect_size(v)) {}
106 
107  reference operator[](size_type i) const
108  { return gmm::conj(linalg_traits<V>::access(origin, begin_, end_, i)); }
109  };
110 
111  template <typename V> struct linalg_traits<conjugated_vector_const_ref<V> > {
112  typedef conjugated_vector_const_ref<V> this_type;
113  typedef typename linalg_traits<V>::origin_type origin_type;
114  typedef linalg_const is_reference;
115  typedef abstract_vector linalg_type;
116  typedef typename linalg_traits<V>::value_type value_type;
117  typedef value_type reference;
118  typedef abstract_null_type iterator;
119  typedef conjugated_const_iterator<typename
120  linalg_traits<V>::const_iterator> const_iterator;
121  typedef typename linalg_traits<V>::storage_type storage_type;
122  typedef typename linalg_traits<V>::index_sorted index_sorted;
123  static size_type size(const this_type &v) { return v.size_; }
124  static iterator begin(this_type &v) { return iterator(v.begin_); }
125  static const_iterator begin(const this_type &v)
126  { return const_iterator(v.begin_); }
127  static iterator end(this_type &v)
128  { return iterator(v.end_); }
129  static const_iterator end(const this_type &v)
130  { return const_iterator(v.end_); }
131  static value_type access(const origin_type *o, const const_iterator &it,
132  const const_iterator &ite, size_type i)
133  { return gmm::conj(linalg_traits<V>::access(o, it.it, ite.it, i)); }
134  static const origin_type* origin(const this_type &v) { return v.origin; }
135  };
136 
137  template<typename V> std::ostream &operator <<
138  (std::ostream &o, const conjugated_vector_const_ref<V>& m)
139  { gmm::write(o,m); return o; }
140 
141  /* ********************************************************************* */
142  /* Conjugated references on matrices */
143  /* ********************************************************************* */
144 
145  template <typename M> struct conjugated_row_const_iterator {
146  typedef conjugated_row_const_iterator<M> iterator;
147  typedef typename linalg_traits<M>::const_row_iterator ITER;
148  typedef typename linalg_traits<M>::value_type value_type;
149  typedef ptrdiff_t difference_type;
150  typedef size_t size_type;
151 
152  ITER it;
153 
154  iterator operator ++(int) { iterator tmp = *this; it++; return tmp; }
155  iterator operator --(int) { iterator tmp = *this; it--; return tmp; }
156  iterator &operator ++() { it++; return *this; }
157  iterator &operator --() { it--; return *this; }
158  iterator &operator +=(difference_type i) { it += i; return *this; }
159  iterator &operator -=(difference_type i) { it -= i; return *this; }
160  iterator operator +(difference_type i) const
161  { iterator itt = *this; return (itt += i); }
162  iterator operator -(difference_type i) const
163  { iterator itt = *this; return (itt -= i); }
164  difference_type operator -(const iterator &i) const
165  { return it - i.it; }
166 
167  ITER operator *() const { return it; }
168  ITER operator [](int i) { return it + i; }
169 
170  bool operator ==(const iterator &i) const { return (it == i.it); }
171  bool operator !=(const iterator &i) const { return !(i == *this); }
172  bool operator < (const iterator &i) const { return (it < i.it); }
173 
174  conjugated_row_const_iterator(void) {}
175  conjugated_row_const_iterator(const ITER &i) : it(i) { }
176 
177  };
178 
179  template <typename M> struct conjugated_row_matrix_const_ref {
180 
181  typedef conjugated_row_matrix_const_ref<M> this_type;
182  typedef typename linalg_traits<M>::const_row_iterator iterator;
183  typedef typename linalg_traits<M>::value_type value_type;
184  typedef typename linalg_traits<this_type>::origin_type origin_type;
185 
186  iterator begin_, end_;
187  const origin_type *origin;
188  size_type nr, nc;
189 
190  conjugated_row_matrix_const_ref(const M &m)
191  : begin_(mat_row_begin(m)), end_(mat_row_end(m)),
192  origin(linalg_origin(m)), nr(mat_ncols(m)), nc(mat_nrows(m)) {}
193 
194  value_type operator()(size_type i, size_type j) const
195  { return gmm::conj(linalg_traits<M>::access(begin_+j, i)); }
196  };
197 
198  template<typename M> std::ostream &operator <<
199  (std::ostream &o, const conjugated_row_matrix_const_ref<M>& m)
200  { gmm::write(o,m); return o; }
201 
202 
203  template <typename M> struct conjugated_col_const_iterator {
204  typedef conjugated_col_const_iterator<M> iterator;
205  typedef typename linalg_traits<M>::const_col_iterator ITER;
206  typedef typename linalg_traits<M>::value_type value_type;
207  typedef ptrdiff_t difference_type;
208  typedef size_t size_type;
209 
210  ITER it;
211 
212  iterator operator ++(int) { iterator tmp = *this; it++; return tmp; }
213  iterator operator --(int) { iterator tmp = *this; it--; return tmp; }
214  iterator &operator ++() { it++; return *this; }
215  iterator &operator --() { it--; return *this; }
216  iterator &operator +=(difference_type i) { it += i; return *this; }
217  iterator &operator -=(difference_type i) { it -= i; return *this; }
218  iterator operator +(difference_type i) const
219  { iterator itt = *this; return (itt += i); }
220  iterator operator -(difference_type i) const
221  { iterator itt = *this; return (itt -= i); }
222  difference_type operator -(const iterator &i) const
223  { return it - i.it; }
224 
225  ITER operator *() const { return it; }
226  ITER operator [](int i) { return it + i; }
227 
228  bool operator ==(const iterator &i) const { return (it == i.it); }
229  bool operator !=(const iterator &i) const { return !(i == *this); }
230  bool operator < (const iterator &i) const { return (it < i.it); }
231 
232  conjugated_col_const_iterator(void) {}
233  conjugated_col_const_iterator(const ITER &i) : it(i) { }
234 
235  };
236 
237  template <typename M> struct conjugated_col_matrix_const_ref {
238 
239  typedef conjugated_col_matrix_const_ref<M> this_type;
240  typedef typename linalg_traits<M>::const_col_iterator iterator;
241  typedef typename linalg_traits<M>::value_type value_type;
242  typedef typename linalg_traits<this_type>::origin_type origin_type;
243 
244  iterator begin_, end_;
245  const origin_type *origin;
246  size_type nr, nc;
247 
248  conjugated_col_matrix_const_ref(const M &m)
249  : begin_(mat_col_begin(m)), end_(mat_col_end(m)),
250  origin(linalg_origin(m)), nr(mat_ncols(m)), nc(mat_nrows(m)) {}
251 
252  value_type operator()(size_type i, size_type j) const
253  { return gmm::conj(linalg_traits<M>::access(begin_+i, j)); }
254  };
255 
256 
257 
258  template<typename M> std::ostream &operator <<
259  (std::ostream &o, const conjugated_col_matrix_const_ref<M>& m)
260  { gmm::write(o,m); return o; }
261 
262 
263  template <typename L, typename SO> struct conjugated_return__ {
264  typedef conjugated_row_matrix_const_ref<L> return_type;
265  };
266  template <typename L> struct conjugated_return__<L, col_major> {
267  typedef conjugated_col_matrix_const_ref<L> return_type;
268  };
269  template <typename L, typename T, typename LT> struct conjugated_return_ {
270  typedef const L & return_type;
271  };
272  template <typename L, typename T>
273  struct conjugated_return_<L, std::complex<T>, abstract_vector> {
274  typedef conjugated_vector_const_ref<L> return_type;
275  };
276  template <typename L, typename T>
277  struct conjugated_return_<L, T, abstract_matrix> {
278  typedef typename conjugated_return__<L,
279  typename principal_orientation_type<typename
280  linalg_traits<L>::sub_orientation>::potype
281  >::return_type return_type;
282  };
283  template <typename L> struct conjugated_return {
284  typedef typename
285  conjugated_return_<L, typename linalg_traits<L>::value_type,
286  typename linalg_traits<L>::linalg_type
287  >::return_type return_type;
288  };
289 
290  ///@endcond
291  /** return a conjugated view of the input matrix or vector. */
292  template <typename L> inline
293  typename conjugated_return<L>::return_type
294  conjugated(const L &v) {
295  return conjugated(v, typename linalg_traits<L>::value_type(),
296  typename linalg_traits<L>::linalg_type());
297  }
298  ///@cond DOXY_SHOW_ALL_FUNCTIONS
299 
300  template <typename L, typename T, typename LT> inline
301  const L & conjugated(const L &v, T, LT) { return v; }
302 
303  template <typename L, typename T> inline
304  conjugated_vector_const_ref<L> conjugated(const L &v, std::complex<T>,
305  abstract_vector)
306  { return conjugated_vector_const_ref<L>(v); }
307 
308  template <typename L, typename T> inline
309  typename conjugated_return__<L,
310  typename principal_orientation_type<typename
311  linalg_traits<L>::sub_orientation>::potype>::return_type
312  conjugated(const L &v, T, abstract_matrix) {
313  return conjugated(v, typename principal_orientation_type<typename
314  linalg_traits<L>::sub_orientation>::potype());
315  }
316 
317  template <typename L> inline
318  conjugated_row_matrix_const_ref<L> conjugated(const L &v, row_major)
319  { return conjugated_row_matrix_const_ref<L>(v); }
320 
321  template <typename L> inline
322  conjugated_col_matrix_const_ref<L> conjugated(const L &v, col_major)
323  { return conjugated_col_matrix_const_ref<L>(v); }
324 
325  template <typename M>
326  struct linalg_traits<conjugated_row_matrix_const_ref<M> > {
327  typedef conjugated_row_matrix_const_ref<M> this_type;
328  typedef typename linalg_traits<M>::origin_type origin_type;
329  typedef linalg_const is_reference;
330  typedef abstract_matrix linalg_type;
331  typedef typename linalg_traits<M>::value_type value_type;
332  typedef value_type reference;
333  typedef typename linalg_traits<M>::storage_type storage_type;
334  typedef typename org_type<typename linalg_traits<M>::const_sub_row_type>::t vector_type;
335  typedef conjugated_vector_const_ref<vector_type> sub_col_type;
336  typedef conjugated_vector_const_ref<vector_type> const_sub_col_type;
337  typedef conjugated_row_const_iterator<M> col_iterator;
338  typedef conjugated_row_const_iterator<M> const_col_iterator;
339  typedef abstract_null_type const_sub_row_type;
340  typedef abstract_null_type sub_row_type;
341  typedef abstract_null_type const_row_iterator;
342  typedef abstract_null_type row_iterator;
343  typedef col_major sub_orientation;
344  typedef typename linalg_traits<M>::index_sorted index_sorted;
345  static inline size_type ncols(const this_type &m) { return m.nc; }
346  static inline size_type nrows(const this_type &m) { return m.nr; }
347  static inline const_sub_col_type col(const const_col_iterator &it)
348  { return conjugated(linalg_traits<M>::row(it.it)); }
349  static inline const_col_iterator col_begin(const this_type &m)
350  { return const_col_iterator(m.begin_); }
351  static inline const_col_iterator col_end(const this_type &m)
352  { return const_col_iterator(m.end_); }
353  static inline const origin_type* origin(const this_type &m)
354  { return m.origin; }
355  static value_type access(const const_col_iterator &it, size_type i)
356  { return gmm::conj(linalg_traits<M>::access(it.it, i)); }
357  };
358 
359  template <typename M>
360  struct linalg_traits<conjugated_col_matrix_const_ref<M> > {
361  typedef conjugated_col_matrix_const_ref<M> this_type;
362  typedef typename linalg_traits<M>::origin_type origin_type;
363  typedef linalg_const is_reference;
364  typedef abstract_matrix linalg_type;
365  typedef typename linalg_traits<M>::value_type value_type;
366  typedef value_type reference;
367  typedef typename linalg_traits<M>::storage_type storage_type;
368  typedef typename org_type<typename linalg_traits<M>::const_sub_col_type>::t vector_type;
369  typedef conjugated_vector_const_ref<vector_type> sub_row_type;
370  typedef conjugated_vector_const_ref<vector_type> const_sub_row_type;
371  typedef conjugated_col_const_iterator<M> row_iterator;
372  typedef conjugated_col_const_iterator<M> const_row_iterator;
373  typedef abstract_null_type const_sub_col_type;
374  typedef abstract_null_type sub_col_type;
375  typedef abstract_null_type const_col_iterator;
376  typedef abstract_null_type col_iterator;
377  typedef row_major sub_orientation;
378  typedef typename linalg_traits<M>::index_sorted index_sorted;
379  static inline size_type nrows(const this_type &m) { return m.nr; }
380  static inline size_type ncols(const this_type &m) { return m.nc; }
381  static inline const_sub_row_type row(const const_row_iterator &it)
382  { return conjugated(linalg_traits<M>::col(it.it)); }
383  static inline const_row_iterator row_begin(const this_type &m)
384  { return const_row_iterator(m.begin_); }
385  static inline const_row_iterator row_end(const this_type &m)
386  { return const_row_iterator(m.end_); }
387  static inline const origin_type* origin(const this_type &m)
388  { return m.origin; }
389  static value_type access(const const_row_iterator &it, size_type i)
390  { return gmm::conj(linalg_traits<M>::access(it.it, i)); }
391  };
392 
393  ///@endcond
394 
395 
396 }
397 
398 #endif // GMM_CONJUGATED_H__
bgeot::size_type
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
gmm_def.h
Basic definitions and tools of GMM.
bgeot::operator+
rational_fraction< T > operator+(const polynomial< T > &P, const rational_fraction< T > &Q)
Add Q to P.
Definition: bgeot_poly.h:749
gmm::conjugated
conjugated_return< L >::return_type conjugated(const L &v)
return a conjugated view of the input matrix or vector.
Definition: gmm_conjugated.h:294
bgeot::operator-
rational_fraction< T > operator-(const polynomial< T > &P, const rational_fraction< T > &Q)
Subtract Q from P.
Definition: bgeot_poly.h:756