Rheolef  7.2
an efficient C++ finite element environment
vec.h
Go to the documentation of this file.
1 #ifndef _RHEO_VEC_H
2 #define _RHEO_VEC_H
3 //
4 // This file is part of Rheolef.
5 //
6 // Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
7 //
8 // Rheolef is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // Rheolef is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Rheolef; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //
22 // =========================================================================
23 // AUTHORS: Pierre.Saramito@imag.fr
24 // DATE: 19 november 1998
25 
26 namespace rheolef {
59 } // namespace rheolef
60 
61 
62 # include "rheolef/disarray.h"
63 # include "rheolef/range.h"
64 
65 namespace rheolef {
66 
67 template <class T, class M> class vec_range;
68 template <class T, class M> class vec_range_const;
69 
70 namespace details {
71 template <class Expr> struct is_vec;
72 template <class Expr> struct is_vec_expr_v2_arg;
73 template <class T, class M> class vec_concat_value; // for vec = {x,y};
74 } // namespace details
75 
76 
77 // [verbatim_vec]
78 template <class T, class M = rheo_default_memory_model>
79 class vec : public disarray<T, M> {
80 public:
81 
82 // typedef:
83 
85  typedef T value_type;
86  typedef typename base::size_type size_type;
87  typedef std::ptrdiff_t difference_type;
88  typedef range range_type;
89  typedef typename base::reference reference;
90  typedef typename base::const_reference const_reference;
91  typedef typename base::iterator iterator;
94 
95 // allocator/deallocator:
96 
97  vec (const vec<T,M>&);
99 
100  vec (const distributor& ownership,
101  const T& init_val = std::numeric_limits<T>::max());
102 
103  vec (const std::initializer_list<details::vec_concat_value<T,M> >& init_list);
104 
105  vec<T,M>& operator= (const std::initializer_list<details::vec_concat_value<T,M> >& init_list);
106  vec(size_type dis_size = 0,
107  const T& init_val = std::numeric_limits<T>::max());
108 
109  void resize (
110  const distributor& ownership,
111  const T& init_val = std::numeric_limits<T>::max());
112 
113  void resize (
114  size_type size = 0,
115  const T& init_val = std::numeric_limits<T>::max());
116 
117 // accessors:
118 
121 
122  T min () const;
123  T max () const;
124  T max_abs () const;
125 
126  int constraint_process_rank() const;
127 
128 // range:
129 
130  vec(const vec_range<T,M>& vr);
134 
137 
138 // assignment to a constant:
139 
140  vec<T,M>& operator= (const int& expr);
142 
143 // expression template:
144 
145  template <class Expr,
146  class Sfinae
147  = typename std::enable_if<
150  >::type>
151  vec (const Expr& expr);
152 
153  template <class Expr,
154  class Sfinae
155  = typename std::enable_if<
158  >::type>
159  vec<T, M>& operator= (const Expr& expr);
160 };
161 // [verbatim_vec]
162 
163 // ----------------------------------------------------------------------------
164 // inlined
165 // ----------------------------------------------------------------------------
166 template <class T, class M>
167 inline
169  : disarray<T,M>(x)
170 {
171 }
172 template <class T, class M>
173 inline
174 vec<T,M>&
176 {
178  return *this;
179 }
180 template <class T, class M>
181 inline
183  const distributor& ownership,
184  const T& init_val)
185  : disarray<T,M>(ownership,init_val)
186 {
187 }
188 template <class T, class M>
189 inline
191  size_type dis_size,
192  const T& init_val)
193  : disarray<T,M>(dis_size,init_val)
194 {
195 }
196 template <class T, class M>
197 inline
198 void
200  const distributor& ownership,
201  const T& init_val)
202 {
203  base::resize (ownership, init_val);
204 }
205 template <class T, class M>
206 inline
207 void
209  size_type dis_size,
210  const T& init_val)
211 {
212  base::resize (dis_size, init_val);
213 }
214 // TODO: group cstors vec(int) & vec(T) via Sfinae
215 template <class T, class M>
216 inline
217 vec<T,M>&
219 {
220  std::fill (disarray<T,M>::begin(), disarray<T,M>::end(), expr);
221  return *this;
222 }
223 template <class T, class M>
224 inline
225 vec<T,M>&
227 {
228  std::fill (disarray<T,M>::begin(), disarray<T,M>::end(), expr);
229  return *this;
230 }
231 template <class T, class M>
232 inline
233 vec<T,M>&
235 {
236  distributor ownership (distributor::decide, vr._u.comm(), vr._r.size());
237  resize (ownership);
238  std::copy (vr.begin(), vr.end(), base::begin());
239  return *this;
240 }
241 template <class T, class M>
242 inline
243 vec<T,M>&
245 {
246  operator= (vec_range_const<T,M>(vr));
247  return *this;
248 }
249 template <class T, class M>
250 inline
252  : disarray<T,M>()
253 {
254  operator= (vr);
255 }
256 template <class T, class M>
257 inline
259  : disarray<T,M>()
260 {
261  operator= (vr);
262 }
263 template <class T, class M>
264 inline
267 {
268  return base::operator[] (i);
269 }
270 template <class T, class M>
271 inline
272 typename vec<T,M>::reference
274 {
275  return base::operator[] (i);
276 }
277 template <class T, class M>
278 inline
281 {
282  return vec_range<T,M> (*this, r);
283 }
284 template <class T, class M>
285 inline
288 {
289  return vec_range_const<T,M> (*this, r);
290 }
291 template <class T, class M>
292 T
294 {
295  T val = std::numeric_limits<T>::max();
296  for (const_iterator iter = base::begin(), last = base::end(); iter != last; iter++) {
297  val = std::min(val, *iter);
298  }
299 #ifdef _RHEOLEF_HAVE_MPI
300  val = mpi::all_reduce (base::comm(), val, mpi::minimum<T>());
301 #endif // _RHEOLEF_HAVE_MPI
302  return val;
303 }
304 template <class T, class M>
305 T
307 {
308  T val = std::numeric_limits<T>::min();
309  for (const_iterator iter = base::begin(), last = base::end(); iter != last; iter++) {
310  val = std::max(val, *iter);
311  }
312 #ifdef _RHEOLEF_HAVE_MPI
313  val = mpi::all_reduce (base::comm(), val, mpi::maximum<T>());
314 #endif // _RHEOLEF_HAVE_MPI
315  return val;
316 }
317 template <class T, class M>
318 T
320 {
321  T val = 0;
322  for (const_iterator iter = base::begin(), last = base::end(); iter != last; iter++) {
323  val = std::max(val, abs(*iter));
324  }
325 #ifdef _RHEOLEF_HAVE_MPI
326  val = mpi::all_reduce (base::comm(), val, mpi::maximum<T>());
327 #endif // _RHEOLEF_HAVE_MPI
328  return val;
329 }
330 template <class T>
331 inline
334 {
335  return x.get_values(ips);
336 }
337 template <class T, class M>
338 inline
340 operator << (odiststream& ods, const vec<T,M>& x)
341 {
342  iorheo::flag_type format = iorheo::flags(ods.os()) & iorheo::format_field;
343  if (format [iorheo::matlab] || format [iorheo::sparse_matlab]) {
344  return x.data().put_matlab (ods);
345  }
346  // default is raw output
347  return x.put_values(ods);
348 }
349 #ifdef _RHEOLEF_HAVE_MPI
350 template <class T>
351 inline
352 idiststream&
354 {
355  return x.get_values(ips);
356 }
357 #ifdef TO_CLEAN
358 template <class T>
359 inline
360 odiststream&
361 operator << (odiststream& ods, const vec<T,distributed>& x)
362 {
363  iorheo::flag_type format = iorheo::flags(ods.os()) & iorheo::format_field;
364  if (format [iorheo::matlab] || format [iorheo::sparse_matlab]) {
365  return x.put_matlab (ods);
366  }
367  // default is raw output
368  return x.put_values(ods);
369 }
370 #endif // TO_CLEAN
371 #endif // _RHEOLEF_HAVE_MPI
372 // -------------------------------------------
373 // norm(x) ; dot(x,y)
374 // -------------------------------------------
376 template<class T, class M>
377 inline
378 T
379 norm2 (const vec<T,M>& x)
380 {
381  return dot(x,x);
382 }
384 template<class T, class M>
385 inline
386 T
387 norm (const vec<T,M>& x)
388 {
389  return sqrt(norm2(x));
390 }
391 
392 } // namespace rheolef
393 #endif // _RHEO_VEC_H
see the disarray page for the full documentation
Definition: disarray.h:497
rep::base::const_iterator const_iterator
Definition: disarray.h:503
rep::base::iterator iterator
Definition: disarray.h:502
rep::base::size_type size_type
Definition: disarray.h:501
see the distributor page for the full documentation
Definition: distributor.h:69
idiststream: see the diststream page for the full documentation
Definition: diststream.h:336
odiststream: see the diststream page for the full documentation
Definition: diststream.h:137
std::ostream & os()
Definition: diststream.h:247
const_iterator begin() const
Definition: vec_range.h:85
const_iterator end() const
Definition: vec_range.h:86
see the vec page for the full documentation
Definition: vec.h:79
T max_abs() const
Definition: vec.h:319
T value_type
Definition: vec.h:85
base::iterator iterator
Definition: vec.h:91
vec(const vec_range_const< T, M > &vr)
Definition: vec.h:258
base::size_type size_type
Definition: vec.h:86
vec< T, M > & operator=(const vec< T, M > &x)
Definition: vec.h:175
base::const_reference const_reference
Definition: vec.h:90
vec(const vec_range< T, M > &vr)
Definition: vec.h:251
range range_type
Definition: vec.h:88
base::const_iterator const_iterator
Definition: vec.h:92
vec(const distributor &ownership, const T &init_val=std::numeric_limits< T >::max())
Definition: vec.h:182
T min() const
Definition: vec.h:293
vec(size_type dis_size=0, const T &init_val=std::numeric_limits< T >::max())
Definition: vec.h:190
T max() const
Definition: vec.h:306
int constraint_process_rank() const
Definition: vec_concat.h:91
disarray< T, M > base
Definition: vec.h:84
std::ptrdiff_t difference_type
Definition: vec.h:87
void resize(size_type size=0, const T &init_val=std::numeric_limits< T >::max())
Definition: vec.h:208
base::reference reference
Definition: vec.h:89
const_reference operator[](size_type i) const
Definition: vec.h:266
float_traits< value_type >::type float_type
Definition: vec.h:93
void resize(const distributor &ownership, const T &init_val=std::numeric_limits< T >::max())
Definition: vec.h:199
vec(const vec< T, M > &)
Definition: vec.h:168
rheolef::std type
Expr1::float_type T
Definition: field_expr.h:230
verbose clean transpose logscale grid shrink ball stereo iso volume skipvtk deformation fastfieldload lattice reader_on_stdin color format format format format format format format format format format format format format format format matlab
rheolef::details::is_vec dot
T max_abs(const T &x)
This file is part of Rheolef.
T norm(const vec< T, M > &x)
norm(x): see the expression page for the full documentation
Definition: vec.h:387
T norm2(const vec< T, M > &x)
norm2(x): see the expression page for the full documentation
Definition: vec.h:379
idiststream & operator>>(idiststream &ips, vec< T, distributed > &x)
Definition: vec.h:353
see the range page for the full documentation
Definition: range.h:61
size_type size() const
Definition: range.h:86
ostream & operator<<(ostream &os, const tiny_element &K)
Definition: tiny_element.cc:27
Expr1::memory_type M
Definition: vec_expr_v2.h:416