dune-common  2.2.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: fvector.hh 6785 2012-05-31 22:07:47Z sander $
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 
13 #include "typetraits.hh"
14 #include "exceptions.hh"
15 #include "array.hh"
16 #include "densevector.hh"
17 #include "static_assert.hh"
18 
19 #if ! DUNE_COMMON_FIELDVECTOR_SIZE_IS_METHOD
20 #warning The FieldVector class exports its size by the enum member 'size'. \
21  This behavior is deprecated. In the future, 'size' will be a method, \
22  which puts it in compliance with the stl conventions. To enable the new behavior \
23  (and get rid of this warning), build your Dune with --enable-fieldvector-size-is-method. \
24  If you do need the vector size as an enum, use 'dimension'.
25 #endif
26 
27 
28 namespace Dune {
29 
39  template< class K, int SIZE > class FieldVector;
40  template< class K, int SIZE >
41  struct DenseMatVecTraits< FieldVector<K,SIZE> >
42  {
45  typedef K value_type;
47  };
48 
49  template< class K, int SIZE >
50  struct FieldTraits< FieldVector<K,SIZE> >
51  {
54  };
55 
64  template<typename C, int SIZE>
66  {
67  enum{
72  value = true};
73  };
74 
75  template<typename T, int SIZE>
77  {
78  enum{value = true};
79  };
80 
81  template<typename T, int SIZE, int SIZE1>
82  struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE1>,SIZE>
83  {
84  enum{value = false};
85  };
86 
87 
93  template< class K, int SIZE >
94  class FieldVector :
95  public DenseVector< FieldVector<K,SIZE> >
96  {
97  Dune::array<K,SIZE> _data;
98  typedef DenseVector< FieldVector<K,SIZE> > Base;
99  public:
101  enum {
103  dimension = SIZE
104 #if ! DUNE_COMMON_FIELDVECTOR_SIZE_IS_METHOD
105  ,
107  size = SIZE
108 #endif
109  };
110 
111  typedef typename Base::size_type size_type;
112  typedef typename Base::value_type value_type;
113 
116 
118  explicit FieldVector (const K& t)
119  {
120  fill(t);
121  }
122 
124  FieldVector (const FieldVector & x) : _data(x._data)
125  {}
126 
138  template<class C>
140  {
141  // do a run-time size check, for the case that x is not a FieldVector
142  assert(x.size() == SIZE);
143  for (size_type i = 0; i<SIZE; i++)
144  _data[i] = x[i];
145  }
146 
148  template<class K1, int SIZE1>
149  explicit FieldVector (const FieldVector<K1,SIZE1> & x)
150  {
151  dune_static_assert(SIZE1 == SIZE, "FieldVector in constructor has wrong size");
152  for (size_type i = 0; i<SIZE; i++)
153  _data[i] = x[i];
154  }
155  using Base::operator=;
156 
157  // make this thing a vector
158  size_type vec_size() const { return SIZE; }
159  K & vec_access(size_type i) { return _data[i]; }
160  const K & vec_access(size_type i) const { return _data[i]; }
161  private:
162  void fill(const K& t)
163  {
164  for (int i=0; i<SIZE; i++) _data[i]=t;
165  }
166  };
167 
179  template<class K, int SIZE>
180  inline std::istream &operator>> ( std::istream &in,
182  {
184  for( typename FieldVector<K, SIZE>::size_type i = 0; i < SIZE; ++i )
185  in >> w[ i ];
186  if(in)
187  v = w;
188  return in;
189  }
190 
191 #ifndef DOXYGEN
192  template< class K >
193  struct DenseMatVecTraits< FieldVector<K,1> >
194  {
195  typedef FieldVector<K,1> derived_type;
196  typedef K container_type;
197  typedef K value_type;
198  typedef size_t size_type;
199  };
200 
203  template<class K>
204  class FieldVector<K, 1> :
205  public DenseVector< FieldVector<K,1> >
206  {
207  K _data;
208  typedef DenseVector< FieldVector<K,1> > Base;
209  public:
211  enum {
213  dimension = 1
214 #if ! DUNE_COMMON_FIELDVECTOR_SIZE_IS_METHOD
215  ,
217  size = 1
218 #endif
219  };
220 
221  typedef typename Base::size_type size_type;
222 
223  //===== construction
224 
226  FieldVector () {}
227 
229  FieldVector (const K& k) : _data(k) {}
230 
232  template<class C>
233  FieldVector (const DenseVector<C> & x)
234  {
235  dune_static_assert(((bool)IsFieldVectorSizeCorrect<C,1>::value), "FieldVectors do not match in dimension!");
236  assert(x.size() == 1);
237  _data = x[0];
238  }
239 
241  inline FieldVector& operator= (const K& k)
242  {
243  _data = k;
244  return *this;
245  }
246 
247  //===== forward methods to container
248  size_type vec_size() const { return 1; }
249  K & vec_access(size_type i)
250  {
251  assert(i == 0);
252  return _data;
253  }
254  const K & vec_access(size_type i) const
255  {
256  assert(i == 0);
257  return _data;
258  }
259 
260  //===== conversion operator
261 
263  operator K () { return _data; }
264 
266  operator K () const { return _data; }
267  };
268 
269  /* ----- FV / FV ----- */
270  /* not necessary as these operations are already covered via the cast operator */
271 
272  /* ----- FV / scalar ----- */
273 
275  template<class K>
276  inline FieldVector<K,1> operator+ (const FieldVector<K,1>& a, const K b)
277  {
278  return a[0]+b;
279  }
280 
282  template<class K>
283  inline FieldVector<K,1> operator- (const FieldVector<K,1>& a, const K b)
284  {
285  return a[0]-b;
286  }
287 
289  template<class K>
290  inline FieldVector<K,1> operator* (const FieldVector<K,1>& a, const K b)
291  {
292  return a[0]*b;
293  }
294 
296  template<class K>
297  inline FieldVector<K,1> operator/ (const FieldVector<K,1>& a, const K b)
298  {
299  return a[0]/b;
300  }
301 
303  template<class K>
304  inline bool operator> (const FieldVector<K,1>& a, const K b)
305  {
306  return a[0]>b;
307  }
308 
310  template<class K>
311  inline bool operator>= (const FieldVector<K,1>& a, const K b)
312  {
313  return a[0]>=b;
314  }
315 
317  template<class K>
318  inline bool operator< (const FieldVector<K,1>& a, const K b)
319  {
320  return a[0]<b;
321  }
322 
324  template<class K>
325  inline bool operator<= (const FieldVector<K,1>& a, const K b)
326  {
327  return a[0]<=b;
328  }
329 
331  template<class K>
332  inline bool operator== (const FieldVector<K,1>& a, const K b)
333  {
334  return a[0]==b;
335  }
336 
338  template<class K>
339  inline bool operator!= (const FieldVector<K,1>& a, const K b)
340  {
341  return a[0]!=b;
342  }
343 
344  /* ----- scalar / FV ------ */
345 
347  template<class K>
348  inline FieldVector<K,1> operator+ (const K a, const FieldVector<K,1>& b)
349  {
350  return a+b[0];
351  }
352 
354  template<class K>
355  inline FieldVector<K,1> operator- (const K a, const FieldVector<K,1>& b)
356  {
357  return a-b[0];
358  }
359 
361  template<class K>
362  inline FieldVector<K,1> operator* (const K a, const FieldVector<K,1>& b)
363  {
364  return a*b[0];
365  }
366 
368  template<class K>
369  inline FieldVector<K,1> operator/ (const K a, const FieldVector<K,1>& b)
370  {
371  return a/b[0];
372  }
373 
375  template<class K>
376  inline bool operator> (const K a, const FieldVector<K,1>& b)
377  {
378  return a>b[0];
379  }
380 
382  template<class K>
383  inline bool operator>= (const K a, const FieldVector<K,1>& b)
384  {
385  return a>=b[0];
386  }
387 
389  template<class K>
390  inline bool operator< (const K a, const FieldVector<K,1>& b)
391  {
392  return a<b[0];
393  }
394 
396  template<class K>
397  inline bool operator<= (const K a, const FieldVector<K,1>& b)
398  {
399  return a<=b[0];
400  }
401 
403  template<class K>
404  inline bool operator== (const K a, const FieldVector<K,1>& b)
405  {
406  return a==b[0];
407  }
408 
410  template<class K>
411  inline bool operator!= (const K a, const FieldVector<K,1>& b)
412  {
413  return a!=b[0];
414  }
415 #endif
416 
419 } // end namespace
420 
421 #endif