nvector.h

Go to the documentation of this file.
00001 #ifndef __nvector_h
00002 #define __nvector_h
00003 
00004 #include  <vector>
00005 #include  <iterator>
00006 #include  <iostream>
00007 #include  <iterator>
00008 #include  <climits>
00009 #include  <numeric>
00010 #include  <cassert>
00011 
00012 #include  "gascoignemath.h"
00013 
00014 /*----------------------------------------------*/
00015 
00016 namespace Gascoigne
00017 {
00018 template<class T>
00019 class nvector : public std::vector<T>
00020 {
00021 private:
00022 
00023 public:
00024 
00025   typedef typename std::vector<T>::iterator       iterator;
00026   typedef typename std::vector<T>::const_iterator const_iterator;
00027 
00028   ~nvector()  {}
00029   nvector()                    : std::vector<T>()    {}
00030   nvector(size_t n)               : std::vector<T>(n)   {}
00031   nvector(size_t n, const T& d)   : std::vector<T>(n,d) {}
00032   nvector(const std::vector<T>& v) : std::vector<T>(v)   {}
00033 
00034   friend std::ostream& operator<<(std::ostream &s, const nvector<T>& A)
00035   {
00036     std::ostream_iterator<T>  os(s, " ");
00037     copy(A.begin(),A.end(),os);
00038     return s;
00039   }
00040   friend std::istream& operator>>(std::istream &s, nvector<T>& A)
00041     {
00042       iterator p = A.begin();
00043       while(p!=A.end())
00044         s >> *p++;
00045       return s;
00046     }
00047 
00048   void write_data(std::ostream& s) const
00049   {
00050     s << std::vector<T>::size() << std::endl << *this;
00051   }
00052 
00053   void read_data(std::istream& s)
00054   {
00055     size_t n;
00056     s >> n;
00057     reservesize(n);
00058     s >> *this;
00059   }
00060 
00061   const T& secure_access(int i) const {
00062     assert(i<std::vector<T>::size());
00063     assert(i>=0);
00064     return std::vector<T>::operator[](i);
00065   }
00066   T& secure_access(int i) {
00067     assert(i<std::vector<T>::size());
00068     assert(i>=0);
00069     return std::vector<T>::operator[](i);
00070   }
00071 
00072 
00073   T operator*(const nvector& v) const;
00074   nvector<T>&   operator=(const T&);
00075   nvector<T>&   operator=(const std::vector<T>&);
00076   nvector<T>&   operator*=(const T& d);
00077   nvector<T>&   operator+=(const T& d) { add(d); return *this; }
00078   nvector<T>&   operator+=(const nvector& v) { add(1,v); return *this; }
00079   nvector<T>&   operator-=(const nvector& v) { add(-1,v); return *this; }
00080 
00081   void   zero ();
00082   void   equ  (const T&);
00083   void   equ  (const T&, const nvector&);
00084   void   equ  (const T&, const nvector&, const T&, const nvector&);
00085   void   equ  (const T&, const nvector&, const T&, const nvector&, 
00086                const T&, const nvector&);
00087   void   sequ (const T&, const T&, const nvector&);
00088   void   add  (const T&);
00089   void   add  (const T&, const nvector&);
00090   void   add  (const T&, const nvector&, const T&, const nvector&);
00091   void   add  (const T&, const nvector&, const T&, const nvector&, 
00092                const T&, const nvector&);
00093   void   sadd (const T&, const T&, const nvector&);
00094   double max  ()     const;
00095   double min  ()     const;
00096   T      sum()     const;
00097   double      norm()     const;
00098   double      norm_l1()     const;
00099   double      norm_l8()     const;
00100 
00101   void ReInit(size_t n)
00102     {
00103       std::vector<T>::reserve(n); std::vector<T>::resize(n);
00104     }
00105   void memory(size_t n)
00106     {
00107       ReInit(n);
00108     }
00109   void reservesize(size_t n)
00110     {
00111       ReInit(n);
00112     }
00113   void reservesize(size_t n, const T& s)
00114     {
00115       std::vector<T>::reserve(n); std::vector<T>::resize(n,s);
00116     }
00117   void reservesize(const nvector<T>& v) {reservesize(v.size());}
00118 
00119   void BinWrite(std::ostream& out) const;
00120   void BinRead (std::istream& in);
00121   int  find(const T& x) const;
00122 };
00123 
00124 
00125 /**************************************************/
00126 
00127 template<class T>
00128 inline double nvector<T>::norm() const
00129 {
00130   const_iterator first  = std::vector<T>::begin();
00131   const_iterator last   = std::vector<T>::end();
00132 
00133   T n(0);
00134   while( first != last)
00135     {
00136       n += ((*first)) * ((*first));
00137       first++;
00138     }
00139   return sqrt(static_cast<double>(n));
00140 }
00141 
00142 /**************************************************/
00143 
00144 template<class T>
00145 inline T nvector<T>::sum() const
00146 {
00147   const_iterator first  = std::vector<T>::begin();
00148   const_iterator last   = std::vector<T>::end();
00149 
00150   T n(0);
00151   while( first != last)
00152     {
00153       n += (*first++);
00154     }
00155   return n;
00156 }
00157 
00158 /**************************************************/
00159 
00160 template<class T>
00161 inline double nvector<T>::norm_l1() const
00162 {
00163   const_iterator first  = std::vector<T>::begin();
00164   const_iterator last   = std::vector<T>::end();
00165 
00166   double n(0);
00167   while( first != last)
00168     {
00169       n += fabs((*first++));
00170     }
00171   return n;
00172 }
00173 
00174 /**************************************************/
00175 
00176 template<class T>
00177 inline double nvector<T>::norm_l8() const
00178 {
00179   const_iterator first  = std::vector<T>::begin();
00180   const_iterator last   = std::vector<T>::end();
00181 
00182   double n(0);
00183   while( first != last)
00184     {
00185       n = Gascoigne::max(n,fabs(*first));
00186       first++;
00187     }
00188   return n;
00189 }
00190 
00191 /**************************************************/
00192 
00193 template<class T>
00194 inline nvector<T>& nvector<T>::operator=(const T& d)
00195 {
00196   iterator  first  = std::vector<T>::begin();
00197   const_iterator last   = std::vector<T>::end();
00198 
00199   while( first != last)
00200     {
00201       *first++ = d;
00202     }
00203   return *this;
00204 }
00205 
00206 /**************************************************/
00207 
00208 template<class T>
00209 inline nvector<T>& nvector<T>::operator=(const std::vector<T>& v)
00210 {
00211   assert(std::vector<T>::size()==v.std::vector<T>::size());
00212   iterator  first  = std::vector<T>::begin();
00213   const_iterator last   = std::vector<T>::end();
00214   const_iterator vfirst = v.std::vector<T>::begin();
00215 
00216   while( first != last)
00217     {
00218       *first++ = *vfirst++;
00219     }
00220   return *this;
00221 }
00222 
00223 /**************************************************/
00224 
00225 template<class T>
00226 inline nvector<T>& nvector<T>::operator*=(const T& d)
00227 {
00228   iterator  first  = std::vector<T>::begin();
00229   const_iterator last   = std::vector<T>::end();
00230 
00231   while(first != last)
00232     {
00233       (*first++) *= d;
00234    }
00235   return *this;
00236 }
00237 
00238 /**************************************************/
00239 
00240 template<class T>
00241 inline void nvector<T>::zero()
00242 {
00243   iterator  first  = std::vector<T>::begin();
00244   const_iterator last   = std::vector<T>::end();
00245 
00246   while( first != last)
00247     {
00248       *first++ = 0;
00249     }
00250 }
00251 
00252 /**************************************************/
00253 
00254 template<class T>
00255 inline double nvector<T>::max() const
00256 {
00257   double d = 0;//std::numeric_limits<double>::min();
00258 /*   double d = std::numeric_limits<double>::min(); */
00259   const_iterator first  = std::vector<T>::begin();
00260   const_iterator last   = std::vector<T>::end();
00261 
00262   while( first != last)
00263     {
00264       d = Gascoigne::max( d, fabs((*first)));
00265       first++;
00266     }
00267   return d;
00268 }
00269 
00270 /**************************************************/
00271 
00272 template<class T>
00273 inline double nvector<T>::min() const
00274 {
00275   double d = 100000.;//std::numeric_limits<double>::max();
00276 /*   double d = std::numeric_limits<double>::max(); */
00277   const_iterator first  = std::vector<T>::begin();
00278   const_iterator last   = std::vector<T>::end();
00279 
00280   while( first != last)
00281     {
00282       d = Gascoigne::min( d, fabs((*first)));
00283       first++;
00284     }
00285   return d;
00286 }
00287 
00288 /**************************************************/
00289 
00290 template<class T>
00291 inline T nvector<T>::operator* (const nvector<T>& v) const
00292 {
00293   const_iterator first  = std::vector<T>::begin();
00294   const_iterator last   = std::vector<T>::end();
00295   const_iterator first2 = v.begin();
00296 
00297   T d(0);
00298   while(first != last)
00299     {
00300       d += (*first++) * (*first2++);
00301     }
00302   return d;
00303 
00304   //return inner_product(first,last,first2,0.);
00305 }
00306 
00307 /**************************************************/
00308 
00309 template<class T>
00310 inline void nvector<T>::equ (const T& d)
00311 {
00312   iterator  first  = std::vector<T>::begin();
00313   const_iterator last   = std::vector<T>::end();
00314 
00315   while(first != last)
00316     {
00317       (*first++) = d;
00318     }
00319 }
00320 
00321 /**************************************************/
00322 
00323 template<class T>
00324 inline void nvector<T>::equ (const T& d, const nvector<T>& v)
00325 {
00326   iterator  first  = std::vector<T>::begin();
00327   const_iterator last   = std::vector<T>::end();
00328   const_iterator first2 = v.begin();
00329 
00330   while(first != last)
00331     {
00332       (*first++) = d*(*first2++);
00333     }
00334 }
00335 
00336 /**************************************************/
00337 
00338 template<class T>
00339 inline void nvector<T>::equ (const T& d, const nvector<T>& v, 
00340                              const T& e, const nvector<T>& w)
00341 {
00342   iterator  first  = std::vector<T>::begin();
00343   const_iterator last   = std::vector<T>::end();
00344   const_iterator first2 = v.begin();
00345   const_iterator first3 = w.begin();
00346 
00347   while(first != last)
00348     {
00349       (*first++) = d*(*first2++) + e*(*first3++);
00350     }
00351 }
00352 
00353 /**************************************************/
00354 
00355 template<class T>
00356 inline void nvector<T>::equ (const T& d, const nvector<T>& v, 
00357                              const T& e, const nvector<T>& w,
00358                              const T& f, const nvector<T>& x)
00359 {
00360   iterator  first  = std::vector<T>::begin();
00361   const_iterator last   = std::vector<T>::end();
00362   const_iterator first2 = v.begin();
00363   const_iterator first3 = w.begin();
00364   const_iterator first4 = x.begin();
00365 
00366   while(first != last)
00367     {
00368       (*first++) = d*(*first2++) + e*(*first3++) + f*(*first4++);
00369     }
00370 }
00371 
00372 /**************************************************/
00373 
00374 template<class T>
00375 inline void nvector<T>::sequ (const T& s, const T& d, const nvector<T>& v)
00376 {
00377   iterator  first  = std::vector<T>::begin();
00378   const_iterator last   = std::vector<T>::end();
00379   const_iterator first2 = v.begin();
00380 
00381   while(first != last)
00382     {
00383       (*first) = s*(*first) + d*(*first2++);
00384       first++;
00385     }
00386 }
00387 
00388 /**************************************************/
00389 
00390 template<class T>
00391 inline void nvector<T>::add (const T& d)
00392 {
00393   iterator  first  = std::vector<T>::begin();
00394   const_iterator last   = std::vector<T>::end();
00395 
00396   while(first != last)
00397     {
00398       (*first++) += d;
00399    }
00400 }
00401 
00402 /**************************************************/
00403 
00404 template<class T>
00405 inline void nvector<T>::add (const T& d, const nvector<T>& v)
00406 {
00407   iterator  first  = std::vector<T>::begin();
00408   const_iterator last   = std::vector<T>::end();
00409   const_iterator first2 = v.begin();
00410 
00411   while(first != last)
00412     {
00413       (*first++) += d*(*first2++);
00414    }
00415 }
00416 
00417 /**************************************************/
00418 
00419 template<class T>
00420 inline void nvector<T>::add (const T& d, const nvector<T>& v,
00421                              const T& e, const nvector<T>& w)
00422 {
00423   iterator  first  = std::vector<T>::begin();
00424   const_iterator last   = std::vector<T>::end();
00425   const_iterator first2 = v.begin();
00426   const_iterator first3 = w.begin();
00427 
00428   while(first != last)
00429     {
00430       (*first++) += d*(*first2++) + e*(*first3++);
00431    }
00432 }
00433 
00434 /**************************************************/
00435 
00436 template<class T>
00437 inline void nvector<T>::add (const T& d, const nvector<T>& v,
00438                              const T& e, const nvector<T>& w,
00439                              const T& f, const nvector<T>& x)
00440 {
00441   iterator  first  = std::vector<T>::begin();
00442   const_iterator last   = std::vector<T>::end();
00443   const_iterator first2 = v.begin();
00444   const_iterator first3 = w.begin();
00445   const_iterator first4 = x.begin();
00446 
00447   while(first != last)
00448     {
00449       (*first++) += d*(*first2++) + e*(*first3++) + f*(*first4++);
00450    }
00451 }
00452 
00453 /**************************************************/
00454 
00455 template<class T>
00456 inline void nvector<T>::sadd (const T& a, const T& d, const nvector<T>& v)
00457 {
00458   iterator  first  = std::vector<T>::begin();
00459   const_iterator last   = std::vector<T>::end();
00460   const_iterator first2 = v.begin();
00461 
00462   while(first != last)
00463     {
00464       (*first) = a*(*first) + d*(*first2++);
00465       first++;
00466    }
00467 }
00468 
00469 /**************************************************/
00470 
00471 template<class T>
00472 inline void nvector<T>::BinWrite(std::ostream& out) const
00473 {
00474   out << std::vector<T>::size() << std::endl << "[";
00475   
00476   int sizeT = sizeof(T);
00477   for(int i=0; i<std::vector<T>::size(); i++)
00478     {
00479       out.write (reinterpret_cast<const char*>(&(std::vector<T>::operator[](i))),sizeT);
00480     }
00481   out << "]"; 
00482 }
00483 
00484 /**********************************************************/
00485 
00486 template<class T>
00487 inline void nvector<T>::BinRead(std::istream& in)
00488 {
00489   char c;
00490   int  n;
00491   in >> n >> c;
00492   std::vector<T>::resize(n);
00493   
00494   int sizeT = sizeof(T);
00495   for(int i=0; i<std::vector<T>::size(); i++)
00496     {
00497       in.read(reinterpret_cast<char*>(&(nvector<T>::operator[](i))),sizeT);
00498     }
00499   in >> c;
00500 }
00501 
00502 /**********************************************************/
00503 
00504 template<class T>
00505 inline int nvector<T>::find(const T& x) const
00506 {
00507   for (int i=0; i<std::vector<T>::size(); i++)
00508     {
00509       if ((*this)[i]==x) return i;
00510     }
00511   return -1;
00512 }
00513 
00514 /**********************************************************/
00515 }
00516 
00517 #endif
00518 

Generated on Thu Sep 14 10:34:35 2006 for Gascoigne by  doxygen 1.4.7