Heidelberg Educational Numerics Library  Version 0.24 (from 9 September 2011)
vector.hh
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 /*
3  * File: vector.hh
4  * Author: ngo
5  *
6  * Created on April 14th, 2011
7  */
8 
9 #ifndef _VECTOR_HH
10 #define _VECTOR_HH
11 
12 #include <vector>
13 #include <cmath>
14 #include <assert.h>
15 #include <iostream>
16 #include <fstream>
17 #include <sstream>
18 #include <iomanip>
19 #include <cstdlib>
20 
21 
22 namespace hdnum {
23 
27  template<typename REAL>
28  class Vector : public std::vector<REAL> // inherit from the STL vector
29  {
30  public:
32  typedef std::size_t size_type;
33 
34  private:
35  static bool bScientific;
36  static std::size_t nIndexWidth;
37  static std::size_t nValueWidth;
38  static std::size_t nValuePrecision;
39 
40  public:
41 
43  Vector() : std::vector<REAL>()
44  {
45  }
46 
48  Vector( const size_t size, // user must specify the size
49  const REAL defaultvalue_ = 0 // if not specified, the value 0 will take effect
50  )
51  : std::vector<REAL>( size, defaultvalue_ )
52  {
53  }
54 
55 
56  // Methods:
57 
79  Vector& operator=( const REAL value )
80  {
81  const size_t s = this->size();
82  Vector & self = *this;
83  for(size_t i=0; i<s; ++i)
84  self[i] = value;
85  return *this;
86  }
87 
97  Vector sub (size_type i, size_type m)
98  {
99  Vector v(m);
100  Vector &self = *this;
101  size_type k=0;
102  for (size_type j=i; j<i+m; j++){
103  v[k]=self[j];
104  k++;
105  }
106  return v;
107  }
108 
109 
110 
111 #ifdef DOXYGEN
112 
143  Vector& operator=( const Vector& y )
144  {
145  // It is already implemented in the STL vector class itself!
146  }
147 #endif
148 
149 
150 
152  Vector& operator*=( const REAL value )
153  {
154  Vector &self = *this;
155  for (size_t i = 0; i < this->size(); ++i)
156  self[i] *= value;
157  return *this;
158  }
159 
160 
162  Vector& operator/=( const REAL value )
163  {
164  Vector &self = *this;
165  for (size_t i = 0; i < this->size(); ++i)
166  self[i] /= value;
167  return *this;
168  }
169 
170 
172  Vector& operator+=( const Vector & y )
173  {
174  assert( this->size() == y.size());
175  Vector &self = *this;
176  for (size_t i = 0; i < this->size(); ++i)
177  self[i] += y[i];
178  return *this;
179  }
180 
181 
183  Vector& operator-=( const Vector & y )
184  {
185  assert( this->size() == y.size());
186  Vector &self = *this;
187  for (size_t i = 0; i < this->size(); ++i)
188  self[i] -= y[i];
189  return *this;
190  }
191 
192 
194  Vector & update(const REAL alpha, const Vector & y)
195  {
196  assert( this->size() == y.size());
197  Vector &self = *this;
198  for (size_t i = 0; i < this->size(); ++i)
199  self[i] += alpha * y[i];
200  return *this;
201  }
202 
203 
235  REAL operator*(Vector & x) const
236  {
237  assert( x.size() == this->size() ); // checks if the dimensions of the two vectors are equal
238  REAL sum( 0 );
239  const Vector & self = *this;
240  for( size_t i = 0; i < this->size(); ++i )
241  sum += self[i] * x[i];
242  return sum;
243  }
244 
245 
246 
247 
281  {
282  assert( x.size() == this->size() ); // checks if the dimensions of the two vectors are equal
283  Vector sum( *this );
284  sum += x;
285  return sum;
286  }
287 
288 
289 
323  {
324  assert( x.size() == this->size() ); // checks if the dimensions of the two vectors are equal
325  Vector sum( *this );
326  sum -= x;
327  return sum;
328  }
329 
330 
331 
333  REAL two_norm_2() const
334  {
335  REAL sum( 0 );
336  const Vector & self = *this;
337  for (size_t i = 0; i < (size_t) this->size(); ++i)
338  sum += self[i] * self[i];
339  return sum;
340  }
341 
366  REAL two_norm() const
367  {
368  return sqrt(two_norm_2());
369  }
370 
372  bool scientific() const
373  {
374  return bScientific;
375  }
376 
404  void scientific(bool b) const
405  {
406  bScientific=b;
407  }
408 
410  std::size_t iwidth () const
411  {
412  return nIndexWidth;
413  }
414 
416  std::size_t width () const
417  {
418  return nValueWidth;
419  }
420 
422  std::size_t precision () const
423  {
424  return nValuePrecision;
425  }
426 
428  void iwidth (std::size_t i) const
429  {
430  nIndexWidth=i;
431  }
432 
434  void width (std::size_t i) const
435  {
436  nValueWidth=i;
437  }
438 
440  void precision (std::size_t i) const
441  {
442  nValuePrecision=i;
443  }
444 
445  };
446 
447 
448 
449  template<typename REAL>
450  bool Vector<REAL>::bScientific = true;
451 
452  template<typename REAL>
453  std::size_t Vector<REAL>::nIndexWidth = 2;
454 
455  template<typename REAL>
456  std::size_t Vector<REAL>::nValueWidth = 15;
457 
458  template<typename REAL>
459  std::size_t Vector<REAL>::nValuePrecision = 7;
460 
461 
483  template <typename REAL>
484  inline std::ostream & operator <<(std::ostream & os, const Vector<REAL> & x)
485  {
486  os << std::endl;
487 
488  for (size_t r = 0; r < x.size(); ++r)
489  {
490  if( x.scientific() )
491  {
492  os << "["
493  << std::setw(x.iwidth())
494  << r
495  << "]"
496  << std::scientific
497  << std::showpoint
498  << std::setw( x.width() )
499  << std::setprecision( x.precision() )
500  << x[r]
501  << std::endl;
502  }
503  else
504  {
505  os << "["
506  << std::setw(x.iwidth())
507  << r
508  << "]"
509  << std::fixed
510  << std::showpoint
511  << std::setw( x.width() )
512  << std::setprecision( x.precision() )
513  << x[r]
514  << std::endl;
515  }
516  }
517  return os;
518  }
519 
520 
521 
544  template<typename REAL>
545  inline void gnuplot(
546  const std::string& fname,
547  const Vector<REAL> x
548  )
549  {
550  std::fstream f(fname.c_str(),std::ios::out);
551  for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
552  {
553  if( x.scientific() )
554  {
555  f << std::setw(x.width())
556  << i
557  << std::scientific
558  << std::showpoint
559  << std::setw( x.width() )
560  << std::setprecision( x.precision() )
561  << x[i]
562  << std::endl;
563  }
564  else
565  {
566  f << std::setw(x.width())
567  << i
568  << std::fixed
569  << std::showpoint
570  << std::setw( x.width() )
571  << std::setprecision( x.precision() )
572  << x[i]
573  << std::endl;
574  }
575  }
576  f.close();
577  }
578 
580  template<typename REAL>
581  inline void gnuplot(
582  const std::string& fname,
583  const Vector<REAL> x,
584  const Vector<REAL> y
585  )
586  {
587  std::fstream f(fname.c_str(),std::ios::out);
588  for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
589  {
590  if( x.scientific() )
591  {
592  f << std::setw(x.width())
593  << i
594  << std::scientific
595  << std::showpoint
596  << std::setw( x.width() )
597  << std::setprecision( x.precision() )
598  << x[i]
599  << " "
600  << std::setw( x.width() )
601  << std::setprecision( x.precision() )
602  << y[i]
603  << std::endl;
604  }
605  else
606  {
607  f << std::setw(x.width())
608  << i
609  << std::fixed
610  << std::showpoint
611  << std::setw( x.width() )
612  << std::setprecision( x.precision() )
613  << x[i]
614  << " "
615  << std::setw( x.width() )
616  << std::setprecision( x.precision() )
617  << y[i]
618  << std::endl;
619  }
620  }
621 
622  f.close();
623  }
624 
625 
626 
655  template<typename REAL>
656  inline void readVectorFromFile (const std::string& filename, Vector<REAL> &vector)
657  {
658  std::string buffer;
659  std::ifstream fin( filename.c_str() );
660  if( fin.is_open() ){
661  while( fin ){
662  std::string sub;
663  fin >> sub;
664  //std::cout << " sub = " << sub.c_str() << ": ";
665  if( sub.length()>0 ){
666  REAL a = atof(sub.c_str());
667  //std::cout << std::fixed << std::setw(10) << std::setprecision(5) << a;
668  vector.push_back(a);
669  }
670  }
671  fin.close();
672  }
673  else{
674  HDNUM_ERROR("Could not open file!");
675  }
676  }
677 
678 
680  template<class REAL>
681  inline void zero (Vector<REAL>& x)
682  {
683  for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
684  x[i] = REAL(0);
685  }
686 
688  template<class REAL>
689  inline REAL norm (Vector<REAL> x)
690  {
691  REAL sum(0.0);
692  for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
693  sum += x[i]*x[i];
694  return sqrt(sum);
695  }
696 
698  template<class REAL>
699  inline void fill (Vector<REAL>& x, const REAL t)
700  {
701  for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
702  x[i] = t;
703  }
704 
727  template<class REAL>
728  inline void fill (Vector<REAL>& x, const REAL& t, const REAL& dt)
729  {
730  REAL myt(t);
731  for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
732  {
733  x[i] = myt;
734  myt += dt;
735  }
736  }
737 
738 
761  template<class REAL>
762  inline void unitvector (Vector<REAL> & x, std::size_t j)
763  {
764  for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
765  if (i==j)
766  x[i] = REAL(1);
767  else
768  x[i] = REAL(0);
769  }
770 
771 
772 } // end of namespace hdnum
773 
774 #endif /* _VECTOR_HH */
Class with mathematical vector operations.
Definition: vector.hh:28
Vector & operator=(const REAL value)
Assign all values of the Vector from one scalar value: x = value.
Definition: vector.hh:79
Vector & operator-=(const Vector &y)
Subtract another vector (x -= y)
Definition: vector.hh:183
std::size_t precision() const
get data precision for pretty-printing
Definition: vector.hh:422
Vector sub(size_type i, size_type m)
Subvector extraction.
Definition: vector.hh:97
Vector()
default constructor, also inherited from the STL vector default constructor
Definition: vector.hh:43
Vector operator-(Vector &x) const
vector subtraction x-y
Definition: vector.hh:322
REAL operator*(Vector &x) const
Inner product with another vector.
Definition: vector.hh:235
void fill(Vector< REAL > &x, const REAL &t, const REAL &dt)
Fill vector, with entries starting at t, consecutively shifted by dt.
Definition: vector.hh:728
void unitvector(Vector< REAL > &x, std::size_t j)
Defines j-th unitvector (j=0,...,n-1) where n = length of the vector.
Definition: vector.hh:762
Vector & update(const REAL alpha, const Vector &y)
Update vector by addition of a scaled vector (x += a y )
Definition: vector.hh:194
bool scientific() const
pretty-print output property: true = scientific, false = fixed point representation ...
Definition: vector.hh:372
void precision(std::size_t i) const
set data precision for pretty-printing
Definition: vector.hh:440
void gnuplot(const std::string &fname, const Vector< REAL > x)
Output contents of a Vector x to a text file named fname.
Definition: vector.hh:545
REAL two_norm() const
Euclidean norm of a vector.
Definition: vector.hh:366
void scientific(bool b) const
scientific(true) is the default, scientific(false) switches to the fixed point representation ...
Definition: vector.hh:404
Vector & operator/=(const REAL value)
Division by a scalar value (x /= value)
Definition: vector.hh:162
Vector(const size_t size, const REAL defaultvalue_=0)
another constructor, with arguments, setting the default value for all entries of the vector of given...
Definition: vector.hh:48
Vector & operator*=(const REAL value)
Multiplication by a scalar value (x *= value)
Definition: vector.hh:152
Vector & operator+=(const Vector &y)
Add another vector (x += y)
Definition: vector.hh:172
void iwidth(std::size_t i) const
set index field width for pretty-printing
Definition: vector.hh:428
void readVectorFromFile(const std::string &filename, Vector< REAL > &vector)
Read vector from a text file.
Definition: vector.hh:656
std::size_t width() const
get data field width for pretty-printing
Definition: vector.hh:416
std::size_t iwidth() const
get index field width for pretty-printing
Definition: vector.hh:410
Definition: densematrix.hh:21
REAL two_norm_2() const
Square of the Euclidean norm.
Definition: vector.hh:333
Vector operator+(Vector &x) const
Adding two vectors x+y.
Definition: vector.hh:280
std::size_t size_type
Type used for array indices.
Definition: vector.hh:32
void width(std::size_t i) const
set data field width for pretty-printing
Definition: vector.hh:434