dune-common 2.1.1
fvector.hh
Go to the documentation of this file.
00001 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002 // vi: set et ts=4 sw=2 sts=2:
00003 // $Id: fvector.hh 6387 2011-03-07 15:32:48Z mblatt $
00004 #ifndef DUNE_FVECTOR_HH
00005 #define DUNE_FVECTOR_HH
00006 
00007 #include<cmath>
00008 #include<cstddef>
00009 #include<cstdlib>
00010 #include<complex>
00011 #include<cstring>
00012 
00013 #include "exceptions.hh"
00014 #include "array.hh"
00015 #include "densevector.hh"
00016 
00017 namespace Dune {
00018 
00028   template< class K, int SIZE > class FieldVector;
00029   template< class K, int SIZE >
00030   struct DenseMatVecTraits< FieldVector<K,SIZE> >
00031   {
00032     typedef FieldVector<K,SIZE> derived_type;
00033     typedef Dune::array<K,SIZE> container_type;
00034     typedef K value_type;
00035     typedef typename container_type::size_type size_type;
00036   };
00037   
00038   template< class K, int SIZE >
00039   struct FieldTraits< FieldVector<K,SIZE> >
00040   {
00041     typedef typename FieldTraits<K>::field_type field_type;
00042     typedef typename FieldTraits<K>::real_type real_type;
00043   };
00044 
00050   template< class K, int SIZE >
00051   class FieldVector :
00052     public DenseVector< FieldVector<K,SIZE> >
00053   {
00054     Dune::array<K,SIZE> _data;
00055     typedef DenseVector< FieldVector<K,SIZE> > Base;
00056   public:
00058         enum {
00060       dimension = SIZE,
00062           size = SIZE
00063         };
00064 
00065     typedef typename Base::size_type size_type;
00066     typedef typename Base::value_type value_type;
00067 
00069         FieldVector() {}
00070 
00072         explicit FieldVector (const K& t)
00073         {
00074       fill(t);
00075         }
00076 
00078         FieldVector (const FieldVector & x) : _data(x._data)
00079         {}
00080 
00082     template<class C>
00083         FieldVector (const DenseVector<C> & x)
00084         {
00085       assert(x.size() == SIZE);
00086       for (size_type i = 0; i<SIZE; i++)
00087         _data[i] = x[i];
00088     }
00089 
00090     using Base::operator=;
00091     
00092     // make this thing a vector
00093     size_type vec_size() const { return SIZE; }
00094     K & vec_access(size_type i) { return _data[i]; }
00095     const K & vec_access(size_type i) const { return _data[i]; }
00096   private:
00097     void fill(const K& t)
00098     {
00099       for (int i=0; i<SIZE; i++) _data[i]=t;
00100     }
00101   };
00102 
00114   template<class K, int SIZE>
00115   inline std::istream &operator>> ( std::istream &in,
00116     FieldVector<K, SIZE> &v )
00117   {
00118     FieldVector<K, SIZE> w;
00119     for( typename FieldVector<K, SIZE>::size_type i = 0; i < SIZE; ++i )
00120       in >> w[ i ];
00121     if(in)
00122       v = w;
00123     return in;
00124   }
00125 
00126 #ifndef DOXYGEN
00127   template< class K >
00128   struct DenseMatVecTraits< FieldVector<K,1> >
00129   {
00130     typedef FieldVector<K,1> derived_type;
00131     typedef K container_type;
00132     typedef K value_type;
00133     typedef size_t size_type;
00134   };
00135 
00138   template<class K>
00139   class FieldVector<K, 1> :
00140     public DenseVector< FieldVector<K,1> >
00141   {
00142     K _data;
00143     typedef DenseVector< FieldVector<K,1> > Base;
00144   public:
00146         enum {
00148       dimension = 1,
00150           size = 1
00151         };
00152 
00153     typedef typename Base::size_type size_type;
00154     
00155         //===== construction
00156 
00158         FieldVector () {}
00159 
00161         FieldVector (const K& k) : _data(k) {}
00162 
00164     template<class C>
00165         FieldVector (const DenseVector<C> & x)
00166         {
00167       assert(x.size() == 1);
00168       _data = x[0];
00169     }
00170 
00172     inline FieldVector& operator= (const K& k)
00173     {
00174       _data = k;
00175       return *this;
00176     }
00177     
00178     //===== forward methods to container
00179     size_type vec_size() const { return 1; }
00180     K & vec_access(size_type i)
00181     {
00182       assert(i == 0);
00183       return _data;
00184     }
00185     const K & vec_access(size_type i) const
00186     {
00187       assert(i == 0);
00188       return _data;
00189     }
00190     
00191         //===== conversion operator
00192 
00194         operator K () { return _data; }
00195 
00197         operator K () const { return _data; }
00198   };
00199 
00200   /* ----- FV / FV ----- */
00201   /* not necessary as these operations are already covered via the cast operator */
00202 
00203   /* ----- FV / scalar ----- */
00204   
00206   template<class K>
00207   inline FieldVector<K,1> operator+ (const FieldVector<K,1>& a, const K b)
00208   {
00209     return a[0]+b;
00210   }
00211   
00213   template<class K>
00214   inline FieldVector<K,1> operator- (const FieldVector<K,1>& a, const K b)
00215   {
00216     return a[0]-b;
00217   }
00218   
00220   template<class K>
00221   inline FieldVector<K,1> operator* (const FieldVector<K,1>& a, const K b)
00222   {
00223     return a[0]*b;
00224   }
00225   
00227   template<class K>
00228   inline FieldVector<K,1> operator/ (const FieldVector<K,1>& a, const K b)
00229   {
00230     return a[0]/b;
00231   }
00232   
00234   template<class K>
00235   inline bool operator> (const FieldVector<K,1>& a, const K b)
00236   {
00237     return a[0]>b;
00238   }
00239 
00241   template<class K>
00242   inline bool operator>= (const FieldVector<K,1>& a, const K b)
00243   {
00244     return a[0]>=b;
00245   }
00246 
00248   template<class K>
00249   inline bool operator< (const FieldVector<K,1>& a, const K b)
00250   {
00251     return a[0]<b;
00252   }
00253 
00255   template<class K>
00256   inline bool operator<= (const FieldVector<K,1>& a, const K b)
00257   {
00258     return a[0]<=b;
00259   }
00260 
00262   template<class K>
00263   inline bool operator== (const FieldVector<K,1>& a, const K b)
00264   {
00265     return a[0]==b;
00266   }
00267 
00269   template<class K>
00270   inline bool operator!= (const FieldVector<K,1>& a, const K b)
00271   {
00272     return a[0]!=b;
00273   }
00274 
00275   /* ----- scalar / FV ------ */
00276   
00278   template<class K>
00279   inline FieldVector<K,1> operator+ (const K a, const FieldVector<K,1>& b)
00280   {
00281     return a+b[0];
00282   }
00283   
00285   template<class K>
00286   inline FieldVector<K,1> operator- (const K a, const FieldVector<K,1>& b)
00287   {
00288     return a-b[0];
00289   }
00290 
00292   template<class K>
00293   inline FieldVector<K,1> operator* (const K a, const FieldVector<K,1>& b)
00294   {
00295     return a*b[0];
00296   }
00297   
00299   template<class K>
00300   inline FieldVector<K,1> operator/ (const K a, const FieldVector<K,1>& b)
00301   {
00302     return a/b[0];
00303   }
00304 
00306   template<class K>
00307   inline bool operator> (const K a, const FieldVector<K,1>& b)
00308   {
00309     return a>b[0];
00310   }
00311 
00313   template<class K>
00314   inline bool operator>= (const K a, const FieldVector<K,1>& b)
00315   {
00316     return a>=b[0];
00317   }
00318 
00320   template<class K>
00321   inline bool operator< (const K a, const FieldVector<K,1>& b)
00322   {
00323     return a<b[0];
00324   }
00325 
00327   template<class K>
00328   inline bool operator<= (const K a, const FieldVector<K,1>& b)
00329   {
00330     return a<=b[0];
00331   }
00332 
00334   template<class K>
00335   inline bool operator== (const K a, const FieldVector<K,1>& b)
00336   {
00337     return a==b[0];
00338   }
00339 
00341   template<class K>
00342   inline bool operator!= (const K a, const FieldVector<K,1>& b)
00343   {
00344     return a!=b[0];
00345   }
00346 #endif
00347 
00350 } // end namespace
00351 
00352 #endif