GetFEM  5.4.2
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 
106  gen_row_vector_iterator(void) {}
107  gen_row_vector_iterator(const gen_row_vector_iterator<MPT> &itm)
108  : vec(itm.vec), isend(itm.isend) {}
109  gen_row_vector_iterator(const gen_row_vector<PT> &m, bool iis_end)
110  : vec(m.vec), isend(iis_end) { }
111 
112  };
113 
114  template <typename PT>
115  struct linalg_traits<gen_row_vector<PT> > {
116  typedef gen_row_vector<PT> this_type;
117  typedef typename std::iterator_traits<PT>::value_type V;
118  typedef typename which_reference<PT>::is_reference is_reference;
119  typedef abstract_matrix linalg_type;
120  typedef typename linalg_traits<V>::origin_type origin_type;
121  typedef typename select_ref<const origin_type *, origin_type *,
122  PT>::ref_type porigin_type;
123  typedef typename linalg_traits<V>::value_type value_type;
124  typedef typename select_ref<value_type,
125  typename linalg_traits<V>::reference, PT>::ref_type reference;
126  typedef abstract_null_type sub_col_type;
127  typedef abstract_null_type col_iterator;
128  typedef abstract_null_type const_sub_col_type;
129  typedef abstract_null_type const_col_iterator;
130  typedef simple_vector_ref<const V *> const_sub_row_type;
131  typedef typename select_ref<abstract_null_type,
132  simple_vector_ref<V *>, PT>::ref_type sub_row_type;
133  typedef gen_row_vector_iterator<typename const_pointer<PT>::pointer>
134  const_row_iterator;
135  typedef typename select_ref<abstract_null_type,
136  gen_row_vector_iterator<PT>, PT>::ref_type row_iterator;
137  typedef typename linalg_traits<V>::storage_type storage_type;
138  typedef row_major sub_orientation;
139  typedef typename linalg_traits<V>::index_sorted index_sorted;
140  static size_type nrows(const this_type &) { return 1; }
141  static size_type ncols(const this_type &m) { return m.ncols(); }
142  static const_sub_row_type row(const const_row_iterator &it) { return *it; }
143  static sub_row_type row(const row_iterator &it) { return *it; }
144  static const_row_iterator row_begin(const this_type &m)
145  { return const_row_iterator(m, false); }
146  static row_iterator row_begin(this_type &m)
147  { return row_iterator(m, false); }
148  static const_row_iterator row_end(const this_type &m)
149  { return const_row_iterator(m, true); }
150  static row_iterator row_end(this_type &m)
151  { return row_iterator(m, true); }
152  static origin_type* origin(this_type &m) { return m.vec.origin; }
153  static const origin_type* origin(const this_type &m)
154  { return m.vec.origin; }
155  static void do_clear(this_type &m)
156  { clear(row(mat_row_begin(m))); }
157  static value_type access(const const_row_iterator &itrow, size_type i)
158  { return itrow.vec[i]; }
159  static reference access(const row_iterator &itrow, size_type i)
160  { return itrow.vec[i]; }
161  };
162 
163  template <typename PT>
164  std::ostream &operator <<(std::ostream &o, const gen_row_vector<PT>& m)
165  { gmm::write(o,m); return o; }
166 
167  /* ********************************************************************* */
168  /* col vector -> transform a vector in a (n, 1) matrix. */
169  /* ********************************************************************* */
170 
171  template <typename PT> struct gen_col_vector {
172  typedef gen_col_vector<PT> this_type;
173  typedef typename std::iterator_traits<PT>::value_type V;
174  typedef V * CPT;
175  typedef typename std::iterator_traits<PT>::reference ref_V;
176  typedef typename linalg_traits<this_type>::reference reference;
177 
178  simple_vector_ref<PT> vec;
179 
180  reference operator()(size_type i, size_type) const { return vec[i]; }
181 
182  size_type ncols(void) const { return 1; }
183  size_type nrows(void) const { return vect_size(vec); }
184 
185  gen_col_vector(ref_V v) : vec(v) {}
186  gen_col_vector() {}
187  gen_col_vector(const gen_col_vector<CPT> &cr) : vec(cr.vec) {}
188  };
189 
190  template <typename PT>
191  struct gen_col_vector_iterator {
192  typedef gen_col_vector<PT> this_type;
193  typedef typename modifiable_pointer<PT>::pointer MPT;
194  typedef typename std::iterator_traits<PT>::value_type V;
195  typedef simple_vector_ref<PT> value_type;
196  typedef const simple_vector_ref<PT> *pointer;
197  typedef const simple_vector_ref<PT> &reference;
198  typedef ptrdiff_t difference_type;
199  typedef size_t size_type;
200  typedef std::random_access_iterator_tag iterator_category;
201  typedef gen_col_vector_iterator<PT> iterator;
202 
203  simple_vector_ref<PT> vec;
204  bool isend;
205 
206  iterator &operator ++() { isend = true; return *this; }
207  iterator &operator --() { isend = false; return *this; }
208  iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
209  iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
210  iterator &operator +=(difference_type i)
211  { if (i) isend = false; return *this; }
212  iterator &operator -=(difference_type i)
213  { if (i) isend = true; return *this; }
214  iterator operator +(difference_type i) const
215  { iterator itt = *this; return (itt += i); }
216  iterator operator -(difference_type i) const
217  { iterator itt = *this; return (itt -= i); }
218  difference_type operator -(const iterator &i) const {
219  return (isend == true) ? ((i.isend == true) ? 0 : 1)
220  : ((i.isend == true) ? -1 : 0);
221  }
222 
223  const simple_vector_ref<PT>& operator *() const { return vec; }
224  const simple_vector_ref<PT>& operator [](int i) { return vec; }
225 
226  bool operator ==(const iterator &i) const { return (isend == i.isend); }
227  bool operator !=(const iterator &i) const { return !(i == *this); }
228  bool operator < (const iterator &i) const { return (*this - i < 0); }
229 
230  gen_col_vector_iterator(void) {}
231  gen_col_vector_iterator(const gen_col_vector_iterator<MPT> &itm)
232  : vec(itm.vec), isend(itm.isend) {}
233  gen_col_vector_iterator(const gen_col_vector<PT> &m, bool iis_end)
234  : vec(m.vec), isend(iis_end) { }
235 
236  };
237 
238  template <typename PT>
239  struct linalg_traits<gen_col_vector<PT> > {
240  typedef gen_col_vector<PT> this_type;
241  typedef typename std::iterator_traits<PT>::value_type V;
242  typedef typename which_reference<PT>::is_reference is_reference;
243  typedef abstract_matrix linalg_type;
244  typedef typename linalg_traits<V>::origin_type origin_type;
245  typedef typename select_ref<const origin_type *, origin_type *,
246  PT>::ref_type porigin_type;
247  typedef typename linalg_traits<V>::value_type value_type;
248  typedef typename select_ref<value_type,
249  typename linalg_traits<V>::reference, PT>::ref_type reference;
250  typedef abstract_null_type sub_row_type;
251  typedef abstract_null_type row_iterator;
252  typedef abstract_null_type const_sub_row_type;
253  typedef abstract_null_type const_row_iterator;
254  typedef simple_vector_ref<const V *> const_sub_col_type;
255  typedef typename select_ref<abstract_null_type,
256  simple_vector_ref<V *>, PT>::ref_type sub_col_type;
257  typedef gen_col_vector_iterator<typename const_pointer<PT>::pointer>
258  const_col_iterator;
259  typedef typename select_ref<abstract_null_type,
260  gen_col_vector_iterator<PT>, PT>::ref_type col_iterator;
261  typedef typename linalg_traits<V>::storage_type storage_type;
262  typedef col_major sub_orientation;
263  typedef typename linalg_traits<V>::index_sorted index_sorted;
264  static size_type ncols(const this_type &) { return 1; }
265  static size_type nrows(const this_type &m) { return m.nrows(); }
266  static const_sub_col_type col(const const_col_iterator &it) { return *it; }
267  static sub_col_type col(const col_iterator &it) { return *it; }
268  static const_col_iterator col_begin(const this_type &m)
269  { return const_col_iterator(m, false); }
270  static col_iterator col_begin(this_type &m)
271  { return col_iterator(m, false); }
272  static const_col_iterator col_end(const this_type &m)
273  { return const_col_iterator(m, true); }
274  static col_iterator col_end(this_type &m)
275  { return col_iterator(m, true); }
276  static origin_type* origin(this_type &m) { return m.vec.origin; }
277  static const origin_type* origin(const this_type &m)
278  { return m.vec.origin; }
279  static void do_clear(this_type &m)
280  { clear(col(mat_col_begin(m))); }
281  static value_type access(const const_col_iterator &itcol, size_type i)
282  { return itcol.vec[i]; }
283  static reference access(const col_iterator &itcol, size_type i)
284  { return itcol.vec[i]; }
285  };
286 
287  template <typename PT>
288  std::ostream &operator <<(std::ostream &o, const gen_col_vector<PT>& m)
289  { gmm::write(o,m); return o; }
290 
291  /* ******************************************************************** */
292  /* col and row vectors */
293  /* ******************************************************************** */
294 
295 
296  template <class V> inline
297  typename select_return< gen_row_vector<const V *>, gen_row_vector<V *>,
298  const V *>::return_type
299  row_vector(const V& v) {
300  return typename select_return< gen_row_vector<const V *>,
301  gen_row_vector<V *>, const V *>::return_type(linalg_cast(v));
302  }
303 
304  template <class V> inline
305  typename select_return< gen_row_vector<const V *>, gen_row_vector<V *>,
306  V *>::return_type
307  row_vector(V& v) {
308  return typename select_return< gen_row_vector<const V *>,
309  gen_row_vector<V *>, V *>::return_type(linalg_cast(v));
310  }
311 
312  template <class V> inline gen_row_vector<const V *>
313  const_row_vector(V& v)
314  { return gen_row_vector<const V *>(v); }
315 
316 
317  template <class V> inline
318  typename select_return< gen_col_vector<const V *>, gen_col_vector<V *>,
319  const V *>::return_type
320  col_vector(const V& v) {
321  return typename select_return< gen_col_vector<const V *>,
322  gen_col_vector<V *>, const V *>::return_type(linalg_cast(v));
323  }
324 
325  template <class V> inline
326  typename select_return< gen_col_vector<const V *>, gen_col_vector<V *>,
327  V *>::return_type
328  col_vector(V& v) {
329  return typename select_return< gen_col_vector<const V *>,
330  gen_col_vector<V *>, V *>::return_type(linalg_cast(v));
331  }
332 
333  template <class V> inline gen_col_vector<const V *>
334  const_col_vector(V& v)
335  { return gen_col_vector<const V *>(v); }
336 
337 
338 }
339 
340 #endif // GMM_VECTOR_TO_MATRIX_H__
bgeot::size_type
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
gmm::clear
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:59
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_interface.h
gmm interface for STL vectors.
bgeot::operator-
rational_fraction< T > operator-(const polynomial< T > &P, const rational_fraction< T > &Q)
Subtract Q from P.
Definition: bgeot_poly.h:756