CPPLapack
 All Classes Files Functions Variables Friends Pages
dgbmatrix-io.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! operator() for non-const object */
3 inline double& dgbmatrix::operator()(const CPPL_INT& i, const CPPL_INT& j)
4 {CPPL_VERBOSE_REPORT;
5 #ifdef CPPL_DEBUG
6  if( i<0 || j<0 || m<=i || n<=j || i-j>kl || j-i>ku ){
7  ERROR_REPORT;
8  std::cerr << "The required component is out of the matrix size." << std::endl
9  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << m << "x" << n << " with kl=" << kl << ", ku=" << ku << "." << std::endl;
10  exit(1);
11  }
12 #endif//CPPL_DEBUG
13 
14  //return array[ku+i+(kl+ku)*j];
15  return darray[j][ku-j+i];
16 }
17 
18 //=============================================================================
19 /*! operator() for const object */
20 inline double dgbmatrix::operator()(const CPPL_INT& i, const CPPL_INT& j) const
21 {CPPL_VERBOSE_REPORT;
22 #ifdef CPPL_DEBUG
23  if( i<0 || j<0 || m<=i || n<=j || i-j>kl || j-i>ku ){
24  ERROR_REPORT;
25  std::cerr << "The required component is out of the matrix size." << std::endl
26  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << m << "x" << n << " with kl=" << kl << ", ku=" << ku << "." << std::endl;
27  exit(1);
28  }
29 #endif//CPPL_DEBUG
30 
31  //return array[ku+i+(kl+ku)*j];
32  return darray[j][ku-j+i];
33 }
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 //=============================================================================
40 /*! set value for const object */
41 inline dgbmatrix& dgbmatrix::set(const CPPL_INT& i, const CPPL_INT& j, const double& v) //const
42 {CPPL_VERBOSE_REPORT;
43 #ifdef CPPL_DEBUG
44  if( i<0 || j<0 || m<=i || n<=j || i-j>kl || j-i>ku ){
45  ERROR_REPORT;
46  std::cerr << "The required component is out of the matrix size." << std::endl
47  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << m << "x" << n << " with kl=" << kl << ", ku=" << ku << "." << std::endl;
48  exit(1);
49  }
50 #endif//CPPL_DEBUG
51 
52  //array[ku+i+(kl+ku)*j] =v;
53  darray[j][ku-j+i] =v;
54  return *this;
55 }
56 
57 ///////////////////////////////////////////////////////////////////////////////
58 ///////////////////////////////////////////////////////////////////////////////
59 ///////////////////////////////////////////////////////////////////////////////
60 
61 //=============================================================================
62 inline std::ostream& operator<<(std::ostream& s, const dgbmatrix& mat)
63 {CPPL_VERBOSE_REPORT;
64  for(CPPL_INT i=0; i<mat.m; i++){
65  for(CPPL_INT j=0; j<mat.n; j++){
66  if( i-j>mat.kl || j-i>mat.ku ){ s << " x"; }
67  else{ s << " " << mat(i,j); }
68  }
69  s << std::endl;
70  }
71 
72  return s;
73 }
74 
75 ///////////////////////////////////////////////////////////////////////////////
76 ///////////////////////////////////////////////////////////////////////////////
77 ///////////////////////////////////////////////////////////////////////////////
78 
79 //=============================================================================
80 inline void dgbmatrix::write(const char *filename) const
81 {CPPL_VERBOSE_REPORT;
82  std::ofstream ofs(filename, std::ios::trunc);
83  ofs.setf(std::cout.flags());
84  ofs.precision(std::cout.precision());
85  ofs.width(std::cout.width());
86  ofs.fill(std::cout.fill());
87 
88  ofs << "#dgbmatrix" << " " << m << " " << n << " " << kl << " " << ku << std::endl;
89 
90  for(CPPL_INT i=0; i<m; i++){
91  const CPPL_INT jmax =std::min(n,i+ku+1);
92  for(CPPL_INT j=std::max(CPPL_INT(0),i-kl); j<jmax; j++){
93  ofs << operator()(i,j) << " ";
94  }
95  ofs << std::endl;
96  }
97 
98  ofs.close();
99 }
100 
101 //=============================================================================
102 inline void dgbmatrix::read(const char* filename)
103 {CPPL_VERBOSE_REPORT;
104  std::ifstream s( filename );
105  if(!s){
106  ERROR_REPORT;
107  std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
108  exit(1);
109  }
110 
111  std::string id;
112  s >> id;
113  if( id != "dgbmatrix" && id != "#dgbmatrix" ){
114  ERROR_REPORT;
115  std::cerr << "The type name of the file \"" << filename << "\" is not dgbmatrix." << std::endl
116  << "Its type name was " << id << " ." << std::endl;
117  exit(1);
118  }
119 
120  s >> m >> n >> kl >> ku;
121  resize(m, n, kl, ku);
122 
123  for(CPPL_INT i=0; i<m; i++){
124  const CPPL_INT jmax =std::min(n,i+ku+1);
125  for(CPPL_INT j=std::max(CPPL_INT(0),i-kl); j<jmax; j++){
126  s >> operator()(i,j);
127  }
128  }
129  if(s.eof()){
130  ERROR_REPORT;
131  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
132  << "Most likely, there is not enough data components, or a linefeed code or space code is missing at the end of the last line." << std::endl;
133  exit(1);
134  }
135 
136  s >> id;
137  if(!s.eof()){
138  ERROR_REPORT;
139  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
140  << "Most likely, there are extra data components." << std::endl;
141  exit(1);
142  }
143 
144  s.close();
145 }
friend _dgematrix i(const dgbmatrix &)
CPPL_INT m
matrix row size
Definition: dgbmatrix.hpp:9
dgbmatrix & set(const CPPL_INT &, const CPPL_INT &, const double &)
double ** darray
array of pointers of column head addresses
Definition: dgbmatrix.hpp:14
_dgematrix i(const _dgbmatrix &mat)
CPPL_INT kl
lower band width
Definition: dgbmatrix.hpp:11
CPPL_INT ku
upper band width
Definition: dgbmatrix.hpp:12
void write(const char *) const
void read(const char *)
dgbmatrix & resize(const CPPL_INT &, const CPPL_INT &, const CPPL_INT &, const CPPL_INT &)
Real Double-precision General Band Matrix Class.
Definition: dgbmatrix.hpp:3
std::ostream & operator<<(std::ostream &s, const dgbmatrix &mat)
double & operator()(const CPPL_INT &, const CPPL_INT &)
Definition: dgbmatrix-io.hpp:3
CPPL_INT n
matrix column size
Definition: dgbmatrix.hpp:10