Dune Core Modules (2.5.0)

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#ifndef DUNE_FVECTOR_HH
4#define DUNE_FVECTOR_HH
5
6#include <array>
7#include <cmath>
8#include <cstddef>
9#include <cstdlib>
10#include <complex>
11#include <cstring>
12#include <utility>
13#include <initializer_list>
14#include <algorithm>
15
16#include "typetraits.hh"
17#include "exceptions.hh"
18
19#include "ftraits.hh"
20#include "densevector.hh"
21#include "unused.hh"
22#include "boundschecking.hh"
23
24namespace Dune {
25
35 template< class K, int SIZE > class FieldVector;
36 template< class K, int SIZE >
37 struct DenseMatVecTraits< FieldVector<K,SIZE> >
38 {
39 typedef FieldVector<K,SIZE> derived_type;
40 typedef std::array<K,SIZE> container_type;
41 typedef K value_type;
42 typedef typename container_type::size_type size_type;
43 };
44
45 template< class K, int SIZE >
46 struct FieldTraits< FieldVector<K,SIZE> >
47 {
48 typedef typename FieldTraits<K>::field_type field_type;
49 typedef typename FieldTraits<K>::real_type real_type;
50 };
51
60 template<typename C, int SIZE>
62 {
63 enum {
68 value = true
69 };
70 };
71
72 template<typename T, int SIZE>
73 struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE>,SIZE>
74 {
75 enum {value = true};
76 };
77
78 template<typename T, int SIZE, int SIZE1>
79 struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE1>,SIZE>
80 {
81 enum {value = false};
82 };
83
84
90 template< class K, int SIZE >
92 public DenseVector< FieldVector<K,SIZE> >
93 {
94 std::array<K,SIZE> _data;
96 public:
98 enum {
100 dimension = SIZE
101 };
102
103 typedef typename Base::size_type size_type;
104 typedef typename Base::value_type value_type;
105
107 typedef value_type& reference;
108
110 typedef const value_type& const_reference;
111
113 constexpr FieldVector()
114 : _data{{}}
115 {}
116
118 explicit FieldVector (const K& t)
119 {
120 std::fill(_data.begin(),_data.end(),t);
121 }
122
124 FieldVector (const FieldVector & x) : Base(), _data(x._data)
125 {}
126
128 FieldVector (std::initializer_list<K> const &l)
129 {
130 assert(l.size() == dimension);// Actually, this is not needed any more!
131 std::copy_n(l.begin(), std::min(static_cast<std::size_t>(dimension),
132 l.size()),
133 _data.begin());
134 }
135
147 template<class C>
148 FieldVector (const DenseVector<C> & x, typename std::enable_if<IsFieldVectorSizeCorrect<C,SIZE>::value>::type* dummy=0 )
149 {
151 // do a run-time size check, for the case that x is not a FieldVector
152 assert(x.size() == SIZE); // Actually this is not needed any more!
153 std::copy_n(x.begin(), std::min(static_cast<std::size_t>(SIZE),x.size()), _data.begin());
154 }
155
157 template<class K1, int SIZE1>
159 {
160 static_assert(SIZE1 == SIZE, "FieldVector in constructor has wrong size");
161 for (size_type i = 0; i<SIZE; i++)
162 _data[i] = x[i];
163 }
164 using Base::operator=;
165
166 // make this thing a vector
167 static constexpr size_type size () { return SIZE; }
168
169 K & operator[](size_type i) {
170 DUNE_ASSERT_BOUNDS(i < SIZE);
171 return _data[i];
172 }
173 const K & operator[](size_type i) const {
174 DUNE_ASSERT_BOUNDS(i < SIZE);
175 return _data[i];
176 }
177 };
178
190 template<class K, int SIZE>
191 inline std::istream &operator>> ( std::istream &in,
193 {
195 for( typename FieldVector<K, SIZE>::size_type i = 0; i < SIZE; ++i )
196 in >> w[ i ];
197 if(in)
198 v = w;
199 return in;
200 }
201
202#ifndef DOXYGEN
203 template< class K >
204 struct DenseMatVecTraits< FieldVector<K,1> >
205 {
206 typedef FieldVector<K,1> derived_type;
207 typedef K container_type;
208 typedef K value_type;
209 typedef size_t size_type;
210 };
211
214 template<class K>
215 class FieldVector<K, 1> :
216 public DenseVector< FieldVector<K,1> >
217 {
218 K _data;
219 typedef DenseVector< FieldVector<K,1> > Base;
220 public:
222 enum {
224 dimension = 1
225 };
226
227 typedef typename Base::size_type size_type;
228
230 typedef K& reference;
231
233 typedef const K& const_reference;
234
235 //===== construction
236
238 constexpr FieldVector ()
239 : _data()
240 {}
241
243 template<typename T,
244 typename EnableIf = typename std::enable_if<
245 std::is_convertible<T, K>::value &&
246 ! std::is_same<K, DenseVector<typename FieldTraits<T>::field_type>
247 >::value
248 >::type
249 >
250 FieldVector (const T& k) : _data(k) {}
251
253 template<class C>
254 FieldVector (const DenseVector<C> & x)
255 {
256 static_assert(((bool)IsFieldVectorSizeCorrect<C,1>::value), "FieldVectors do not match in dimension!");
257 assert(x.size() == 1);
258 _data = x[0];
259 }
260
262 FieldVector ( const FieldVector &other )
263 : Base(), _data( other._data )
264 {}
265
267 FieldVector (std::initializer_list<K> const &l)
268 {
269 assert(l.size() == 1);
270 _data = *l.begin();
271 }
272
274 template<typename T,
275 typename EnableIf = typename std::enable_if<
276 std::is_convertible<T, K>::value &&
277 ! std::is_same<K, DenseVector<typename FieldTraits<T>::field_type>
278 >::value
279 >::type
280 >
281 inline FieldVector& operator= (const T& k)
282 {
283 _data = k;
284 return *this;
285 }
286
287 //===== forward methods to container
288 static constexpr size_type size () { return 1; }
289
290 K & operator[](size_type i)
291 {
293 DUNE_ASSERT_BOUNDS(i == 0);
294 return _data;
295 }
296 const K & operator[](size_type i) const
297 {
299 DUNE_ASSERT_BOUNDS(i == 0);
300 return _data;
301 }
302
303 //===== conversion operator
304
306 operator K& () { return _data; }
307
309 operator const K& () const { return _data; }
310 };
311
312 /* ----- FV / FV ----- */
313 /* mostly not necessary as these operations are already covered via the cast operator */
314
316 template<class K>
317 inline bool operator> (const FieldVector<K,1>& a, const FieldVector<K,1>& b)
318 {
319 return a[0]>b[0];
320 }
321
323 template<class K>
324 inline bool operator>= (const FieldVector<K,1>& a, const FieldVector<K,1>& b)
325 {
326 return a[0]>=b[0];
327 }
328
330 template<class K>
331 inline bool operator< (const FieldVector<K,1>& a, const FieldVector<K,1>& b)
332 {
333 return a[0]<b[0];
334 }
335
337 template<class K>
338 inline bool operator<= (const FieldVector<K,1>& a, const FieldVector<K,1>& b)
339 {
340 return a[0]<=b[0];
341 }
342
343 /* ----- FV / scalar ----- */
344
346 template<class K>
347 inline FieldVector<K,1> operator+ (const FieldVector<K,1>& a, const K b)
348 {
349 return a[0]+b;
350 }
351
353 template<class K>
354 inline FieldVector<K,1> operator- (const FieldVector<K,1>& a, const K b)
355 {
356 return a[0]-b;
357 }
358
360 template<class K>
361 inline FieldVector<K,1> operator* (const FieldVector<K,1>& a, const K b)
362 {
363 return a[0]*b;
364 }
365
367 template<class K>
368 inline FieldVector<K,1> operator/ (const FieldVector<K,1>& a, const K b)
369 {
370 return a[0]/b;
371 }
372
374 template<class K>
375 inline bool operator> (const FieldVector<K,1>& a, const K b)
376 {
377 return a[0]>b;
378 }
379
381 template<class K>
382 inline bool operator>= (const FieldVector<K,1>& a, const K b)
383 {
384 return a[0]>=b;
385 }
386
388 template<class K>
389 inline bool operator< (const FieldVector<K,1>& a, const K b)
390 {
391 return a[0]<b;
392 }
393
395 template<class K>
396 inline bool operator<= (const FieldVector<K,1>& a, const K b)
397 {
398 return a[0]<=b;
399 }
400
402 template<class K>
403 inline bool operator== (const FieldVector<K,1>& a, const K b)
404 {
405 return a[0]==b;
406 }
407
409 template<class K>
410 inline bool operator!= (const FieldVector<K,1>& a, const K b)
411 {
412 return a[0]!=b;
413 }
414
415 /* ----- scalar / FV ------ */
416
418 template<class K>
419 inline FieldVector<K,1> operator+ (const K a, const FieldVector<K,1>& b)
420 {
421 return a+b[0];
422 }
423
425 template<class K>
426 inline FieldVector<K,1> operator- (const K a, const FieldVector<K,1>& b)
427 {
428 return a-b[0];
429 }
430
432 template<class K>
433 inline FieldVector<K,1> operator* (const K a, const FieldVector<K,1>& b)
434 {
435 return a*b[0];
436 }
437
439 template<class K>
440 inline FieldVector<K,1> operator/ (const K a, const FieldVector<K,1>& b)
441 {
442 return a/b[0];
443 }
444
446 template<class K>
447 inline bool operator> (const K a, const FieldVector<K,1>& b)
448 {
449 return a>b[0];
450 }
451
453 template<class K>
454 inline bool operator>= (const K a, const FieldVector<K,1>& b)
455 {
456 return a>=b[0];
457 }
458
460 template<class K>
461 inline bool operator< (const K a, const FieldVector<K,1>& b)
462 {
463 return a<b[0];
464 }
465
467 template<class K>
468 inline bool operator<= (const K a, const FieldVector<K,1>& b)
469 {
470 return a<=b[0];
471 }
472
474 template<class K>
475 inline bool operator== (const K a, const FieldVector<K,1>& b)
476 {
477 return a==b[0];
478 }
479
481 template<class K>
482 inline bool operator!= (const K a, const FieldVector<K,1>& b)
483 {
484 return a!=b[0];
485 }
486#endif
487
490} // end namespace
491
492#endif
Macro for wrapping boundary checks.
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:235
Traits::value_type value_type
export the type representing the field
Definition: densevector.hh:257
Iterator begin()
begin iterator
Definition: densevector.hh:308
size_type size() const
size method
Definition: densevector.hh:297
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:266
vector space out of a tensor product of fields.
Definition: fvector.hh:93
constexpr FieldVector()
Constructor making default-initialized vector.
Definition: fvector.hh:113
const value_type & const_reference
The type used for const references to the vector entry.
Definition: fvector.hh:110
@ dimension
The size of this vector.
Definition: fvector.hh:100
FieldVector(const DenseVector< C > &x, typename std::enable_if< IsFieldVectorSizeCorrect< C, SIZE >::value >::type *dummy=0)
Copy constructor from a second vector of possibly different type.
Definition: fvector.hh:148
FieldVector(const K &t)
Constructor making vector with identical coordinates.
Definition: fvector.hh:118
FieldVector(std::initializer_list< K > const &l)
Construct from a std::initializer_list.
Definition: fvector.hh:128
FieldVector(const FieldVector &x)
Copy constructor.
Definition: fvector.hh:124
value_type & reference
The type used for references to the vector entry.
Definition: fvector.hh:107
FieldVector(const FieldVector< K1, SIZE1 > &x)
Constructor making vector with identical coordinates.
Definition: fvector.hh:158
Implements the dense vector interface, with an exchangeable storage class.
Type traits to determine the type of reals (when working with complex numbers)
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
std::istream & operator>>(std::istream &in, DynamicVector< K, Allocator > &v)
Read a DynamicVector from an input stream.
Definition: dynvector.hh:170
EnableIfInterOperable< T1, T2, bool >::type operator<(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:629
EnableIfInterOperable< T1, T2, bool >::type operator>(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:675
EnableIfInterOperable< T1, T2, bool >::type operator<=(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:652
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:233
EnableIfInterOperable< T1, T2, bool >::type operator>=(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:697
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:255
Dune namespace.
Definition: alignment.hh:11
TMP to check the size of a DenseVectors statically, if possible.
Definition: fvector.hh:62
@ value
Definition: fvector.hh:68
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 intentionally 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 23, 23:29, 2024)