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