dune-common 3.0-git
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 >
38 {
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 {
50 };
51
60 template<typename C, int SIZE>
62 {
63 enum {
68 value = true
69 };
70 };
71
72 template<typename T, int SIZE>
74 {
75 enum {value = true};
76 };
77
78 template<typename T, int SIZE, int SIZE1>
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
108
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 constexpr size_type size () const { return SIZE; }
169 DUNE_ASSERT_BOUNDS(i < SIZE);
170 return _data[i];
171 }
172 const K & operator[](size_type i) const {
173 DUNE_ASSERT_BOUNDS(i < SIZE);
174 return _data[i];
175 }
176 };
177
189 template<class K, int SIZE>
190 inline std::istream &operator>> ( std::istream &in,
192 {
194 for( typename FieldVector<K, SIZE>::size_type i = 0; i < SIZE; ++i )
195 in >> w[ i ];
196 if(in)
197 v = w;
198 return in;
199 }
200
201#ifndef DOXYGEN
202 template< class K >
203 struct DenseMatVecTraits< FieldVector<K,1> >
204 {
205 typedef FieldVector<K,1> derived_type;
206 typedef K container_type;
207 typedef K value_type;
208 typedef size_t size_type;
209 };
210
213 template<class K>
214 class FieldVector<K, 1> :
215 public DenseVector< FieldVector<K,1> >
216 {
217 K _data;
218 typedef DenseVector< FieldVector<K,1> > Base;
219 public:
221 enum {
223 dimension = 1
224 };
225
226 typedef typename Base::size_type size_type;
227
229 typedef K& reference;
230
232 typedef const K& const_reference;
233
234 //===== construction
235
237 constexpr FieldVector ()
238 : _data()
239 {}
240
242 template<typename T,
243 typename EnableIf = typename std::enable_if<
244 std::is_convertible<T, K>::value &&
245 ! std::is_same<K, DenseVector<typename FieldTraits<T>::field_type>
246 >::value
247 >::type
248 >
249 FieldVector (const T& k) : _data(k) {}
250
252 template<class C>
253 FieldVector (const DenseVector<C> & x)
254 {
255 static_assert(((bool)IsFieldVectorSizeCorrect<C,1>::value), "FieldVectors do not match in dimension!");
256 assert(x.size() == 1);
257 _data = x[0];
258 }
259
261 FieldVector ( const FieldVector &other )
262 : Base(), _data( other._data )
263 {}
264
266 FieldVector (std::initializer_list<K> const &l)
267 {
268 assert(l.size() == 1);
269 _data = *l.begin();
270 }
271
273 template<typename T,
274 typename EnableIf = typename std::enable_if<
275 std::is_convertible<T, K>::value &&
276 ! std::is_same<K, DenseVector<typename FieldTraits<T>::field_type>
277 >::value
278 >::type
279 >
280 inline FieldVector& operator= (const T& k)
281 {
282 _data = k;
283 return *this;
284 }
285
286 //===== forward methods to container
287 constexpr size_type size () const { return 1; }
288 K & operator[](size_type i)
289 {
291 DUNE_ASSERT_BOUNDS(i == 0);
292 return _data;
293 }
294 const K & operator[](size_type i) const
295 {
297 DUNE_ASSERT_BOUNDS(i == 0);
298 return _data;
299 }
300
301 //===== conversion operator
302
304 operator K& () { return _data; }
305
307 operator const K& () const { return _data; }
308 };
309
310 /* ----- FV / FV ----- */
311 /* mostly not necessary as these operations are already covered via the cast operator */
312
314 template<class K>
315 inline bool operator> (const FieldVector<K,1>& a, const FieldVector<K,1>& b)
316 {
317 return a[0]>b[0];
318 }
319
321 template<class K>
322 inline bool operator>= (const FieldVector<K,1>& a, const FieldVector<K,1>& b)
323 {
324 return a[0]>=b[0];
325 }
326
328 template<class K>
329 inline bool operator< (const FieldVector<K,1>& a, const FieldVector<K,1>& b)
330 {
331 return a[0]<b[0];
332 }
333
335 template<class K>
336 inline bool operator<= (const FieldVector<K,1>& a, const FieldVector<K,1>& b)
337 {
338 return a[0]<=b[0];
339 }
340
341 /* ----- FV / scalar ----- */
342
344 template<class K>
345 inline FieldVector<K,1> operator+ (const FieldVector<K,1>& a, const K b)
346 {
347 return a[0]+b;
348 }
349
351 template<class K>
352 inline FieldVector<K,1> operator- (const FieldVector<K,1>& a, const K b)
353 {
354 return a[0]-b;
355 }
356
358 template<class K>
359 inline FieldVector<K,1> operator* (const FieldVector<K,1>& a, const K b)
360 {
361 return a[0]*b;
362 }
363
365 template<class K>
366 inline FieldVector<K,1> operator/ (const FieldVector<K,1>& a, const K b)
367 {
368 return a[0]/b;
369 }
370
372 template<class K>
373 inline bool operator> (const FieldVector<K,1>& a, const K b)
374 {
375 return a[0]>b;
376 }
377
379 template<class K>
380 inline bool operator>= (const FieldVector<K,1>& a, const K b)
381 {
382 return a[0]>=b;
383 }
384
386 template<class K>
387 inline bool operator< (const FieldVector<K,1>& a, const K b)
388 {
389 return a[0]<b;
390 }
391
393 template<class K>
394 inline bool operator<= (const FieldVector<K,1>& a, const K b)
395 {
396 return a[0]<=b;
397 }
398
400 template<class K>
401 inline bool operator== (const FieldVector<K,1>& a, const K b)
402 {
403 return a[0]==b;
404 }
405
407 template<class K>
408 inline bool operator!= (const FieldVector<K,1>& a, const K b)
409 {
410 return a[0]!=b;
411 }
412
413 /* ----- scalar / FV ------ */
414
416 template<class K>
417 inline FieldVector<K,1> operator+ (const K a, const FieldVector<K,1>& b)
418 {
419 return a+b[0];
420 }
421
423 template<class K>
424 inline FieldVector<K,1> operator- (const K a, const FieldVector<K,1>& b)
425 {
426 return a-b[0];
427 }
428
430 template<class K>
431 inline FieldVector<K,1> operator* (const K a, const FieldVector<K,1>& b)
432 {
433 return a*b[0];
434 }
435
437 template<class K>
438 inline FieldVector<K,1> operator/ (const K a, const FieldVector<K,1>& b)
439 {
440 return a/b[0];
441 }
442
444 template<class K>
445 inline bool operator> (const K a, const FieldVector<K,1>& b)
446 {
447 return a>b[0];
448 }
449
451 template<class K>
452 inline bool operator>= (const K a, const FieldVector<K,1>& b)
453 {
454 return a>=b[0];
455 }
456
458 template<class K>
459 inline bool operator< (const K a, const FieldVector<K,1>& b)
460 {
461 return a<b[0];
462 }
463
465 template<class K>
466 inline bool operator<= (const K a, const FieldVector<K,1>& b)
467 {
468 return a<=b[0];
469 }
470
472 template<class K>
473 inline bool operator== (const K a, const FieldVector<K,1>& b)
474 {
475 return a==b[0];
476 }
477
479 template<class K>
480 inline bool operator!= (const K a, const FieldVector<K,1>& b)
481 {
482 return a!=b[0];
483 }
484#endif
485
488} // end namespace
489
490#endif
#define DUNE_ASSERT_BOUNDS(cond)
Definition boundschecking.hh:20
Traits for type conversions and type information.
A few common exception classes.
Type traits to determine the type of reals (when working with complex numbers)
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
Implements the dense vector interface, with an exchangeable storage class.
Dune namespace.
Definition alignment.hh:11
constexpr auto size(const Dune::FieldVector< T, i > *, const PriorityTag< 5 > &) -> decltype(std::integral_constant< std::size_t, i >())
Definition hybridutilities.hh:22
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
Base::size_type size_type
Definition fvector.hh:103
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
constexpr size_type size() const
Definition fvector.hh:167
K & operator[](size_type i)
Definition fvector.hh:168
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
Base::value_type value_type
Definition fvector.hh:104
const K & operator[](size_type i) const
Definition fvector.hh:172
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
Definition ftraits.hh:24
T field_type
export the type representing the field
Definition ftraits.hh:26
T real_type
export the type representing the real type of the field
Definition ftraits.hh:28
std::array< K, SIZE > container_type
Definition fvector.hh:40
container_type::size_type size_type
Definition fvector.hh:42
FieldVector< K, SIZE > derived_type
Definition fvector.hh:39
FieldTraits< K >::real_type real_type
Definition fvector.hh:49
FieldTraits< K >::field_type field_type
Definition fvector.hh:48
TMP to check the size of a DenseVectors statically, if possible.
Definition fvector.hh:62
@ value
Definition fvector.hh:68
Definition matvectraits.hh:29