Dune Core Modules (2.3.1)

fvector.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// $Id$
4#ifndef DUNE_FVECTOR_HH
5#define DUNE_FVECTOR_HH
6
7#include <cmath>
8#include <cstddef>
9#include <cstdlib>
10#include <complex>
11#include <cstring>
12#include <utility>
13
14#include <dune/common/std/constexpr.hh>
15
16#include "typetraits.hh"
17#include "exceptions.hh"
18#include "array.hh"
19#include "densevector.hh"
20#include "static_assert.hh"
21#include "unused.hh"
22
23namespace Dune {
24
34 template< class K, int SIZE > class FieldVector;
35 template< class K, int SIZE >
36 struct DenseMatVecTraits< FieldVector<K,SIZE> >
37 {
38 typedef FieldVector<K,SIZE> derived_type;
39 typedef Dune::array<K,SIZE> container_type;
40 typedef K value_type;
41 typedef typename container_type::size_type size_type;
42 };
43
44 template< class K, int SIZE >
45 struct FieldTraits< FieldVector<K,SIZE> >
46 {
47 typedef typename FieldTraits<K>::field_type field_type;
48 typedef typename FieldTraits<K>::real_type real_type;
49 };
50
59 template<typename C, int SIZE>
61 {
62 enum {
67 value = true
68 };
69 };
70
71 template<typename T, int SIZE>
72 struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE>,SIZE>
73 {
74 enum {value = true};
75 };
76
77 template<typename T, int SIZE, int SIZE1>
78 struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE1>,SIZE>
79 {
80 enum {value = false};
81 };
82
83
89 template< class K, int SIZE >
91 public DenseVector< FieldVector<K,SIZE> >
92 {
95 public:
97 enum {
99 dimension = SIZE
100 };
101
102 typedef typename Base::size_type size_type;
103 typedef typename Base::value_type value_type;
104
107 // Use C++11 unified initialization if available - tends to generate
108 // fastest code
109#if HAVE_INITIALIZER_LIST
110 : _data{}
111 {}
112#else
113 {
114 // fall back to library approach - this gives faster code than array placement
115 // new. Apart from that, placement new may create problems if K is a complex
116 // type. In that case, the default constructor of the _data elements has already
117 // been called and may have allocated memory.
118 std::fill(_data.begin(),_data.end(),K());
119 }
120#endif
121
123 explicit FieldVector (const K& t)
124 {
125 fill(t);
126 }
127
129 FieldVector (const FieldVector & x) : _data(x._data)
130 {}
131
143 template<class C>
145 {
147 // do a run-time size check, for the case that x is not a FieldVector
148 assert(x.size() == SIZE);
149 for (size_type i = 0; i<SIZE; i++)
150 _data[i] = x[i];
151 }
152
154 template<class K1, int SIZE1>
156 {
157 dune_static_assert(SIZE1 == SIZE, "FieldVector in constructor has wrong size");
158 for (size_type i = 0; i<SIZE; i++)
159 _data[i] = x[i];
160 }
161 using Base::operator=;
162
163 DUNE_CONSTEXPR size_type size () const { return vec_size(); }
164
165 // make this thing a vector
166 DUNE_CONSTEXPR size_type vec_size () const { return SIZE; }
167 K & vec_access(size_type i) { return _data[i]; }
168 const K & vec_access(size_type i) const { return _data[i]; }
169 private:
170 void fill(const K& t)
171 {
172 for (int i=0; i<SIZE; i++) _data[i]=t;
173 }
174 };
175
187 template<class K, int SIZE>
188 inline std::istream &operator>> ( std::istream &in,
190 {
192 for( typename FieldVector<K, SIZE>::size_type i = 0; i < SIZE; ++i )
193 in >> w[ i ];
194 if(in)
195 v = w;
196 return in;
197 }
198
199#ifndef DOXYGEN
200 template< class K >
201 struct DenseMatVecTraits< FieldVector<K,1> >
202 {
203 typedef FieldVector<K,1> derived_type;
204 typedef K container_type;
205 typedef K value_type;
206 typedef size_t size_type;
207 };
208
211 template<class K>
212 class FieldVector<K, 1> :
213 public DenseVector< FieldVector<K,1> >
214 {
215 K _data;
216 typedef DenseVector< FieldVector<K,1> > Base;
217 public:
219 enum {
221 dimension = 1
222 };
223
224 typedef typename Base::size_type size_type;
225
226 //===== construction
227
229 FieldVector ()
230 : _data()
231 {}
232
234 FieldVector (const K& k) : _data(k) {}
235
237 template<class C>
238 FieldVector (const DenseVector<C> & x)
239 {
240 dune_static_assert(((bool)IsFieldVectorSizeCorrect<C,1>::value), "FieldVectors do not match in dimension!");
241 assert(x.size() == 1);
242 _data = x[0];
243 }
244
246 FieldVector ( const FieldVector &other )
247 : _data( other._data )
248 {}
249
251 inline FieldVector& operator= (const K& k)
252 {
253 _data = k;
254 return *this;
255 }
256
257 DUNE_CONSTEXPR size_type size () const { return vec_size(); }
258
259 //===== forward methods to container
260 DUNE_CONSTEXPR size_type vec_size () const { return 1; }
261 K & vec_access(size_type i)
262 {
263 assert(i == 0);
264 return _data;
265 }
266 const K & vec_access(size_type i) const
267 {
268 assert(i == 0);
269 return _data;
270 }
271
272 //===== conversion operator
273
275 operator K () { return _data; }
276
278 operator K () const { return _data; }
279 };
280
281 /* ----- FV / FV ----- */
282 /* not necessary as these operations are already covered via the cast operator */
283
284 /* ----- FV / scalar ----- */
285
287 template<class K>
288 inline FieldVector<K,1> operator+ (const FieldVector<K,1>& a, const K b)
289 {
290 return a[0]+b;
291 }
292
294 template<class K>
295 inline FieldVector<K,1> operator- (const FieldVector<K,1>& a, const K b)
296 {
297 return a[0]-b;
298 }
299
301 template<class K>
302 inline FieldVector<K,1> operator* (const FieldVector<K,1>& a, const K b)
303 {
304 return a[0]*b;
305 }
306
308 template<class K>
309 inline FieldVector<K,1> operator/ (const FieldVector<K,1>& a, const K b)
310 {
311 return a[0]/b;
312 }
313
315 template<class K>
316 inline bool operator> (const FieldVector<K,1>& a, const K b)
317 {
318 return a[0]>b;
319 }
320
322 template<class K>
323 inline bool operator>= (const FieldVector<K,1>& a, const K b)
324 {
325 return a[0]>=b;
326 }
327
329 template<class K>
330 inline bool operator< (const FieldVector<K,1>& a, const K b)
331 {
332 return a[0]<b;
333 }
334
336 template<class K>
337 inline bool operator<= (const FieldVector<K,1>& a, const K b)
338 {
339 return a[0]<=b;
340 }
341
343 template<class K>
344 inline bool operator== (const FieldVector<K,1>& a, const K b)
345 {
346 return a[0]==b;
347 }
348
350 template<class K>
351 inline bool operator!= (const FieldVector<K,1>& a, const K b)
352 {
353 return a[0]!=b;
354 }
355
356 /* ----- scalar / FV ------ */
357
359 template<class K>
360 inline FieldVector<K,1> operator+ (const K a, const FieldVector<K,1>& b)
361 {
362 return a+b[0];
363 }
364
366 template<class K>
367 inline FieldVector<K,1> operator- (const K a, const FieldVector<K,1>& b)
368 {
369 return a-b[0];
370 }
371
373 template<class K>
374 inline FieldVector<K,1> operator* (const K a, const FieldVector<K,1>& b)
375 {
376 return a*b[0];
377 }
378
380 template<class K>
381 inline FieldVector<K,1> operator/ (const K a, const FieldVector<K,1>& b)
382 {
383 return a/b[0];
384 }
385
387 template<class K>
388 inline bool operator> (const K a, const FieldVector<K,1>& b)
389 {
390 return a>b[0];
391 }
392
394 template<class K>
395 inline bool operator>= (const K a, const FieldVector<K,1>& b)
396 {
397 return a>=b[0];
398 }
399
401 template<class K>
402 inline bool operator< (const K a, const FieldVector<K,1>& b)
403 {
404 return a<b[0];
405 }
406
408 template<class K>
409 inline bool operator<= (const K a, const FieldVector<K,1>& b)
410 {
411 return a<=b[0];
412 }
413
415 template<class K>
416 inline bool operator== (const K a, const FieldVector<K,1>& b)
417 {
418 return a==b[0];
419 }
420
422 template<class K>
423 inline bool operator!= (const K a, const FieldVector<K,1>& b)
424 {
425 return a!=b[0];
426 }
427#endif
428
431} // end namespace
432
433#endif
Fallback implementation of the std::array class (a static array)
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:223
Traits::value_type value_type
export the type representing the field
Definition: densevector.hh:245
size_type size() const
size method
Definition: densevector.hh:285
derived_type & operator=(const value_type &k)
Assignment operator for scalar.
Definition: densevector.hh:264
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:254
vector space out of a tensor product of fields.
Definition: fvector.hh:92
FieldVector(const K &t)
Constructor making vector with identical coordinates.
Definition: fvector.hh:123
FieldVector(const FieldVector &x)
Constructor making vector with identical coordinates.
Definition: fvector.hh:129
@ dimension
The size of this vector.
Definition: fvector.hh:99
FieldVector(const FieldVector< K1, SIZE1 > &x)
Constructor making vector with identical coordinates.
Definition: fvector.hh:155
FieldVector()
Constructor making default-initialized vector.
Definition: fvector.hh:106
FieldVector(const DenseVector< C > &x, typename Dune::enable_if< IsFieldVectorSizeCorrect< C, SIZE >::value >::type *dummy=0)
Copy constructor from a second vector of possibly different type.
Definition: fvector.hh:144
Implements the dense vector interface, with an exchangeable storage class.
#define dune_static_assert(COND, MSG)
Helper template so that compilation fails if condition is not true.
Definition: static_assert.hh:79
std::istream & operator>>(std::istream &is, Pair< T1, T2 > &pair)
Read a pair or tuple.
Definition: tuples.hh:949
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:231
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:253
Dune namespace.
Definition: alignment.hh:14
Fallback implementation of the C++0x static_assert feature.
TMP to check the size of a DenseVectors statically, if possible.
Definition: fvector.hh:61
@ value
Definition: fvector.hh:67
Enable typedef if condition is met.
Definition: typetraits.hh:329
Traits for type conversions and type information.
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentional unused function parameters with.
Definition: unused.hh:18
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)