GetFEM  5.5
gmm_scaled.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-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_scaled.h
32  @author Yves Renard <Yves.Renard@insa-lyon.fr>
33  @date November 10, 2002.
34  @brief get a scaled view of a vector/matrix.
35 */
36 #ifndef GMM_SCALED_H__
37 #define GMM_SCALED_H__
38 
39 #include "gmm_def.h"
40 
41 namespace gmm {
42 
43  /* ********************************************************************* */
44  /* Scaled references on vectors */
45  /* ********************************************************************* */
46 
47  template <typename IT, typename S> struct scaled_const_iterator {
48  typedef typename strongest_numeric_type<typename std::iterator_traits<IT>::value_type,
49  S>::T value_type;
50 
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  S r;
59 
60  scaled_const_iterator(void) {}
61  scaled_const_iterator(const IT &i, S x) : it(i), r(x) {}
62 
63  inline size_type index(void) const { return it.index(); }
64  inline scaled_const_iterator operator ++(int)
65  { scaled_const_iterator tmp = *this; ++it; return tmp; }
66  inline scaled_const_iterator operator --(int)
67  { scaled_const_iterator tmp = *this; --it; return tmp; }
68  inline scaled_const_iterator &operator ++() { ++it; return *this; }
69  inline scaled_const_iterator &operator --() { --it; return *this; }
70  inline scaled_const_iterator &operator +=(difference_type i)
71  { it += i; return *this; }
72  inline scaled_const_iterator &operator -=(difference_type i)
73  { it -= i; return *this; }
74  inline scaled_const_iterator operator +(difference_type i) const
75  { scaled_const_iterator itb = *this; return (itb += i); }
76  inline scaled_const_iterator operator -(difference_type i) const
77  { scaled_const_iterator itb = *this; return (itb -= i); }
78  inline difference_type operator -(const scaled_const_iterator &i) const
79  { return difference_type(it - i.it); }
80 
81  inline value_type operator *() const { return (*it) * value_type(r); }
82  inline value_type operator [](size_type ii) const { return it[ii] * r; }
83 
84  inline bool operator ==(const scaled_const_iterator &i) const
85  { return (i.it == it); }
86  inline bool operator !=(const scaled_const_iterator &i) const
87  { return (i.it != it); }
88  inline bool operator < (const scaled_const_iterator &i) const
89  { return (it < i.it); }
90  inline bool operator > (const scaled_const_iterator &i) const
91  { return (it > i.it); }
92  inline bool operator >=(const scaled_const_iterator &i) const
93  { return (it >= i.it); }
94  };
95 
96  template <typename V, typename S> struct scaled_vector_const_ref {
97  typedef scaled_vector_const_ref<V,S> this_type;
98  typedef typename linalg_traits<this_type>::value_type value_type;
99  typedef typename linalg_traits<V>::const_iterator iterator;
100  typedef typename linalg_traits<this_type>::reference reference;
101  typedef typename linalg_traits<this_type>::origin_type origin_type;
102 
103  iterator begin_, end_;
104  const origin_type *origin;
105  size_type size_;
106  S r;
107 
108  scaled_vector_const_ref(const V &v, S rr)
109  : begin_(vect_const_begin(v)), end_(vect_const_end(v)),
110  origin(linalg_origin(v)), size_(vect_size(v)), r(rr) {}
111 
112  reference operator[](size_type i) const
113  { return value_type(r) * linalg_traits<V>::access(origin, begin_, end_, i); }
114  };
115 
116 
117  template<typename V, typename S> std::ostream &operator <<
118  (std::ostream &o, const scaled_vector_const_ref<V,S>& m)
119  { gmm::write(o,m); return o; }
120 
121  /* ********************************************************************* */
122  /* Scaled references on matrices */
123  /* ********************************************************************* */
124 
125  template <typename M, typename S> struct scaled_row_const_iterator {
126  typedef scaled_row_const_iterator<M,S> iterator;
127  typedef typename linalg_traits<M>::const_row_iterator ITER;
128  typedef ptrdiff_t difference_type;
129  typedef size_t size_type;
130 
131  ITER it;
132  S r;
133 
134  inline iterator operator ++(int) { iterator tmp=*this; it++; return tmp; }
135  inline iterator operator --(int) { iterator tmp=*this; it--; return tmp; }
136  inline iterator &operator ++() { it++; return *this; }
137  inline iterator &operator --() { it--; return *this; }
138  iterator &operator +=(difference_type i) { it += i; return *this; }
139  iterator &operator -=(difference_type i) { it -= i; return *this; }
140  iterator operator +(difference_type i) const
141  { iterator itt = *this; return (itt += i); }
142  iterator operator -(difference_type i) const
143  { iterator itt = *this; return (itt -= i); }
144  difference_type operator -(const iterator &i) const
145  { return it - i.it; }
146 
147  inline ITER operator *() const { return it; }
148  inline ITER operator [](int i) { return it + i; }
149 
150  inline bool operator ==(const iterator &i) const { return (it == i.it); }
151  inline bool operator !=(const iterator &i) const { return !(i == *this); }
152  inline bool operator < (const iterator &i) const { return (it < i.it); }
153  inline bool operator >=(const iterator &i) const { return (it >= i.it); }
154  inline bool operator > (const iterator &i) const { return (it > i.it); }
155 
156  scaled_row_const_iterator(void) {}
157  scaled_row_const_iterator(const ITER &i, S rr)
158  : it(i), r(rr) { }
159 
160  };
161 
162  template <typename M, typename S> struct scaled_row_matrix_const_ref {
163 
164  typedef scaled_row_matrix_const_ref<M,S> this_type;
165  typedef typename linalg_traits<M>::const_row_iterator iterator;
166  typedef typename linalg_traits<this_type>::value_type value_type;
167  typedef typename linalg_traits<this_type>::origin_type origin_type;
168 
169  iterator begin_, end_;
170  const origin_type *origin;
171  S r;
172  size_type nr, nc;
173 
174  scaled_row_matrix_const_ref(const M &m, S rr)
175  : begin_(mat_row_begin(m)), end_(mat_row_end(m)),
176  origin(linalg_origin(m)), r(rr), nr(mat_nrows(m)), nc(mat_ncols(m)) {}
177 
178  value_type operator()(size_type i, size_type j) const
179  { return r * linalg_traits<M>::access(begin_+i, j); }
180  };
181 
182 
183  template<typename M, typename S> std::ostream &operator <<
184  (std::ostream &o, const scaled_row_matrix_const_ref<M,S>& m)
185  { gmm::write(o,m); return o; }
186 
187 
188  template <typename M, typename S> struct scaled_col_const_iterator {
189  typedef scaled_col_const_iterator<M,S> iterator;
190  typedef typename linalg_traits<M>::const_col_iterator ITER;
191  typedef ptrdiff_t difference_type;
192  typedef size_t size_type;
193 
194  ITER it;
195  S r;
196 
197  iterator operator ++(int) { iterator tmp = *this; it++; return tmp; }
198  iterator operator --(int) { iterator tmp = *this; it--; return tmp; }
199  iterator &operator ++() { it++; return *this; }
200  iterator &operator --() { it--; return *this; }
201  iterator &operator +=(difference_type i) { it += i; return *this; }
202  iterator &operator -=(difference_type i) { it -= i; return *this; }
203  iterator operator +(difference_type i) const
204  { iterator itt = *this; return (itt += i); }
205  iterator operator -(difference_type i) const
206  { iterator itt = *this; return (itt -= i); }
207  difference_type operator -(const iterator &i) const
208  { return it - i.it; }
209 
210  ITER operator *() const { return it; }
211  ITER operator [](int i) { return it + i; }
212 
213  bool operator ==(const iterator &i) const { return (it == i.it); }
214  bool operator !=(const iterator &i) const { return !(i == *this); }
215  bool operator < (const iterator &i) const { return (it < i.it); }
216  bool operator > (const iterator &i) const { return (it > i.it); }
217  bool operator >=(const iterator &i) const { return (it >= i.it); }
218 
219  scaled_col_const_iterator(void) {}
220  scaled_col_const_iterator(const ITER &i, S rr)
221  : it(i), r(rr) { }
222 
223  };
224 
225  template <typename M, typename S> struct scaled_col_matrix_const_ref {
226 
227  typedef scaled_col_matrix_const_ref<M,S> this_type;
228  typedef typename linalg_traits<M>::const_col_iterator iterator;
229  typedef typename linalg_traits<this_type>::value_type value_type;
230  typedef typename linalg_traits<this_type>::origin_type origin_type;
231 
232  iterator begin_, end_;
233  const origin_type *origin;
234  S r;
235  size_type nr, nc;
236 
237  scaled_col_matrix_const_ref(const M &m, S rr)
238  : begin_(mat_col_begin(m)), end_(mat_col_end(m)),
239  origin(linalg_origin(m)), r(rr), nr(mat_nrows(m)), nc(mat_ncols(m)) {}
240 
241  value_type operator()(size_type i, size_type j) const
242  { return r * linalg_traits<M>::access(begin_+j, i); }
243  };
244 
245 
246 
247  template<typename M, typename S> std::ostream &operator <<
248  (std::ostream &o, const scaled_col_matrix_const_ref<M,S>& m)
249  { gmm::write(o,m); return o; }
250 
251 
252  template <typename L, typename S, typename R> struct scaled_return__ {
253  typedef abstract_null_type return_type;
254  };
255  template <typename L, typename S> struct scaled_return__<L, S, row_major>
256  { typedef scaled_row_matrix_const_ref<L,S> return_type; };
257  template <typename L, typename S> struct scaled_return__<L, S, col_major>
258  { typedef scaled_col_matrix_const_ref<L,S> return_type; };
259 
260 
261  template <typename L, typename S, typename LT> struct scaled_return_ {
262  typedef abstract_null_type return_type;
263  };
264  template <typename L, typename S> struct scaled_return_<L, S, abstract_vector>
265  { typedef scaled_vector_const_ref<L,S> return_type; };
266  template <typename L, typename S> struct scaled_return_<L, S, abstract_matrix> {
267  typedef typename scaled_return__<L, S,
268  typename principal_orientation_type<typename
269  linalg_traits<L>::sub_orientation>::potype>::return_type return_type;
270  };
271 
272  template <typename L, typename S> struct scaled_return {
273  typedef typename scaled_return_<L, S, typename
274  linalg_traits<L>::linalg_type>::return_type return_type;
275  };
276 
277  template <typename L, typename S> inline
278  typename scaled_return<L,S>::return_type
279  scaled(const L &v, S x)
280  { return scaled(v, x, typename linalg_traits<L>::linalg_type()); }
281 
282  template <typename V, typename S> inline
283  typename scaled_return<V,S>::return_type
284  scaled(const V &v, S x, abstract_vector)
285  { return scaled_vector_const_ref<V,S>(v, x); }
286 
287  template <typename M, typename S> inline
288  typename scaled_return<M,S>::return_type
289  scaled(const M &m, S x,abstract_matrix) {
290  return scaled(m, x, typename principal_orientation_type<typename
291  linalg_traits<M>::sub_orientation>::potype());
292  }
293 
294  template <typename M, typename S> inline
295  typename scaled_return<M,S>::return_type
296  scaled(const M &m, S x, row_major) {
297  return scaled_row_matrix_const_ref<M,S>(m, x);
298  }
299 
300  template <typename M, typename S> inline
301  typename scaled_return<M,S>::return_type
302  scaled(const M &m, S x, col_major) {
303  return scaled_col_matrix_const_ref<M,S>(m, x);
304  }
305 
306 
307  /* ******************************************************************** */
308  /* matrix or vector scale */
309  /* ******************************************************************** */
310 
311  template <typename L> inline
312  void scale(L& l, typename linalg_traits<L>::value_type a)
313  { scale(l, a, typename linalg_traits<L>::linalg_type()); }
314 
315  template <typename L> inline
316  void scale(const L& l, typename linalg_traits<L>::value_type a)
317  { scale(linalg_const_cast(l), a); }
318 
319  template <typename L> inline
320  void scale(L& l, typename linalg_traits<L>::value_type a, abstract_vector) {
321  typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
322  for ( ; it != ite; ++it) *it *= a;
323  }
324 
325  template <typename L>
326  void scale(L& l, typename linalg_traits<L>::value_type a, abstract_matrix) {
327  scale(l, a, typename principal_orientation_type<typename
328  linalg_traits<L>::sub_orientation>::potype());
329  }
330 
331  template <typename L>
332  void scale(L& l, typename linalg_traits<L>::value_type a, row_major) {
333  typename linalg_traits<L>::row_iterator it = mat_row_begin(l),
334  ite = mat_row_end(l);
335  for ( ; it != ite; ++it) scale(linalg_traits<L>::row(it), a);
336  }
337 
338  template <typename L>
339  void scale(L& l, typename linalg_traits<L>::value_type a, col_major) {
340  typename linalg_traits<L>::col_iterator it = mat_col_begin(l),
341  ite = mat_col_end(l);
342  for ( ; it != ite; ++it) scale(linalg_traits<L>::col(it), a);
343  }
344 
345  template <typename V, typename S> struct linalg_traits<scaled_vector_const_ref<V,S> > {
346  typedef scaled_vector_const_ref<V,S> this_type;
347  typedef linalg_const is_reference;
348  typedef abstract_vector linalg_type;
349  typedef typename strongest_numeric_type<S, typename linalg_traits<V>::value_type>::T value_type;
350  typedef typename linalg_traits<V>::origin_type origin_type;
351  typedef value_type reference;
352  typedef abstract_null_type iterator;
353  typedef scaled_const_iterator<typename linalg_traits<V>::const_iterator, S>
354  const_iterator;
355  typedef typename linalg_traits<V>::storage_type storage_type;
356  typedef typename linalg_traits<V>::index_sorted index_sorted;
357  static size_type size(const this_type &v) { return v.size_; }
358  static const_iterator begin(const this_type &v)
359  { return const_iterator(v.begin_, v.r); }
360  static const_iterator end(const this_type &v)
361  { return const_iterator(v.end_, v.r); }
362  static const origin_type* origin(const this_type &v) { return v.origin; }
363  static value_type access(const origin_type *o, const const_iterator &it,
364  const const_iterator &ite, size_type i)
365  { return it.r * (linalg_traits<V>::access(o, it.it, ite.it, i)); }
366 
367  };
368 
369 
370  template <typename M, typename S> struct linalg_traits<scaled_row_matrix_const_ref<M,S> > {
371  typedef scaled_row_matrix_const_ref<M,S> this_type;
372  typedef linalg_const is_reference;
373  typedef abstract_matrix linalg_type;
374  typedef typename linalg_traits<M>::origin_type origin_type;
375  typedef typename strongest_numeric_type<S, typename linalg_traits<M>::value_type>::T value_type;
376  typedef value_type reference;
377  typedef typename linalg_traits<M>::storage_type storage_type;
378  typedef typename org_type<typename linalg_traits<M>::const_sub_row_type>::t vector_type;
379  typedef scaled_vector_const_ref<vector_type,S> sub_row_type;
380  typedef scaled_vector_const_ref<vector_type,S> const_sub_row_type;
381  typedef scaled_row_const_iterator<M,S> row_iterator;
382  typedef scaled_row_const_iterator<M,S> const_row_iterator;
383  typedef abstract_null_type const_sub_col_type;
384  typedef abstract_null_type sub_col_type;
385  typedef abstract_null_type const_col_iterator;
386  typedef abstract_null_type col_iterator;
387  typedef row_major sub_orientation;
388  typedef typename linalg_traits<M>::index_sorted index_sorted;
389  static size_type nrows(const this_type &m)
390  { return m.nr; }
391  static size_type ncols(const this_type &m)
392  { return m.nc; }
393  static const_sub_row_type row(const const_row_iterator &it)
394  { return scaled(linalg_traits<M>::row(it.it), it.r); }
395  static const_row_iterator row_begin(const this_type &m)
396  { return const_row_iterator(m.begin_, m.r); }
397  static const_row_iterator row_end(const this_type &m)
398  { return const_row_iterator(m.end_, m.r); }
399  static const origin_type* origin(const this_type &m) { return m.origin; }
400  static value_type access(const const_row_iterator &it, size_type i)
401  { return it.r * (linalg_traits<M>::access(it.it, i)); }
402  };
403 
404  template <typename M, typename S> struct linalg_traits<scaled_col_matrix_const_ref<M,S> > {
405  typedef scaled_col_matrix_const_ref<M,S> this_type;
406  typedef linalg_const is_reference;
407  typedef abstract_matrix linalg_type;
408  typedef typename strongest_numeric_type<S, typename linalg_traits<M>::value_type>::T value_type;
409  typedef typename linalg_traits<M>::origin_type origin_type;
410  typedef value_type reference;
411  typedef typename linalg_traits<M>::storage_type storage_type;
412  typedef typename org_type<typename linalg_traits<M>::const_sub_col_type>::t vector_type;
413  typedef abstract_null_type sub_col_type;
414  typedef scaled_vector_const_ref<vector_type,S> const_sub_col_type;
415  typedef abstract_null_type col_iterator;
416  typedef scaled_col_const_iterator<M,S> const_col_iterator;
417  typedef abstract_null_type const_sub_row_type;
418  typedef abstract_null_type sub_row_type;
419  typedef abstract_null_type const_row_iterator;
420  typedef abstract_null_type row_iterator;
421  typedef col_major sub_orientation;
422  typedef typename linalg_traits<M>::index_sorted index_sorted;
423  static size_type ncols(const this_type &m)
424  { return m.nc; }
425  static size_type nrows(const this_type &m)
426  { return m.nr; }
427  static const_sub_col_type col(const const_col_iterator &it)
428  { return scaled(linalg_traits<M>::col(it.it), it.r); }
429  static const_col_iterator col_begin(const this_type &m)
430  { return const_col_iterator(m.begin_, m.r); }
431  static const_col_iterator col_end(const this_type &m)
432  { return const_col_iterator(m.end_, m.r); }
433  static const origin_type* origin(const this_type &m) { return m.origin; }
434  static value_type access(const const_col_iterator &it, size_type i)
435  { return it.r * (linalg_traits<M>::access(it.it, i)); }
436  };
437 
438 
439 }
440 
441 #endif // GMM_SCALED_H__
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