00001
00002
00003
00004
00005
00006
00007
00008 #ifndef _VECTOR_HH
00009 #define _VECTOR_HH
00010
00011 #include <vector>
00012 #include <cmath>
00013 #include <assert.h>
00014 #include <iostream>
00015 #include <fstream>
00016 #include <sstream>
00017 #include <iomanip>
00018 #include <cstdlib>
00019
00020
00021 #define DOXYGEN
00022
00023 namespace hdnum {
00024
00028 template<typename REAL>
00029 class Vector : public std::vector<REAL>
00030 {
00031 public:
00033 typedef std::size_t size_type;
00034
00035 private:
00036 static bool bScientific;
00037 static std::size_t nIndexWidth;
00038 static std::size_t nValueWidth;
00039 static std::size_t nValuePrecision;
00040
00041 public:
00042
00043
00044 Vector() : std::vector<REAL>()
00045 {
00046 }
00047
00048
00049 Vector( const size_t size,
00050 const REAL defaultvalue_ = 0
00051 )
00052 : std::vector<REAL>( size, defaultvalue_ )
00053 {
00054 }
00055
00056
00057
00058
00078 Vector& operator=( const REAL & value )
00079 {
00080 const size_t s = this->size();
00081 Vector & self = *this;
00082 for(size_t i=0; i<s; ++i)
00083 self[i] = value;
00084 return *this;
00085 }
00086
00087
00088
00120 #ifdef DOXYGEN
00121 Vector& operator=( const Vector& y )
00122 {
00123
00124 }
00125 #endif
00126
00127
00128
00129
00130 Vector& operator*=( const REAL & value )
00131 {
00132 Vector &self = *this;
00133 for (size_t i = 0; i < this->size(); ++i)
00134 self[i] *= value;
00135 return *this;
00136 }
00137
00138
00139
00140 Vector& operator/=( const REAL & value )
00141 {
00142 Vector &self = *this;
00143 for (size_t i = 0; i < this->size(); ++i)
00144 self[i] /= value;
00145 return *this;
00146 }
00147
00148
00149
00150 Vector& operator+=( const Vector & y )
00151 {
00152 assert( this->size() == y.size());
00153 Vector &self = *this;
00154 for (size_t i = 0; i < this->size(); ++i)
00155 self[i] += y[i];
00156 return *this;
00157 }
00158
00159
00160
00161 Vector& operator-=( const Vector & y )
00162 {
00163 assert( this->size() == y.size());
00164 Vector &self = *this;
00165 for (size_t i = 0; i < this->size(); ++i)
00166 self[i] -= y[i];
00167 return *this;
00168 }
00169
00170
00171
00172
00173
00174 Vector & update(const REAL alpha, const Vector & y)
00175 {
00176 assert( this->size() == y.size());
00177 Vector &self = *this;
00178 for (size_t i = 0; i < this->size(); ++i)
00179 self[i] += alpha * y[i];
00180 return *this;
00181 }
00182
00183
00215 REAL operator*(Vector & x) const
00216 {
00217 assert( x.size() == this->size() );
00218 REAL sum( 0 );
00219 const Vector & self = *this;
00220 for( size_t i = 0; i < this->size(); ++i )
00221 sum += self[i] * x[i];
00222 return sum;
00223 }
00224
00225
00226
00227
00260 Vector operator+(Vector & x) const
00261 {
00262 assert( x.size() == this->size() );
00263 Vector sum( *this );
00264 sum += x;
00265 return sum;
00266 }
00267
00268
00269
00302 Vector operator-(Vector & x) const
00303 {
00304 assert( x.size() == this->size() );
00305 Vector sum( *this );
00306 sum -= x;
00307 return sum;
00308 }
00309
00310
00311
00313 REAL two_norm_2() const
00314 {
00315 REAL sum( 0 );
00316 const Vector & self = *this;
00317 for (size_t i = 0; i < (size_t) this->size(); ++i)
00318 sum += self[i] * self[i];
00319 return sum;
00320 }
00321
00346 REAL two_norm() const
00347 {
00348 return sqrt(two_norm_2());
00349 }
00350
00352 bool scientific() const
00353 {
00354 return bScientific;
00355 }
00356
00384 void scientific(bool b) const
00385 {
00386 bScientific=b;
00387 }
00388
00389 std::size_t width() const
00390 {
00391 return nValueWidth;
00392 }
00393
00394 void width (std::size_t i) const
00395 {
00396 nValueWidth=i;
00397 }
00398
00399 std::size_t iwidth() const
00400 {
00401 return nIndexWidth;
00402 }
00403
00404 void iwidth (std::size_t i) const
00405 {
00406 nIndexWidth=i;
00407 }
00408
00409 std::size_t precision() const
00410 {
00411 return nValuePrecision;
00412 }
00413
00414 void precision (std::size_t i) const
00415 {
00416 nValuePrecision=i;
00417 }
00418
00419 };
00420
00421
00422
00423 template<typename REAL>
00424 bool Vector<REAL>::bScientific = true;
00425
00426 template<typename REAL>
00427 std::size_t Vector<REAL>::nIndexWidth = 2;
00428
00429 template<typename REAL>
00430 std::size_t Vector<REAL>::nValueWidth = 15;
00431
00432 template<typename REAL>
00433 std::size_t Vector<REAL>::nValuePrecision = 7;
00434
00435
00457 template <typename REAL>
00458 inline std::ostream & operator <<(std::ostream & os, const Vector<REAL> & x)
00459 {
00460 os << std::endl;
00461
00462 for (size_t r = 0; r < x.size(); ++r)
00463 {
00464 if( x.scientific() )
00465 {
00466 os << "["
00467 << std::setw(x.iwidth())
00468 << r
00469 << "]"
00470 << std::scientific
00471 << std::showpoint
00472 << std::setw( x.width() )
00473 << std::setprecision( x.precision() )
00474 << x[r]
00475 << std::endl;
00476 }
00477 else
00478 {
00479 os << "["
00480 << std::setw(x.iwidth())
00481 << r
00482 << "]"
00483 << std::fixed
00484 << std::showpoint
00485 << std::setw( x.width() )
00486 << std::setprecision( x.precision() )
00487 << x[r]
00488 << std::endl;
00489 }
00490 }
00491 return os;
00492 }
00493
00494
00495
00518 template<typename REAL>
00519 inline void gnuplot(
00520 const std::string& fname,
00521 const Vector<REAL> x
00522 )
00523 {
00524 std::fstream f(fname.c_str(),std::ios::out);
00525 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
00526 {
00527 if( x.scientific() )
00528 {
00529 f << std::setw(x.width())
00530 << i
00531 << std::scientific
00532 << std::showpoint
00533 << std::setw( x.width() )
00534 << std::setprecision( x.precision() )
00535 << x[i]
00536 << std::endl;
00537 }
00538 else
00539 {
00540 f << std::setw(x.width())
00541 << i
00542 << std::fixed
00543 << std::showpoint
00544 << std::setw( x.width() )
00545 << std::setprecision( x.precision() )
00546 << x[i]
00547 << std::endl;
00548 }
00549 }
00550 f.close();
00551 }
00552
00554 template<typename REAL>
00555 inline void gnuplot(
00556 const std::string& fname,
00557 const Vector<REAL> x,
00558 const Vector<REAL> y
00559 )
00560 {
00561 std::fstream f(fname.c_str(),std::ios::out);
00562 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
00563 {
00564 if( x.scientific() )
00565 {
00566 f << std::setw(x.width())
00567 << i
00568 << std::scientific
00569 << std::showpoint
00570 << std::setw( x.width() )
00571 << std::setprecision( x.precision() )
00572 << x[i]
00573 << " "
00574 << std::setw( x.width() )
00575 << std::setprecision( x.precision() )
00576 << y[i]
00577 << std::endl;
00578 }
00579 else
00580 {
00581 f << std::setw(x.width())
00582 << i
00583 << std::fixed
00584 << std::showpoint
00585 << std::setw( x.width() )
00586 << std::setprecision( x.precision() )
00587 << x[i]
00588 << " "
00589 << std::setw( x.width() )
00590 << std::setprecision( x.precision() )
00591 << y[i]
00592 << std::endl;
00593 }
00594 }
00595
00596 f.close();
00597 }
00598
00599
00600
00629 template<typename REAL>
00630 inline void readVectorFromFile (const std::string& filename, Vector<REAL> &vector)
00631 {
00632 std::string buffer;
00633 std::ifstream fin( filename.c_str() );
00634 if( fin.is_open() ){
00635 while( fin ){
00636 std::string sub;
00637 fin >> sub;
00638
00639 if( sub.length()>0 ){
00640 REAL a = atof(sub.c_str());
00641
00642 vector.push_back(a);
00643 }
00644 }
00645 fin.close();
00646 }
00647 else{
00648 HDNUM_ERROR("Could not open file!");
00649 }
00650 }
00651
00652
00654 template<class REAL>
00655 inline void zero (Vector<REAL>& x)
00656 {
00657 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
00658 x[i] = REAL(0);
00659 }
00660
00662 template<class REAL>
00663 inline void fill (Vector<REAL>& x, const REAL& t)
00664 {
00665 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
00666 x[i] = t;
00667 }
00668
00691 template<class REAL>
00692 inline void fill (Vector<REAL>& x, const REAL& t, const REAL& dt)
00693 {
00694 REAL myt(t);
00695 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
00696 {
00697 x[i] = myt;
00698 myt += dt;
00699 }
00700 }
00701
00702
00725 template<class REAL>
00726 inline void unitvector (Vector<REAL> & x, std::size_t j)
00727 {
00728 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
00729 if (i==j)
00730 x[i] = REAL(1);
00731 else
00732 x[i] = REAL(0);
00733 }
00734
00735
00736 }
00737
00738 #endif
00739