My Project
dgbmatrix-io.hpp
1 //=============================================================================
3 inline double& dgbmatrix::operator()(const long& i, const long& j)
4 {
5 #ifdef CPPL_VERBOSE
6  std::cerr << "# [MARK] dgbmatrix::operator()(const long&, const long&)"
7  << std::endl;
8 #endif//CPPL_VERBOSE
9 
10 #ifdef CPPL_DEBUG
11  if( i<0 || j<0 || M<=i || N<=j || i-j>KL || j-i>KU ){
12  std::cerr << "[ERROR] dgbmatrix::operator()(long, long)" << std::endl
13  << "The required component is out of the matrix size."
14  << std::endl
15  << "Your input was (" << i << "," << j << ")." << std::endl;
16  exit(1);
17  }
18 #endif//CPPL_DEBUG
19 
20  //return Array[KU+i+(KL+KU)*j];
21  return Darray[j][KU-j+i];
22 }
23 
24 //=============================================================================
26 inline double dgbmatrix::operator()(const long& i, const long& j) const
27 {
28 #ifdef CPPL_VERBOSE
29  std::cerr << "# [MARK] dgbmatrix::operator()(const long&, const long&) const"
30  << std::endl;
31 #endif//CPPL_VERBOSE
32 
33 #ifdef CPPL_DEBUG
34  if( i<0 || j<0 || M<=i || N<=j || i-j>KL || j-i>KU ){
35  std::cerr << "[ERROR] dgbmatrix::operator()(long, long)" << std::endl
36  << "The required component is out of the matrix size."
37  << std::endl
38  << "Your input was (" << i << "," << j << ")." << std::endl;
39  exit(1);
40  }
41 #endif//CPPL_DEBUG
42 
43  //return Array[KU+i+(KL+KU)*j];
44  return Darray[j][KU-j+i];
45 }
46 
50 
51 //=============================================================================
53 inline void dgbmatrix::set(const long& i, const long& j, const double& v) const
54 {
55 #ifdef CPPL_VERBOSE
56  std::cerr << "# [MARK] dgbmatrix::set(const long&, const long&, const double&) const"
57  << std::endl;
58 #endif//CPPL_VERBOSE
59 
60 #ifdef CPPL_DEBUG
61  if( i<0 || j<0 || M<=i || N<=j || i-j>KL || j-i>KU ){
62  std::cerr << "[ERROR] dgbmatrix::set(long&, long&, double&)" << std::endl
63  << "The required component is out of the matrix size."
64  << std::endl
65  << "Your input was (" << i << "," << j << ")." << std::endl;
66  exit(1);
67  }
68 #endif//CPPL_DEBUG
69 
70  //Array[KU+i+(KL+KU)*j] =v;
71  Darray[j][KU-j+i] =v;
72 }
73 
77 
78 //=============================================================================
79 inline std::ostream& operator<<(std::ostream& s, const dgbmatrix& mat)
80 {
81 #ifdef CPPL_VERBOSE
82  std::cerr << "# [MARK] operator<<(std::ostream&, const dgbmatrix&)"
83  << std::endl;
84 #endif//CPPL_VERBOSE
85 
86  for(long i=0; i<mat.M; i++){
87  for(long j=0; j<mat.N; j++){
88  if( i-j>mat.KL || j-i>mat.KU ){ s << " x"; }
89  else{ s << " " << mat(i,j); }
90  }
91  s << std::endl;
92  }
93 
94  return s;
95 }
96 
100 
101 //=============================================================================
102 inline void dgbmatrix::write(const char *filename) const
103 {
104 #ifdef CPPL_VERBOSE
105  std::cerr << "# [MARK] dgbmatrix::write(const char*) const"
106  << std::endl;
107 #endif//CPPL_VERBOSE
108 
109  std::ofstream s(filename, std::ios::trunc);
110 
111  s << "dgbmatrix" << " "
112  << M << " " << N << " " << KL << " " << KU << std::endl;
113  for(long i=0; i<M; i++){
114  for(long j=max(0,i-KL); j<min(N,i+KU+1); j++){
115  s << operator()(i,j) << " ";
116  }
117  s << std::endl;
118  }
119 
120  s.close();
121 }
122 
123 //=============================================================================
124 inline void dgbmatrix::read(const char* filename)
125 {
126 #ifdef CPPL_VERBOSE
127  std::cerr << "# [MARK] dgbmatrix::read(const char*)"
128  << std::endl;
129 #endif//CPPL_VERBOSE
130 
131  std::ifstream s( filename );
132  if(!s){
133  std::cerr << "[ERROR] dgbmatrix::read(const char*) " << std::endl
134  << "The file \"" << filename << "\" can not be opened."
135  << std::endl;
136  exit(1);
137  }
138 
139  std::string id;
140  s >> id;
141  if( id != "dgbmatrix" ){
142  std::cerr << "[ERROR] dgbmatrix::read(const char*) " << std::endl
143  << "The type name of the file \"" << filename
144  << "\" is not dgbmatrix." << std::endl
145  << "Its type name was " << id << " ." << std::endl;
146  exit(1);
147  }
148 
149  s >> M >> N >> KL >> KU;
150  resize(M, N, KL, KU);
151  for(long i=0; i<M; i++){
152  for(long j=max(0,i-KL); j<min(N,i+KU+1); j++){
153  s >> operator()(i,j);
154  }
155  }
156  if(s.eof()){
157  std::cerr << "[ERROR] dgbmatrix::read(const char*) " << std::endl
158  << "There is something is wrong with the file \""
159  << filename << " ." << std::endl
160  << "Most likely, there is not enough data components, "
161  << "or a linefeed code or space code is missing "
162  << "at the end of the last line." << std::endl;
163  exit(1);
164  }
165 
166  s.close();
167 }
friend _dgematrix i(const dgbmatrix &)
Definition: dgbmatrix-calc.hpp:22
double & operator()(const long &, const long &)
Definition: dgbmatrix-io.hpp:3
void set(const long &, const long &, const double &) const
Definition: dgbmatrix-io.hpp:53
Real Double-precision General Band Matrix Class.
Definition: dgbmatrix.hpp:3
void resize(const long &, const long &, const long &, const long &)
Definition: dgbmatrix-misc.hpp:132