Heidelberg Educational Numerics Library Version 0.27 (from 15 March 2021)
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 <assert.h>
13
14#include <cmath>
15#include <cstdlib>
16#include <fstream>
17#include <iomanip>
18#include <iostream>
19#include <sstream>
20#include <vector>
21
22#include "exceptions.hh"
23
24namespace hdnum {
25
29 template<typename REAL>
30 class Vector : public std::vector<REAL> // inherit from the STL vector
31 {
32 public:
34 typedef std::size_t size_type;
35
36 private:
37 static bool bScientific;
38 static std::size_t nIndexWidth;
39 static std::size_t nValueWidth;
40 static std::size_t nValuePrecision;
41
42 public:
43
45 Vector() : std::vector<REAL>()
46 {
47 }
48
50 Vector( const size_t size, // user must specify the size
51 const REAL defaultvalue_ = 0 // if not specified, the value 0 will take effect
52 )
53 : std::vector<REAL>( size, defaultvalue_ )
54 {
55 }
56
58 Vector (const std::initializer_list<REAL> &v)
59 {
60 for (auto elem : v) this->push_back(elem);
61 }
62
63 // Methods:
64
86 Vector& operator=( const REAL value )
87 {
88 const size_t s = this->size();
89 Vector & self = *this;
90 for(size_t i=0; i<s; ++i)
91 self[i] = value;
92 return *this;
93 }
94
105 {
106 Vector v(m);
107 Vector &self = *this;
108 size_type k=0;
109 for (size_type j=i; j<i+m; j++){
110 v[k]=self[j];
111 k++;
112 }
113 return v;
114 }
115
116
117
118#ifdef DOXYGEN
150 Vector& operator=( const Vector& y )
151 {
152 // It is already implemented in the STL vector class itself!
153 }
154#endif
155
156
157
159 Vector& operator*=( const REAL value )
160 {
161 Vector &self = *this;
162 for (size_t i = 0; i < this->size(); ++i)
163 self[i] *= value;
164 return *this;
165 }
166
167
169 Vector& operator/=( const REAL value )
170 {
171 Vector &self = *this;
172 for (size_t i = 0; i < this->size(); ++i)
173 self[i] /= value;
174 return *this;
175 }
176
177
180 {
181 assert( this->size() == y.size());
182 Vector &self = *this;
183 for (size_t i = 0; i < this->size(); ++i)
184 self[i] += y[i];
185 return *this;
186 }
187
188
191 {
192 assert( this->size() == y.size());
193 Vector &self = *this;
194 for (size_t i = 0; i < this->size(); ++i)
195 self[i] -= y[i];
196 return *this;
197 }
198
199
201 Vector & update(const REAL alpha, const Vector & y)
202 {
203 assert( this->size() == y.size());
204 Vector &self = *this;
205 for (size_t i = 0; i < this->size(); ++i)
206 self[i] += alpha * y[i];
207 return *this;
208 }
209
210
243 {
244 assert( x.size() == this->size() ); // checks if the dimensions of the two vectors are equal
245 REAL sum( 0 );
246 const Vector & self = *this;
247 for( size_t i = 0; i < this->size(); ++i )
248 sum += self[i] * x[i];
249 return sum;
250 }
251
252
253
254
288 {
289 assert( x.size() == this->size() ); // checks if the dimensions of the two vectors are equal
290 Vector sum( *this );
291 sum += x;
292 return sum;
293 }
294
295
296
330 {
331 assert( x.size() == this->size() ); // checks if the dimensions of the two vectors are equal
332 Vector sum( *this );
333 sum -= x;
334 return sum;
335 }
336
337
338
341 {
342 REAL sum( 0 );
343 const Vector & self = *this;
344 for (size_t i = 0; i < (size_t) this->size(); ++i)
345 sum += self[i] * self[i];
346 return sum;
347 }
348
374 {
375 return sqrt(two_norm_2());
376 }
377
379 bool scientific() const
380 {
381 return bScientific;
382 }
383
411 void scientific(bool b) const
412 {
413 bScientific=b;
414 }
415
417 std::size_t iwidth () const
418 {
419 return nIndexWidth;
420 }
421
423 std::size_t width () const
424 {
425 return nValueWidth;
426 }
427
429 std::size_t precision () const
430 {
431 return nValuePrecision;
432 }
433
435 void iwidth (std::size_t i) const
436 {
437 nIndexWidth=i;
438 }
439
441 void width (std::size_t i) const
442 {
443 nValueWidth=i;
444 }
445
447 void precision (std::size_t i) const
448 {
449 nValuePrecision=i;
450 }
451
452 };
453
454
455
456 template<typename REAL>
457 bool Vector<REAL>::bScientific = true;
458
459 template<typename REAL>
460 std::size_t Vector<REAL>::nIndexWidth = 2;
461
462 template<typename REAL>
463 std::size_t Vector<REAL>::nValueWidth = 15;
464
465 template<typename REAL>
466 std::size_t Vector<REAL>::nValuePrecision = 7;
467
468
490 template <typename REAL>
491 inline std::ostream & operator <<(std::ostream & os, const Vector<REAL> & x)
492 {
493 os << std::endl;
494
495 for (size_t r = 0; r < x.size(); ++r)
496 {
497 if( x.scientific() )
498 {
499 os << "["
500 << std::setw(x.iwidth())
501 << r
502 << "]"
503 << std::scientific
504 << std::showpoint
505 << std::setw( x.width() )
506 << std::setprecision( x.precision() )
507 << x[r]
508 << std::endl;
509 }
510 else
511 {
512 os << "["
513 << std::setw(x.iwidth())
514 << r
515 << "]"
516 << std::fixed
517 << std::showpoint
518 << std::setw( x.width() )
519 << std::setprecision( x.precision() )
520 << x[r]
521 << std::endl;
522 }
523 }
524 return os;
525 }
526
527
528
551 template<typename REAL>
552 inline void gnuplot(
553 const std::string& fname,
554 const Vector<REAL> x
555 )
556 {
557 std::fstream f(fname.c_str(),std::ios::out);
558 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
559 {
560 if( x.scientific() )
561 {
562 f << std::setw(x.width())
563 << i
564 << std::scientific
565 << std::showpoint
566 << std::setw( x.width() )
567 << std::setprecision( x.precision() )
568 << x[i]
569 << std::endl;
570 }
571 else
572 {
573 f << std::setw(x.width())
574 << i
575 << std::fixed
576 << std::showpoint
577 << std::setw( x.width() )
578 << std::setprecision( x.precision() )
579 << x[i]
580 << std::endl;
581 }
582 }
583 f.close();
584 }
585
586 template<typename REAL>
587 inline void gnuplot(
588 const std::string& fname,
589 const std::vector<std::string>& t,
590 const Vector<REAL> x
591 )
592 {
593 std::fstream f(fname.c_str(),std::ios::out);
594 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
595 {
596 if( x.scientific() )
597 {
598 f << t[i] << " "
599 << std::scientific
600 << std::showpoint
601 << std::setw( x.width() )
602 << std::setprecision( x.precision() )
603 << x[i]
604 << std::endl;
605 }
606 else
607 {
608 f << t[i] << " "
609 << std::fixed
610 << std::showpoint
611 << std::setw( x.width() )
612 << std::setprecision( x.precision() )
613 << x[i]
614 << std::endl;
615 }
616 }
617 f.close();
618 }
619
621 template<typename REAL>
622 inline void gnuplot(
623 const std::string& fname,
624 const Vector<REAL> x,
625 const Vector<REAL> y
626 )
627 {
628 std::fstream f(fname.c_str(),std::ios::out);
629 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
630 {
631 if( x.scientific() )
632 {
633 f << std::setw(x.width())
634 << i
635 << std::scientific
636 << std::showpoint
637 << std::setw( x.width() )
638 << std::setprecision( x.precision() )
639 << x[i]
640 << " "
641 << std::setw( x.width() )
642 << std::setprecision( x.precision() )
643 << y[i]
644 << std::endl;
645 }
646 else
647 {
648 f << std::setw(x.width())
649 << i
650 << std::fixed
651 << std::showpoint
652 << std::setw( x.width() )
653 << std::setprecision( x.precision() )
654 << x[i]
655 << " "
656 << std::setw( x.width() )
657 << std::setprecision( x.precision() )
658 << y[i]
659 << std::endl;
660 }
661 }
662
663 f.close();
664 }
665
666
667
696 template<typename REAL>
697 inline void readVectorFromFile (const std::string& filename, Vector<REAL> &vector)
698 {
699 std::string buffer;
700 std::ifstream fin( filename.c_str() );
701 if( fin.is_open() ){
702 while( fin ){
703 std::string sub;
704 fin >> sub;
705 //std::cout << " sub = " << sub.c_str() << ": ";
706 if( sub.length()>0 ){
707 REAL a = atof(sub.c_str());
708 //std::cout << std::fixed << std::setw(10) << std::setprecision(5) << a;
709 vector.push_back(a);
710 }
711 }
712 fin.close();
713 }
714 else{
715 HDNUM_ERROR("Could not open file!");
716 }
717 }
718
719
721 template<class REAL>
722 inline void zero (Vector<REAL>& x)
723 {
724 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
725 x[i] = REAL(0);
726 }
727
729 template<class REAL>
730 inline REAL norm (Vector<REAL> x)
731 {
732 REAL sum(0.0);
733 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
734 sum += x[i]*x[i];
735 return sqrt(sum);
736 }
737
739 template<class REAL>
740 inline void fill (Vector<REAL>& x, const REAL t)
741 {
742 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
743 x[i] = t;
744 }
745
768 template<class REAL>
769 inline void fill (Vector<REAL>& x, const REAL& t, const REAL& dt)
770 {
771 REAL myt(t);
772 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
773 {
774 x[i] = myt;
775 myt += dt;
776 }
777 }
778
779
802 template<class REAL>
803 inline void unitvector (Vector<REAL> & x, std::size_t j)
804 {
805 for (typename Vector<REAL>::size_type i=0; i<x.size(); i++)
806 if (i==j)
807 x[i] = REAL(1);
808 else
809 x[i] = REAL(0);
810 }
811
812
813} // end of namespace hdnum
814
815#endif /* _VECTOR_HH */
Class with mathematical matrix operations.
Definition densematrix.hh:33
std::size_t iwidth() const
get index field width for pretty-printing
Definition densematrix.hh:182
std::size_t precision() const
get data precision for pretty-printing
Definition densematrix.hh:188
std::size_t width() const
get data field width for pretty-printing
Definition densematrix.hh:185
Class with mathematical vector operations.
Definition vector.hh:31
void precision(std::size_t i) const
set data precision for pretty-printing
Definition vector.hh:447
std::size_t size_type
Type used for array indices.
Definition vector.hh:34
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:50
REAL operator*(Vector &x) const
Inner product with another vector.
Definition vector.hh:242
Vector & operator/=(const REAL value)
Division by a scalar value (x /= value)
Definition vector.hh:169
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:769
std::size_t width() const
get data field width for pretty-printing
Definition vector.hh:423
void scientific(bool b) const
scientific(true) is the default, scientific(false) switches to the fixed point representation
Definition vector.hh:411
Vector sub(size_type i, size_type m)
Subvector extraction.
Definition vector.hh:104
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:803
Vector & operator+=(const Vector &y)
Add another vector (x += y)
Definition vector.hh:179
Vector()
default constructor, also inherited from the STL vector default constructor
Definition vector.hh:45
std::size_t precision() const
get data precision for pretty-printing
Definition vector.hh:429
bool scientific() const
pretty-print output property: true = scientific, false = fixed point representation
Definition vector.hh:379
std::size_t iwidth() const
get index field width for pretty-printing
Definition vector.hh:417
Vector operator-(Vector &x) const
vector subtraction x-y
Definition vector.hh:329
REAL two_norm_2() const
Square of the Euclidean norm.
Definition vector.hh:340
Vector(const std::initializer_list< REAL > &v)
constructor from initializer list
Definition vector.hh:58
Vector & update(const REAL alpha, const Vector &y)
Update vector by addition of a scaled vector (x += a y )
Definition vector.hh:201
Vector & operator-=(const Vector &y)
Subtract another vector (x -= y)
Definition vector.hh:190
Vector & operator*=(const REAL value)
Multiplication by a scalar value (x *= value)
Definition vector.hh:159
Vector & operator=(const REAL value)
Assign all values of the Vector from one scalar value: x = value.
Definition vector.hh:86
Vector operator+(Vector &x) const
Adding two vectors x+y.
Definition vector.hh:287
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:552
void readVectorFromFile(const std::string &filename, Vector< REAL > &vector)
Read vector from a text file.
Definition vector.hh:697
void width(std::size_t i) const
set data field width for pretty-printing
Definition vector.hh:441
REAL two_norm() const
Euclidean norm of a vector.
Definition vector.hh:373
void iwidth(std::size_t i) const
set index field width for pretty-printing
Definition vector.hh:435
A few common exception classes.