Heidelberg Educational Numerics Library Version 0.24 (from 9 September 2011)
|
00001 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- 00002 /* 00003 * File: vector.hh 00004 * Author: ngo 00005 * 00006 * Created on April 14th, 2011 00007 */ 00008 00009 #ifndef _VECTOR_HH 00010 #define _VECTOR_HH 00011 00012 #include <vector> 00013 #include <cmath> 00014 #include <assert.h> 00015 #include <iostream> 00016 #include <fstream> 00017 #include <sstream> 00018 #include <iomanip> 00019 #include <cstdlib> 00020 00021 00022 namespace hdnum { 00023 00027 template<typename REAL> 00028 class Vector : public std::vector<REAL> // inherit from the STL vector 00029 { 00030 public: 00032 typedef std::size_t size_type; 00033 00034 private: 00035 static bool bScientific; 00036 static std::size_t nIndexWidth; 00037 static std::size_t nValueWidth; 00038 static std::size_t nValuePrecision; 00039 00040 public: 00041 00042 // default constructor, also inherited from the STL vector default constructor 00043 Vector() : std::vector<REAL>() 00044 { 00045 } 00046 00047 // another constructor, with arguments, setting the default value for all entries of the vector of given size 00048 Vector( const size_t size, // user must specify the size 00049 const REAL defaultvalue_ = 0 // if not specified, the value 0 will take effect 00050 ) 00051 : std::vector<REAL>( size, defaultvalue_ ) 00052 { 00053 } 00054 00055 00056 // Methods: 00057 00077 Vector& operator=( const REAL value ) 00078 { 00079 const size_t s = this->size(); 00080 Vector & self = *this; 00081 for(size_t i=0; i<s; ++i) 00082 self[i] = value; 00083 return *this; 00084 } 00085 00095 Vector sub (size_type i, size_type m) 00096 { 00097 Vector v(m); 00098 Vector &self = *this; 00099 size_type k=0; 00100 for (size_type j=i; j<i+m; j++){ 00101 v[k]=self[j]; 00102 k++; 00103 } 00104 return v; 00105 } 00106 00107 00108 00140 #ifdef DOXYGEN 00141 Vector& operator=( const Vector& y ) 00142 { 00143 // It is already implemented in the STL vector class itself! 00144 } 00145 #endif 00146 00147 00148 00149 // Multiplication by a scalar value: x *= value 00150 Vector& operator*=( const REAL value ) 00151 { 00152 Vector &self = *this; 00153 for (size_t i = 0; i < this->size(); ++i) 00154 self[i] *= value; 00155 return *this; 00156 } 00157 00158 00159 // Division by a scalar value: x /= value 00160 Vector& operator/=( const REAL value ) 00161 { 00162 Vector &self = *this; 00163 for (size_t i = 0; i < this->size(); ++i) 00164 self[i] /= value; 00165 return *this; 00166 } 00167 00168 00169 // Add another vector: x += y 00170 Vector& operator+=( const Vector & y ) 00171 { 00172 assert( this->size() == y.size()); 00173 Vector &self = *this; 00174 for (size_t i = 0; i < this->size(); ++i) 00175 self[i] += y[i]; 00176 return *this; 00177 } 00178 00179 00180 // Subtract another vector: x -= y 00181 Vector& operator-=( const Vector & y ) 00182 { 00183 assert( this->size() == y.size()); 00184 Vector &self = *this; 00185 for (size_t i = 0; i < this->size(); ++i) 00186 self[i] -= y[i]; 00187 return *this; 00188 } 00189 00190 00191 // Update vector by addition of a scaled vector: 00192 // x += alpha * y 00193 // 00194 Vector & update(const REAL alpha, const Vector & y) 00195 { 00196 assert( this->size() == y.size()); 00197 Vector &self = *this; 00198 for (size_t i = 0; i < this->size(); ++i) 00199 self[i] += alpha * y[i]; 00200 return *this; 00201 } 00202 00203 00235 REAL operator*(Vector & x) const 00236 { 00237 assert( x.size() == this->size() ); // checks if the dimensions of the two vectors are equal 00238 REAL sum( 0 ); 00239 const Vector & self = *this; 00240 for( size_t i = 0; i < this->size(); ++i ) 00241 sum += self[i] * x[i]; 00242 return sum; 00243 } 00244 00245 00246 00247 00280 Vector operator+(Vector & x) const 00281 { 00282 assert( x.size() == this->size() ); // checks if the dimensions of the two vectors are equal 00283 Vector sum( *this ); 00284 sum += x; 00285 return sum; 00286 } 00287 00288 00289 00322 Vector operator-(Vector & x) const 00323 { 00324 assert( x.size() == this->size() ); // checks if the dimensions of the two vectors are equal 00325 Vector sum( *this ); 00326 sum -= x; 00327 return sum; 00328 } 00329 00330 00331 00333 REAL two_norm_2() const 00334 { 00335 REAL sum( 0 ); 00336 const Vector & self = *this; 00337 for (size_t i = 0; i < (size_t) this->size(); ++i) 00338 sum += self[i] * self[i]; 00339 return sum; 00340 } 00341 00366 REAL two_norm() const 00367 { 00368 return sqrt(two_norm_2()); 00369 } 00370 00372 bool scientific() const 00373 { 00374 return bScientific; 00375 } 00376 00404 void scientific(bool b) const 00405 { 00406 bScientific=b; 00407 } 00408 00409 std::size_t width() const 00410 { 00411 return nValueWidth; 00412 } 00413 00414 void width (std::size_t i) const 00415 { 00416 nValueWidth=i; 00417 } 00418 00419 std::size_t iwidth() const 00420 { 00421 return nIndexWidth; 00422 } 00423 00424 void iwidth (std::size_t i) const 00425 { 00426 nIndexWidth=i; 00427 } 00428 00429 std::size_t precision() const 00430 { 00431 return nValuePrecision; 00432 } 00433 00434 void precision (std::size_t i) const 00435 { 00436 nValuePrecision=i; 00437 } 00438 00439 }; 00440 00441 00442 00443 template<typename REAL> 00444 bool Vector<REAL>::bScientific = true; 00445 00446 template<typename REAL> 00447 std::size_t Vector<REAL>::nIndexWidth = 2; 00448 00449 template<typename REAL> 00450 std::size_t Vector<REAL>::nValueWidth = 15; 00451 00452 template<typename REAL> 00453 std::size_t Vector<REAL>::nValuePrecision = 7; 00454 00455 00477 template <typename REAL> 00478 inline std::ostream & operator <<(std::ostream & os, const Vector<REAL> & x) 00479 { 00480 os << std::endl; 00481 00482 for (size_t r = 0; r < x.size(); ++r) 00483 { 00484 if( x.scientific() ) 00485 { 00486 os << "[" 00487 << std::setw(x.iwidth()) 00488 << r 00489 << "]" 00490 << std::scientific 00491 << std::showpoint 00492 << std::setw( x.width() ) 00493 << std::setprecision( x.precision() ) 00494 << x[r] 00495 << std::endl; 00496 } 00497 else 00498 { 00499 os << "[" 00500 << std::setw(x.iwidth()) 00501 << r 00502 << "]" 00503 << std::fixed 00504 << std::showpoint 00505 << std::setw( x.width() ) 00506 << std::setprecision( x.precision() ) 00507 << x[r] 00508 << std::endl; 00509 } 00510 } 00511 return os; 00512 } 00513 00514 00515 00538 template<typename REAL> 00539 inline void gnuplot( 00540 const std::string& fname, 00541 const Vector<REAL> x 00542 ) 00543 { 00544 std::fstream f(fname.c_str(),std::ios::out); 00545 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++) 00546 { 00547 if( x.scientific() ) 00548 { 00549 f << std::setw(x.width()) 00550 << i 00551 << std::scientific 00552 << std::showpoint 00553 << std::setw( x.width() ) 00554 << std::setprecision( x.precision() ) 00555 << x[i] 00556 << std::endl; 00557 } 00558 else 00559 { 00560 f << std::setw(x.width()) 00561 << i 00562 << std::fixed 00563 << std::showpoint 00564 << std::setw( x.width() ) 00565 << std::setprecision( x.precision() ) 00566 << x[i] 00567 << std::endl; 00568 } 00569 } 00570 f.close(); 00571 } 00572 00574 template<typename REAL> 00575 inline void gnuplot( 00576 const std::string& fname, 00577 const Vector<REAL> x, 00578 const Vector<REAL> y 00579 ) 00580 { 00581 std::fstream f(fname.c_str(),std::ios::out); 00582 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++) 00583 { 00584 if( x.scientific() ) 00585 { 00586 f << std::setw(x.width()) 00587 << i 00588 << std::scientific 00589 << std::showpoint 00590 << std::setw( x.width() ) 00591 << std::setprecision( x.precision() ) 00592 << x[i] 00593 << " " 00594 << std::setw( x.width() ) 00595 << std::setprecision( x.precision() ) 00596 << y[i] 00597 << std::endl; 00598 } 00599 else 00600 { 00601 f << std::setw(x.width()) 00602 << i 00603 << std::fixed 00604 << std::showpoint 00605 << std::setw( x.width() ) 00606 << std::setprecision( x.precision() ) 00607 << x[i] 00608 << " " 00609 << std::setw( x.width() ) 00610 << std::setprecision( x.precision() ) 00611 << y[i] 00612 << std::endl; 00613 } 00614 } 00615 00616 f.close(); 00617 } 00618 00619 00620 00649 template<typename REAL> 00650 inline void readVectorFromFile (const std::string& filename, Vector<REAL> &vector) 00651 { 00652 std::string buffer; 00653 std::ifstream fin( filename.c_str() ); 00654 if( fin.is_open() ){ 00655 while( fin ){ 00656 std::string sub; 00657 fin >> sub; 00658 //std::cout << " sub = " << sub.c_str() << ": "; 00659 if( sub.length()>0 ){ 00660 REAL a = atof(sub.c_str()); 00661 //std::cout << std::fixed << std::setw(10) << std::setprecision(5) << a; 00662 vector.push_back(a); 00663 } 00664 } 00665 fin.close(); 00666 } 00667 else{ 00668 HDNUM_ERROR("Could not open file!"); 00669 } 00670 } 00671 00672 00674 template<class REAL> 00675 inline void zero (Vector<REAL>& x) 00676 { 00677 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++) 00678 x[i] = REAL(0); 00679 } 00680 00682 template<class REAL> 00683 inline REAL norm (Vector<REAL> x) 00684 { 00685 REAL sum(0.0); 00686 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++) 00687 sum += x[i]*x[i]; 00688 return sqrt(sum); 00689 } 00690 00692 template<class REAL> 00693 inline void fill (Vector<REAL>& x, const REAL t) 00694 { 00695 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++) 00696 x[i] = t; 00697 } 00698 00721 template<class REAL> 00722 inline void fill (Vector<REAL>& x, const REAL& t, const REAL& dt) 00723 { 00724 REAL myt(t); 00725 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++) 00726 { 00727 x[i] = myt; 00728 myt += dt; 00729 } 00730 } 00731 00732 00755 template<class REAL> 00756 inline void unitvector (Vector<REAL> & x, std::size_t j) 00757 { 00758 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++) 00759 if (i==j) 00760 x[i] = REAL(1); 00761 else 00762 x[i] = REAL(0); 00763 } 00764 00765 00766 } // end of namespace hdnum 00767 00768 #endif /* _VECTOR_HH */