dune-fem  2.4.1-rc
vector.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_VECTOR_HH
2 #define DUNE_FEM_VECTOR_HH
3 
4 #include <dune/common/math.hh>
5 #include <dune/common/typetraits.hh>
6 #include <dune/common/bartonnackmanifcheck.hh>
7 #include <dune/common/fvector.hh>
8 
9 
13 
18 namespace Dune
19 {
20 
21  namespace Fem
22  {
23 
24  template< class VT >
26  {
27  typedef typename VT::VectorType ArrayType;
28  typedef typename VT::FieldType ElementType;
29 
30  typedef typename VT::ConstIteratorType ConstIteratorType;
31  typedef typename VT::IteratorType IteratorType;
32  };
33 
34 
36  template< class VT >
38  : public ArrayInterface< VectorInterfaceArrayTraits< VT > >
39  {
42 
43  template< class > friend class VectorInterface;
44 
45  public:
46  typedef VT Traits;
47 
49  typedef ThisType VectorInterfaceType;
50 
52  typedef typename Traits::VectorType VectorType;
53 
55  typedef typename Traits::FieldType FieldType;
56  typedef FieldType value_type;
57 
60 
63 
64  public:
66  template< class T >
67  VectorType& operator= ( const VectorInterface< T > &v );
68 
70  VectorType& operator= ( const ThisType &v );
71 
73  VectorType &operator= ( const FieldType s );
74 
76  const FieldType &operator[] ( unsigned int index ) const;
77 
79  FieldType &operator[] ( unsigned int index );
80 
82  template< class T >
83  VectorType &operator+= ( const VectorInterface< T > &v );
84 
86  template< class T >
87  VectorType &operator-= ( const VectorInterface< T > &v );
88 
90  VectorType &operator*= ( const FieldType s );
91 
93  template< class T >
94  VectorType &addScaled ( const FieldType s,
95  const VectorInterface< T > &v );
96 
104  template< class T >
105  void assign ( const VectorInterface< T > &v );
106 
108  void assign ( const FieldType s );
109 
111  void clear ();
112 
114  ConstIteratorType begin () const;
115 
117  IteratorType begin ();
118 
120  ConstIteratorType end () const;
121 
123  IteratorType end ();
124 
126  unsigned int size () const;
127 
128  protected:
129  using BaseType::asImp;
130  };
131 
132 
133  template< class Vector >
135  {
137  static const bool v = Conversion< Vector, VectorInterfaceType >::exists;
138  };
139 
140 
141  template< class V, class W >
143  {
144  typedef typename V::FieldType FieldType;
145 
146  private:
147  static_assert( (Conversion< FieldType, typename W::FieldType >::sameType),
148  "FieldType must be identical." );
149  };
150 
151 
152 
153  template< class Field, class Vector >
155  {
156  typedef Field FieldType;
157  typedef Vector VectorType;
158 
161  };
162 
163 
164 
165  // VectorDefault
166  // -------------
167 
172  template< class Field, class Vector >
174  : public VectorInterface< VectorDefaultTraits< Field, Vector > >
175  {
178 
179  public:
180  typedef typename BaseType :: FieldType FieldType;
181 
182  typedef typename BaseType :: VectorInterfaceType VectorInterfaceType;
183  typedef typename BaseType :: VectorType VectorType;
184 
185  typedef typename BaseType :: ConstIteratorType ConstIteratorType;
186  typedef typename BaseType :: IteratorType IteratorType;
187 
188  public:
190  template< class T >
191  VectorType &operator+= ( const VectorInterface< T > &v )
192  {
193  const unsigned int size = this->size();
194  assert( size == v.size() );
195  for( unsigned int i = 0; i < size; ++i )
196  (*this)[ i ] += v[ i ];
197  return asImp();
198  }
199 
201  template< class T >
202  VectorType &operator-= ( const VectorInterface< T > &v )
203  {
204  const unsigned int size = this->size();
205  assert( size == v.size() );
206  for( unsigned int i = 0; i < size; ++i )
207  (*this)[ i ] -= v[ i ];
208  return asImp();
209  }
210 
212  VectorType &operator*= ( const FieldType s )
213  {
214  const unsigned int size = this->size();
215  for( unsigned int i = 0; i < size; ++i )
216  (*this)[ i ] *= s;
217  return asImp();
218  }
219 
221  template< class T >
222  VectorType &addScaled ( const FieldType s, const VectorInterface< T > &v )
223  {
224  const unsigned int size = this->size();
225  assert( size == v.size() );
226  for( unsigned int i = 0; i < size; ++i )
227  (*this)[ i ] += s * v[ i ];
228  return asImp();
229  }
230 
232  template< class T >
233  void assign ( const VectorInterface< T > &v )
234  {
235  const unsigned int size = this->size();
236  assert( size == v.size() );
237  for( unsigned int i = 0; i < size; ++i )
238  asImp()[ i ] = v[ i ];
239  }
240 
242  void assign ( const FieldType s )
243  {
244  const unsigned int size = this->size();
245  for( unsigned int i = 0; i < size; ++i )
246  asImp()[ i ] = s;
247  }
248 
250  void clear ()
251  {
252  asImp().assign( 0 );
253  }
254 
255 
257  ConstIteratorType begin () const
258  {
259  return ConstIteratorType( asImp(), 0 );
260  }
261 
263  IteratorType begin ()
264  {
265  return IteratorType( asImp(), 0 );
266  }
267 
269  ConstIteratorType end () const
270  {
271  return ConstIteratorType( asImp(), size() );
272  }
273 
275  IteratorType end ()
276  {
277  return IteratorType( asImp(), size() );
278  }
279 
280  using BaseType :: size;
281 
282  protected:
283  using BaseType :: asImp;
284  };
285 
286 
287 
288  // FieldVectorAdapter
289  // ------------------
290 
291  template< class FieldVectorImp >
293 
294  template< class Field, int sz >
295  class FieldVectorAdapter< FieldVector< Field, sz > >
296  : public VectorDefault< Field, FieldVectorAdapter< FieldVector< Field, sz > > >
297  {
300 
301  public:
302  typedef Field FieldType;
303 
304  typedef FieldVector< FieldType, sz > FieldVectorType;
305 
306  using BaseType :: operator+=;
307  using BaseType :: operator-=;
308  using BaseType :: addScaled;
309  using BaseType :: assign;
310 
311  protected:
312  FieldVectorType fieldVector_;
313 
314  public:
316  : fieldVector_()
317  {}
318 
319  explicit FieldVectorAdapter ( const FieldType &s )
320  : fieldVector_( s )
321  {}
322 
323  explicit FieldVectorAdapter ( const FieldVectorType &v )
324  : fieldVector_( v )
325  {}
326 
327  template< class T >
329  : fieldVector_()
330  {
331  assign( v );
332  }
333 
334  FieldVectorAdapter ( const ThisType &other )
335  : fieldVector_( other.fieldVector_ )
336  {}
337 
338  public:
339  operator const FieldVectorType & () const
340  {
341  return fieldVector_;
342  }
343 
344  operator FieldVectorType & ()
345  {
346  return fieldVector_;
347  }
348 
349  template< class T >
350  ThisType &operator= ( const VectorInterface< T > &v )
351  {
352  assign( v );
353  return *this;
354  }
355 
356  ThisType &operator= ( const ThisType &v )
357  {
358  assign( v );
359  return *this;
360  }
361 
362  ThisType &operator= ( const FieldType &s )
363  {
364  return assign( s );
365  }
366 
367  const FieldType &operator[] ( unsigned int index ) const
368  {
369  return fieldVector_[ index ];
370  }
371 
372  FieldType &operator[] ( unsigned int index )
373  {
374  return fieldVector_[ index ];
375  }
376 
377  ThisType &operator+= ( const ThisType &v )
378  {
379  fieldVector_ += v.fieldVector_;
380  return *this;
381  }
382 
383  ThisType &operator+= ( const FieldVectorType &v )
384  {
385  fieldVector_ += v;
386  return *this;
387  }
388 
389  ThisType &operator-= ( const ThisType &v )
390  {
391  fieldVector_ -= v.fieldVector_;
392  return *this;
393  }
394 
395  ThisType &operator-= ( const FieldVectorType &v )
396  {
397  fieldVector_ -= v;
398  return *this;
399  }
400 
401  ThisType &operator*= ( const FieldType &s )
402  {
403  fieldVector_ *= s;
404  return *this;
405  }
406 
407  ThisType &addScaled ( const FieldType &s, const ThisType &other )
408  {
409  fieldVector_.axpy( s, other.fieldVector_ );
410  return *this;
411  }
412 
413  void assign ( const ThisType &other )
414  {
415  fieldVector_ = other.fieldVector_;
416  }
417 
418  void assign ( const FieldType &s )
419  {
420  fieldVector_ = s;
421  }
422 
423  unsigned int size () const
424  {
425  return FieldVectorType::dimension;
426  }
427 
428  static const ThisType &adapt ( const FieldVectorType &v )
429  {
430  return reinterpret_cast< const ThisType & >( v );
431  }
432 
433  static ThisType &adapt ( FieldVectorType &v )
434  {
435  return reinterpret_cast< ThisType & >( v );
436  }
437  };
438 
439 
440 
442  template< class FieldImp >
444  : public VectorDefault< FieldImp, ArrayWrapperVector< FieldImp > >
445  {
446  public:
448  typedef FieldImp FieldType;
449 
450  private:
453 
454  public:
455  using BaseType :: assign;
456 
457  protected:
458  const unsigned int size_;
459  FieldType *const fields_;
460 
461  public:
463  inline ArrayWrapperVector ( const unsigned int size,
464  FieldType *const fields )
465  : size_( size ),
466  fields_( fields )
467  {
468  }
469 
471  inline ArrayWrapperVector ( const unsigned int size,
472  FieldType *const fields,
473  const FieldType s )
474  : size_( size ),
475  fields_( fields )
476  {
477  assign( s );
478  }
479 
481  template< class T >
482  inline ArrayWrapperVector ( const unsigned int size,
483  FieldType *const fields,
484  const VectorInterface< T > &v )
485  : size_( size ),
486  fields_( fields )
487  {
488  assign( v );
489  }
490 
492  template< class T >
493  inline ThisType &operator= ( const VectorInterface< T > &v )
494  {
495  assign( v );
496  return *this;
497  }
498 
500  inline ThisType &operator= ( const ThisType &v )
501  {
502  assign( v );
503  return *this;
504  }
505 
507  inline ThisType &operator= ( const FieldType s )
508  {
509  assign( s );
510  return *this;
511  }
512 
513  inline const FieldType &operator[] ( unsigned int index ) const
514  {
515  assert( index < size_ );
516  return fields_[ index ];
517  }
518 
519  inline FieldType &operator[] ( unsigned int index )
520  {
521  assert( index < size_ );
522  return fields_[ index ];
523  }
524 
525  inline unsigned int size () const
526  {
527  return size_;
528  }
529  };
530 
531 
532 
540  template< class Field,
541  template< class > class ArrayAllocator = DefaultArrayAllocator >
543  : public VectorDefault< Field, DynamicVector< Field, ArrayAllocator > >
544  {
547 
548  public:
550  typedef Field FieldType;
551 
552  using BaseType :: assign;
553 
554  protected:
556 
557  public:
559  inline explicit DynamicVector ( unsigned int size = 0 )
560  : fields_( size )
561  {}
562 
564  inline DynamicVector ( unsigned int size,
565  const FieldType s )
566  : fields_( size )
567  {
568  assign( s );
569  }
570 
572  template< class T >
574  : fields_()
575  {
576  assign( v );
577  }
578 
580  inline DynamicVector ( const ThisType &v )
581  : fields_()
582  {
583  assign( v );
584  }
585 
587  template< class T >
588  inline ThisType &operator= ( const VectorInterface< T > &v )
589  {
590  assign( v );
591  return *this;
592  }
593 
595  inline ThisType &operator= ( const ThisType &v )
596  {
597  assign( v );
598  return *this;
599  }
600 
602  inline ThisType &operator= ( const FieldType s )
603  {
604  assign( s );
605  return *this;
606  }
607 
608  inline const FieldType &operator[] ( unsigned int index ) const
609  {
610  return fields_[ index ];
611  }
612 
613  inline FieldType &operator[] ( unsigned int index )
614  {
615  return fields_[ index ];
616  }
617 
619  template< class T >
620  inline void assign ( const VectorInterface< T > &v )
621  {
622  fields_.assign( v );
623  }
624 
625  inline const FieldType *leakPointer () const
626  {
627  return fields_.leakPointer();
628  }
629 
630  inline FieldType *leakPointer ()
631  {
632  return fields_.leakPointer();
633  }
634 
635  inline void reserve ( unsigned int newSize )
636  {
637  fields_.reserve( newSize );
638  }
639 
640  inline void resize ( unsigned int newSize )
641  {
642  fields_.resize( newSize );
643  }
644 
645  inline void resize ( unsigned int newSize,
646  const FieldType defaultValue )
647  {
648  fields_.resize( newSize, defaultValue );
649  }
650 
651  inline unsigned int size () const
652  {
653  return fields_.size();
654  }
655  };
656 
657 
662  template< class FieldImp, int sz >
664  : public VectorDefault< FieldImp, StaticVector< FieldImp, sz > >
665  {
666  public:
668  typedef FieldImp FieldType;
669 
670  private:
673 
674  public:
675  using BaseType :: assign;
676 
677  protected:
678  FieldType fields_[ sz ];
679 
680  public:
682  inline StaticVector ()
683  {
684  }
685 
687  inline explicit StaticVector ( const FieldType s )
688  {
689  assign( s );
690  }
691 
693  template< class T >
694  inline StaticVector ( const VectorInterface< T > &v )
695  {
696  assign( v );
697  }
698 
700  inline StaticVector ( const ThisType &v )
701  {
702  assign( v );
703  }
704 
706  template< class T >
707  inline ThisType &operator= ( const VectorInterface< T > &v )
708  {
709  assign( v );
710  return *this;
711  }
712 
714  inline ThisType &operator= ( const ThisType &v )
715  {
716  assign( v );
717  return *this;
718  }
719 
721  inline ThisType &operator= ( const FieldType s )
722  {
723  assign( s );
724  return *this;
725  }
726 
727  inline const FieldType &operator[] ( unsigned int index ) const
728  {
729  assert( index < sz );
730  return fields_[ index ];
731  }
732 
733  inline FieldType &operator[] ( unsigned int index )
734  {
735  assert( index < sz );
736  return fields_[ index ];
737  }
738 
739  inline unsigned int size () const
740  {
741  return sz;
742  }
743  };
744 
745 
746  template< class Vector1Type, class Vector2Type >
748  : public VectorDefault< typename ExtractCommonFieldType< Vector1Type, Vector2Type >::FieldType,
749  CombinedVector< Vector1Type, Vector2Type > >
750  {
753 
754  static_assert( SupportsVectorInterface< Vector1Type >::v, "CombinedVector only works on vectors." );
755  static_assert( SupportsVectorInterface< Vector2Type >::v, "CombinedVector only works on vectors." );
756 
757  public:
759 
760  public:
761  CombinedVector( Vector1Type &v1, Vector2Type &v2 )
762  : vector1_( v1 ),
763  vector2_( v2 )
764  {}
765 
766  const FieldType &operator[] ( unsigned int index ) const
767  {
768  const int index2 = index - vector1_.size();
769  if( index2 < 0 )
770  return vector1_[ index ];
771  else
772  return vector2_[ index2 ];
773  }
774 
775  FieldType &operator[] ( unsigned int index )
776  {
777  const int index2 = index - vector1_.size();
778  if( index2 < 0 )
779  return vector1_[ index ];
780  else
781  return vector2_[ index2 ];
782  }
783 
784  unsigned int size() const
785  {
786  return vector1_.size() + vector2_.size();
787  }
788 
789  protected:
790  Vector1Type &vector1_;
791  Vector2Type &vector2_;
792  };
793 
794 
795 
796  // Capabilities
797  // ------------
798 
799  namespace Capabilities
800  {
801 
802  template< class Field, template< class > class ArrayAllocator >
803  struct HasLeakPointer< DynamicVector< Field, ArrayAllocator > >
804  : public MetaBool< true >
805  {};
806 
807  }
808 
809  } // namespace Fem
810 
811 } // namespace Dune
812 
813 #include "vector_inline.hh"
814 
816 
817 #endif // #ifndef DUNE_FEM_VECTOR_HH
implementation of VectorInterface using a C++ array embedded info the class to provide the fields ...
Definition: vector.hh:663
void clear()
initialize the vector to 0
Definition: vector.hh:250
FieldVector< FieldType, sz > FieldVectorType
Definition: vector.hh:304
V::FieldType FieldType
Definition: vector.hh:144
Field FieldType
Definition: vector.hh:156
ExtractCommonFieldType< Vector1Type, Vector2Type >::FieldType FieldType
Definition: vector.hh:754
IteratorType end()
obtain end iterator
Definition: vector.hh:275
VT::IteratorType IteratorType
Definition: vector.hh:31
unsigned int size() const
Definition: array.hh:617
Traits::VectorType VectorType
type of the implementation (Barton-Nackman)
Definition: vector.hh:52
DynamicVector(unsigned int size=0)
Constructor setting up a vector of a specified size.
Definition: vector.hh:559
BaseType::VectorInterfaceType VectorInterfaceType
Definition: vector.hh:182
IteratorType begin()
obtain begin iterator
Definition: vector.hh:263
unsigned int size() const
Returns the vector&#39;s size.
Definition: vector_inline.hh:144
unsigned int size() const
Definition: vector.hh:784
ThisType VectorInterfaceType
type of this interface
Definition: vector.hh:49
unsigned int size() const
Definition: vector.hh:739
void assign(const VectorInterface< T > &v)
copy another vector to this one
Definition: vector.hh:620
FieldVectorAdapter(const FieldType &s)
Definition: vector.hh:319
StaticVector(const FieldType s)
Constructor setting up a vector initialized to a constant value.
Definition: vector.hh:687
FieldImp FieldType
field type of vector
Definition: vector.hh:448
default implementation of VectorInterface
Definition: vector.hh:173
unsigned int size() const
Definition: vector.hh:525
ElementType * leakPointer()
Definition: array.hh:579
DynamicVector(const ThisType &v)
Copy constructor setting up a vector with the data of another one (of the same type) ...
Definition: vector.hh:580
FieldVectorAdapter(const VectorInterface< T > &v)
Definition: vector.hh:328
BaseType::VectorType VectorType
Definition: vector.hh:183
const FieldType * leakPointer() const
Definition: vector.hh:625
Field FieldType
field type of the vector
Definition: vector.hh:550
void reserve(unsigned int newSize)
Definition: array.hh:589
An implementation of VectorInterface wrapping a standard C++ array.
Definition: vector.hh:443
DynamicVector(const VectorInterface< T > &v)
Copy constructor setting up a vector with the data of another one.
Definition: vector.hh:573
StaticVector(const VectorInterface< T > &v)
Copy constructor setting up a vector with the data of another one.
Definition: vector.hh:694
const unsigned int size_
Definition: vector.hh:458
DynamicVector(unsigned int size, const FieldType s)
Constructor setting up a vector iniitialized with a constant value.
Definition: vector.hh:564
ThisType & addScaled(const FieldType &s, const ThisType &other)
Definition: vector.hh:407
Definition: vector.hh:154
ArrayDefaultIterator< const FieldType, const VectorType > ConstIteratorType
Definition: vector.hh:160
ArrayDefaultIterator< FieldType, VectorType > IteratorType
Definition: vector.hh:159
VectorInterface< typename Vector::Traits > VectorInterfaceType
Definition: vector.hh:136
BaseType::ConstIteratorType ConstIteratorType
Definition: vector.hh:185
Definition: vector.hh:747
FieldType * leakPointer()
Definition: vector.hh:630
FieldType *const fields_
Definition: vector.hh:459
CombinedVector(Vector1Type &v1, Vector2Type &v2)
Definition: vector.hh:761
void assign(const ThisType &other)
Definition: vector.hh:413
void assign(const FieldType s)
Initialize all fields of this vector with a scalar.
Definition: vector.hh:242
void assign(const FieldType &s)
Definition: vector.hh:418
void resize(unsigned int newSize, const FieldType defaultValue)
Definition: vector.hh:645
unsigned int size() const
Definition: vector.hh:651
VectorType & addScaled(const FieldType s, const VectorInterface< T > &v)
Add a multiple of another vector to this one.
Definition: vector.hh:222
FieldType value_type
Definition: vector.hh:56
Definition: coordinate.hh:4
StaticVector(const ThisType &v)
Copy constructor setting up a vector with the data of another one.
Definition: vector.hh:700
FieldVectorAdapter(const FieldVectorType &v)
Definition: vector.hh:323
Definition: vector.hh:134
Definition: vector.hh:142
VT::VectorType ArrayType
Definition: vector.hh:27
void assign(const VectorInterface< T > &v)
copy another vector to this one
Definition: vector.hh:233
An abstract vector interface.
Definition: vector.hh:37
StaticVector()
Constructor setting up an uninitialized vector.
Definition: vector.hh:682
static const ThisType & adapt(const FieldVectorType &v)
Definition: vector.hh:428
unsigned int size() const
Definition: vector.hh:423
void resize(unsigned int newSize)
Definition: vector.hh:640
ConstIteratorType begin() const
obtain begin iterator
Definition: vector.hh:257
Traits::ConstIteratorType ConstIteratorType
type of constant iterator
Definition: vector.hh:59
Vector1Type & vector1_
Definition: vector.hh:790
VT::FieldType ElementType
Definition: vector.hh:28
ArrayWrapperVector(const unsigned int size, FieldType *const fields, const FieldType s)
Constructor setting up the vector and initializing the fields to a constant value.
Definition: vector.hh:471
ConstIteratorType end() const
obtain end iterator
Definition: vector.hh:269
ArrayWrapperVector(const unsigned int size, FieldType *const fields)
Constructor setting up the vector (without initializing the fields)
Definition: vector.hh:463
Vector VectorType
Definition: vector.hh:157
void assign(const ArrayInterface< T > &other)
copy another array to this one
Definition: array.hh:572
FieldImp FieldType
field type of vector
Definition: vector.hh:668
DynamicArray< FieldType, ArrayAllocator > fields_
Definition: vector.hh:555
BaseType::FieldType FieldType
Definition: vector.hh:180
Definition: vector.hh:292
BaseType::IteratorType IteratorType
Definition: vector.hh:186
FieldVectorAdapter(const ThisType &other)
Definition: vector.hh:334
abstract array interface
Definition: array.hh:23
static ThisType & adapt(FieldVectorType &v)
Definition: vector.hh:433
void reserve(unsigned int newSize)
Definition: vector.hh:635
Definition: array.hh:172
void resize(unsigned int newSize)
Definition: array.hh:594
FieldVectorType fieldVector_
Definition: vector.hh:312
Vector2Type & vector2_
Definition: vector.hh:791
VT Traits
Definition: vector.hh:46
VT::ConstIteratorType ConstIteratorType
Definition: vector.hh:30
Traits::IteratorType IteratorType
type of iterator
Definition: vector.hh:62
A vector using a DynamicArray as storage.
Definition: vector.hh:542
ArrayWrapperVector(const unsigned int size, FieldType *const fields, const VectorInterface< T > &v)
Copy constructor setting up a vector with the data of another one.
Definition: vector.hh:482
Definition: arrayallocator.hh:112
Traits::FieldType FieldType
field type for the vector
Definition: vector.hh:55