Rheolef  7.2
an efficient C++ finite element environment
 
Loading...
Searching...
No Matches
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
26namespace rheolef {
59} // namespace rheolef
60
61
62# include "rheolef/disarray.h"
63# include "rheolef/range.h"
64
65namespace rheolef {
66
67template <class T, class M> class vec_range;
68template <class T, class M> class vec_range_const;
69
70namespace details {
71template <class Expr> struct is_vec;
72template <class Expr> struct is_vec_expr_v2_arg;
73template <class T, class M> class vec_concat_value; // for vec = {x,y};
74} // namespace details
75
76
77// [verbatim_vec]
78template <class T, class M = rheo_default_memory_model>
79class vec : public disarray<T, M> {
80public:
81
82// typedef:
83
85 typedef T value_type;
86 typedef typename base::size_type size_type;
87 typedef std::ptrdiff_t difference_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);
141 vec<T,M>& operator= (const T& 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// ----------------------------------------------------------------------------
166template <class T, class M>
167inline
169 : disarray<T,M>(x)
170{
171}
172template <class T, class M>
173inline
176{
178 return *this;
179}
180template <class T, class M>
181inline
183 const distributor& ownership,
184 const T& init_val)
185 : disarray<T,M>(ownership,init_val)
186{
187}
188template <class T, class M>
189inline
191 size_type dis_size,
192 const T& init_val)
193 : disarray<T,M>(dis_size,init_val)
194{
195}
196template <class T, class M>
197inline
198void
200 const distributor& ownership,
201 const T& init_val)
202{
203 base::resize (ownership, init_val);
204}
205template <class T, class M>
206inline
207void
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
215template <class T, class M>
216inline
218vec<T,M>::operator= (const int& expr)
219{
220 std::fill (disarray<T,M>::begin(), disarray<T,M>::end(), expr);
221 return *this;
222}
223template <class T, class M>
224inline
227{
228 std::fill (disarray<T,M>::begin(), disarray<T,M>::end(), expr);
229 return *this;
230}
231template <class T, class M>
232inline
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}
241template <class T, class M>
242inline
245{
246 operator= (vec_range_const<T,M>(vr));
247 return *this;
248}
249template <class T, class M>
250inline
252 : disarray<T,M>()
253{
254 operator= (vr);
255}
256template <class T, class M>
257inline
259 : disarray<T,M>()
260{
261 operator= (vr);
262}
263template <class T, class M>
264inline
267{
268 return base::operator[] (i);
269}
270template <class T, class M>
271inline
272typename vec<T,M>::reference
274{
275 return base::operator[] (i);
276}
277template <class T, class M>
278inline
281{
282 return vec_range<T,M> (*this, r);
283}
284template <class T, class M>
285inline
288{
289 return vec_range_const<T,M> (*this, r);
290}
291template <class T, class M>
292T
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}
304template <class T, class M>
305T
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}
317template <class T, class M>
318T
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}
330template <class T>
331inline
334{
335 return x.get_values(ips);
336}
337template <class T, class M>
338inline
340operator << (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
350template <class T>
351inline
352idiststream&
354{
355 return x.get_values(ips);
356}
357#ifdef TO_CLEAN
358template <class T>
359inline
360odiststream&
361operator << (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// -------------------------------------------
376template<class T, class M>
377inline
378T
379norm2 (const vec<T,M>& x)
380{
381 return dot(x,x);
382}
384template<class T, class M>
385inline
386T
387norm (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
static const size_type decide
Definition distributor.h:83
idiststream: see the diststream page for the full documentation
Definition diststream.h:336
std::bitset< last > flag_type
Definition iorheo.h:440
static flag_type format_field
Definition iorheo.h:445
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
Expr1::float_type T
Definition field_expr.h:230
This file is part of Rheolef.
std::ostream & operator<<(std::ostream &os, const catchmark &m)
Definition catchmark.h:99
T norm2(const vec< T, M > &x)
norm2(x): see the expression page for the full documentation
Definition vec.h:379
rheolef::std enable_if ::type dot const Expr1 expr1, const Expr2 expr2 dot(const Expr1 &expr1, const Expr2 &expr2)
std::istream & operator>>(std::istream &is, const catchmark &m)
Definition catchmark.h:88
T norm(const vec< T, M > &x)
norm(x): see the expression page for the full documentation
Definition vec.h:387
see the range page for the full documentation
Definition range.h:61
size_type size() const
Definition range.h:86
Expr1::memory_type M