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