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