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