GetFEM  5.4.3
gmm_vector_to_matrix.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_vector_to_matrix.h
33  @author Yves Renard <Yves.Renard@insa-lyon.fr>
34  @date December 6, 2003.
35  @brief View vectors as row or column matrices. */
36 #ifndef GMM_VECTOR_TO_MATRIX_H__
37 #define GMM_VECTOR_TO_MATRIX_H__
38 
39 #include "gmm_interface.h"
40 
41 namespace gmm {
42 
43  /* ********************************************************************* */
44  /* row vector -> transform a vector in a (1, n) matrix. */
45  /* ********************************************************************* */
46 
47  template <typename PT> struct gen_row_vector {
48  typedef gen_row_vector<PT> this_type;
49  typedef typename std::iterator_traits<PT>::value_type V;
50  typedef V * CPT;
51  typedef typename std::iterator_traits<PT>::reference ref_V;
52  typedef typename linalg_traits<this_type>::reference reference;
53 
54  simple_vector_ref<PT> vec;
55 
56  reference operator()(size_type, size_type j) const { return vec[j]; }
57 
58  size_type nrows(void) const { return 1; }
59  size_type ncols(void) const { return vect_size(vec); }
60 
61  gen_row_vector(ref_V v) : vec(v) {}
62  gen_row_vector() {}
63  gen_row_vector(const gen_row_vector<CPT> &cr) : vec(cr.vec) {}
64  };
65 
66  template <typename PT>
67  struct gen_row_vector_iterator {
68  typedef gen_row_vector<PT> this_type;
69  typedef typename modifiable_pointer<PT>::pointer MPT;
70  typedef typename std::iterator_traits<PT>::value_type V;
71  typedef simple_vector_ref<PT> value_type;
72  typedef const simple_vector_ref<PT> *pointer;
73  typedef const simple_vector_ref<PT> &reference;
74  typedef ptrdiff_t difference_type;
75  typedef size_t size_type;
76  typedef std::random_access_iterator_tag iterator_category;
77  typedef gen_row_vector_iterator<PT> iterator;
78 
79  simple_vector_ref<PT> vec;
80  bool isend;
81 
82  iterator &operator ++() { isend = true; return *this; }
83  iterator &operator --() { isend = false; return *this; }
84  iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
85  iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
86  iterator &operator +=(difference_type i)
87  { if (i) isend = false; return *this; }
88  iterator &operator -=(difference_type i)
89  { if (i) isend = true; return *this; }
90  iterator operator +(difference_type i) const
91  { iterator itt = *this; return (itt += i); }
92  iterator operator -(difference_type i) const
93  { iterator itt = *this; return (itt -= i); }
94  difference_type operator -(const iterator &i) const {
95  return (isend == true) ? ((i.isend == true) ? 0 : 1)
96  : ((i.isend == true) ? -1 : 0);
97  }
98 
99  const simple_vector_ref<PT>& operator *() const { return vec; }
100  const simple_vector_ref<PT>& operator [](int i) { return vec; }
101 
102  bool operator ==(const iterator &i) const { return (isend == i.isend); }
103  bool operator !=(const iterator &i) const { return !(i == *this); }
104  bool operator < (const iterator &i) const { return (*this - i < 0); }
105  bool operator > (const iterator &i) const { return (*this - i > 0); }
106  bool operator >=(const iterator &i) const { return (*this - i >= 0); }
107 
108  gen_row_vector_iterator(void) {}
109  gen_row_vector_iterator(const gen_row_vector_iterator<MPT> &itm)
110  : vec(itm.vec), isend(itm.isend) {}
111  gen_row_vector_iterator(const gen_row_vector<PT> &m, bool iis_end)
112  : vec(m.vec), isend(iis_end) { }
113 
114  };
115 
116  template <typename PT>
117  struct linalg_traits<gen_row_vector<PT> > {
118  typedef gen_row_vector<PT> this_type;
119  typedef typename std::iterator_traits<PT>::value_type V;
120  typedef typename which_reference<PT>::is_reference is_reference;
121  typedef abstract_matrix linalg_type;
122  typedef typename linalg_traits<V>::origin_type origin_type;
123  typedef typename select_ref<const origin_type *, origin_type *,
124  PT>::ref_type porigin_type;
125  typedef typename linalg_traits<V>::value_type value_type;
126  typedef typename select_ref<value_type,
127  typename linalg_traits<V>::reference, PT>::ref_type reference;
128  typedef abstract_null_type sub_col_type;
129  typedef abstract_null_type col_iterator;
130  typedef abstract_null_type const_sub_col_type;
131  typedef abstract_null_type const_col_iterator;
132  typedef simple_vector_ref<const V *> const_sub_row_type;
133  typedef typename select_ref<abstract_null_type,
134  simple_vector_ref<V *>, PT>::ref_type sub_row_type;
135  typedef gen_row_vector_iterator<typename const_pointer<PT>::pointer>
136  const_row_iterator;
137  typedef typename select_ref<abstract_null_type,
138  gen_row_vector_iterator<PT>, PT>::ref_type row_iterator;
139  typedef typename linalg_traits<V>::storage_type storage_type;
140  typedef row_major sub_orientation;
141  typedef typename linalg_traits<V>::index_sorted index_sorted;
142  static size_type nrows(const this_type &) { return 1; }
143  static size_type ncols(const this_type &m) { return m.ncols(); }
144  static const_sub_row_type row(const const_row_iterator &it) { return *it; }
145  static sub_row_type row(const row_iterator &it) { return *it; }
146  static const_row_iterator row_begin(const this_type &m)
147  { return const_row_iterator(m, false); }
148  static row_iterator row_begin(this_type &m)
149  { return row_iterator(m, false); }
150  static const_row_iterator row_end(const this_type &m)
151  { return const_row_iterator(m, true); }
152  static row_iterator row_end(this_type &m)
153  { return row_iterator(m, true); }
154  static origin_type* origin(this_type &m) { return m.vec.origin; }
155  static const origin_type* origin(const this_type &m)
156  { return m.vec.origin; }
157  static void do_clear(this_type &m)
158  { clear(row(mat_row_begin(m))); }
159  static value_type access(const const_row_iterator &itrow, size_type i)
160  { return itrow.vec[i]; }
161  static reference access(const row_iterator &itrow, size_type i)
162  { return itrow.vec[i]; }
163  };
164 
165  template <typename PT>
166  std::ostream &operator <<(std::ostream &o, const gen_row_vector<PT>& m)
167  { gmm::write(o,m); return o; }
168 
169  /* ********************************************************************* */
170  /* col vector -> transform a vector in a (n, 1) matrix. */
171  /* ********************************************************************* */
172 
173  template <typename PT> struct gen_col_vector {
174  typedef gen_col_vector<PT> this_type;
175  typedef typename std::iterator_traits<PT>::value_type V;
176  typedef V * CPT;
177  typedef typename std::iterator_traits<PT>::reference ref_V;
178  typedef typename linalg_traits<this_type>::reference reference;
179 
180  simple_vector_ref<PT> vec;
181 
182  reference operator()(size_type i, size_type) const { return vec[i]; }
183 
184  size_type ncols(void) const { return 1; }
185  size_type nrows(void) const { return vect_size(vec); }
186 
187  gen_col_vector(ref_V v) : vec(v) {}
188  gen_col_vector() {}
189  gen_col_vector(const gen_col_vector<CPT> &cr) : vec(cr.vec) {}
190  };
191 
192  template <typename PT>
193  struct gen_col_vector_iterator {
194  typedef gen_col_vector<PT> this_type;
195  typedef typename modifiable_pointer<PT>::pointer MPT;
196  typedef typename std::iterator_traits<PT>::value_type V;
197  typedef simple_vector_ref<PT> value_type;
198  typedef const simple_vector_ref<PT> *pointer;
199  typedef const simple_vector_ref<PT> &reference;
200  typedef ptrdiff_t difference_type;
201  typedef size_t size_type;
202  typedef std::random_access_iterator_tag iterator_category;
203  typedef gen_col_vector_iterator<PT> iterator;
204 
205  simple_vector_ref<PT> vec;
206  bool isend;
207 
208  iterator &operator ++() { isend = true; return *this; }
209  iterator &operator --() { isend = false; return *this; }
210  iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
211  iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
212  iterator &operator +=(difference_type i)
213  { if (i) isend = false; return *this; }
214  iterator &operator -=(difference_type i)
215  { if (i) isend = true; return *this; }
216  iterator operator +(difference_type i) const
217  { iterator itt = *this; return (itt += i); }
218  iterator operator -(difference_type i) const
219  { iterator itt = *this; return (itt -= i); }
220  difference_type operator -(const iterator &i) const {
221  return (isend == true) ? ((i.isend == true) ? 0 : 1)
222  : ((i.isend == true) ? -1 : 0);
223  }
224 
225  const simple_vector_ref<PT>& operator *() const { return vec; }
226  const simple_vector_ref<PT>& operator [](int i) { return vec; }
227 
228  bool operator ==(const iterator &i) const { return (isend == i.isend); }
229  bool operator !=(const iterator &i) const { return !(i == *this); }
230  bool operator < (const iterator &i) const { return (*this - i < 0); }
231  bool operator > (const iterator &i) const { return (*this - i > 0); }
232  bool operator >=(const iterator &i) const { return (*this - i >= 0); }
233 
234  gen_col_vector_iterator(void) {}
235  gen_col_vector_iterator(const gen_col_vector_iterator<MPT> &itm)
236  : vec(itm.vec), isend(itm.isend) {}
237  gen_col_vector_iterator(const gen_col_vector<PT> &m, bool iis_end)
238  : vec(m.vec), isend(iis_end) { }
239 
240  };
241 
242  template <typename PT>
243  struct linalg_traits<gen_col_vector<PT> > {
244  typedef gen_col_vector<PT> this_type;
245  typedef typename std::iterator_traits<PT>::value_type V;
246  typedef typename which_reference<PT>::is_reference is_reference;
247  typedef abstract_matrix linalg_type;
248  typedef typename linalg_traits<V>::origin_type origin_type;
249  typedef typename select_ref<const origin_type *, origin_type *,
250  PT>::ref_type porigin_type;
251  typedef typename linalg_traits<V>::value_type value_type;
252  typedef typename select_ref<value_type,
253  typename linalg_traits<V>::reference, PT>::ref_type reference;
254  typedef abstract_null_type sub_row_type;
255  typedef abstract_null_type row_iterator;
256  typedef abstract_null_type const_sub_row_type;
257  typedef abstract_null_type const_row_iterator;
258  typedef simple_vector_ref<const V *> const_sub_col_type;
259  typedef typename select_ref<abstract_null_type,
260  simple_vector_ref<V *>, PT>::ref_type sub_col_type;
261  typedef gen_col_vector_iterator<typename const_pointer<PT>::pointer>
262  const_col_iterator;
263  typedef typename select_ref<abstract_null_type,
264  gen_col_vector_iterator<PT>, PT>::ref_type col_iterator;
265  typedef typename linalg_traits<V>::storage_type storage_type;
266  typedef col_major sub_orientation;
267  typedef typename linalg_traits<V>::index_sorted index_sorted;
268  static size_type ncols(const this_type &) { return 1; }
269  static size_type nrows(const this_type &m) { return m.nrows(); }
270  static const_sub_col_type col(const const_col_iterator &it) { return *it; }
271  static sub_col_type col(const col_iterator &it) { return *it; }
272  static const_col_iterator col_begin(const this_type &m)
273  { return const_col_iterator(m, false); }
274  static col_iterator col_begin(this_type &m)
275  { return col_iterator(m, false); }
276  static const_col_iterator col_end(const this_type &m)
277  { return const_col_iterator(m, true); }
278  static col_iterator col_end(this_type &m)
279  { return col_iterator(m, true); }
280  static origin_type* origin(this_type &m) { return m.vec.origin; }
281  static const origin_type* origin(const this_type &m)
282  { return m.vec.origin; }
283  static void do_clear(this_type &m)
284  { clear(col(mat_col_begin(m))); }
285  static value_type access(const const_col_iterator &itcol, size_type i)
286  { return itcol.vec[i]; }
287  static reference access(const col_iterator &itcol, size_type i)
288  { return itcol.vec[i]; }
289  };
290 
291  template <typename PT>
292  std::ostream &operator <<(std::ostream &o, const gen_col_vector<PT>& m)
293  { gmm::write(o,m); return o; }
294 
295  /* ******************************************************************** */
296  /* col and row vectors */
297  /* ******************************************************************** */
298 
299 
300  template <class V> inline
301  typename select_return< gen_row_vector<const V *>, gen_row_vector<V *>,
302  const V *>::return_type
303  row_vector(const V& v) {
304  return typename select_return< gen_row_vector<const V *>,
305  gen_row_vector<V *>, const V *>::return_type(linalg_cast(v));
306  }
307 
308  template <class V> inline
309  typename select_return< gen_row_vector<const V *>, gen_row_vector<V *>,
310  V *>::return_type
311  row_vector(V& v) {
312  return typename select_return< gen_row_vector<const V *>,
313  gen_row_vector<V *>, V *>::return_type(linalg_cast(v));
314  }
315 
316  template <class V> inline gen_row_vector<const V *>
317  const_row_vector(V& v)
318  { return gen_row_vector<const V *>(v); }
319 
320 
321  template <class V> inline
322  typename select_return< gen_col_vector<const V *>, gen_col_vector<V *>,
323  const V *>::return_type
324  col_vector(const V& v) {
325  return typename select_return< gen_col_vector<const V *>,
326  gen_col_vector<V *>, const V *>::return_type(linalg_cast(v));
327  }
328 
329  template <class V> inline
330  typename select_return< gen_col_vector<const V *>, gen_col_vector<V *>,
331  V *>::return_type
332  col_vector(V& v) {
333  return typename select_return< gen_col_vector<const V *>,
334  gen_col_vector<V *>, V *>::return_type(linalg_cast(v));
335  }
336 
337  template <class V> inline gen_col_vector<const V *>
338  const_col_vector(V& v)
339  { return gen_col_vector<const V *>(v); }
340 
341 
342 }
343 
344 #endif // GMM_VECTOR_TO_MATRIX_H__
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:59
gmm interface for STL vectors.
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