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