My Project
zssmatrix-io.hpp
1 //=============================================================================
3 inline std::complex<double> zssmatrix::operator()(const long& i, const long& j) const
4 {
5 #ifdef CPPL_VERBOSE
6  std::cerr << "# [MARK] zssmatrix::operator()(const long&, const long&) const"
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] zssmatrix::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  for(long c=0; c<VOL; c++){
21  if(Indx[c]==i && Jndx[c]==j){ return Array[c]; }
22  }
23 
25  std::cerr << "[ERROR] zssmatrix::operator()(long, long)" << std::endl
26  << "The required component is out of the matrix size."
27  << std::endl
28  << "Your input was (" << i << "," << j << ")." << std::endl;
29  exit(1);
30 }
31 
35 
36 //=============================================================================
38 inline void zssmatrix::put(const long& i, const long& j, const std::complex<double>& v)
39 {
40 #ifdef CPPL_VERBOSE
41  std::cerr << "# [MARK] zssmatrix::put(const long&, const long&, const std::complex<double>&)"
42  << std::endl;
43 #endif//CPPL_VERBOSE
44 
45 #ifdef CPPL_DEBUG
46  if( i<0 || j<0 || M<=i || N<=j ){
47  std::cerr << "[ERROR] zssmatrix::put(long&, long&, std::complex<double>&)" << std::endl
48  << "The required component is out of the matrix size."
49  << std::endl
50  << "Your input was (" << i << "," << j << ")." << std::endl;
51  exit(1);
52  }
53 #endif//CPPL_DEBUG
54 
56  for(long c=0; c<VOL; c++){
57  if(Indx[c]==i && Jndx[c]==j){ Array[c]=v; return; }
58  }
59 
61  if(VOL==CAP){
62  expand(CPPL_SS_SECTOR);
63 #ifdef CPPL_DEBUG
64  std::cerr << "[NOTE] zssmatrix::put(long&, long&, std::complex<double>&) "
65  << "The matrix volume was automatically expanded." << std::endl;
66 #endif//CPPL_DEBUG
67  }
68 
69  Indx[VOL]=i; Jndx[VOL]=j; Array[VOL]=v;
70  VOL++;
71 }
72 
73 //=============================================================================
75 inline void zssmatrix::fput(const long& i, const long& j, const std::complex<double>& v)
76 {
77 #ifdef CPPL_VERBOSE
78  std::cerr << "# [MARK] zssmatrix::fput(const long&, const long&, const std::complex<double>&)"
79  << std::endl;
80 #endif//CPPL_VERBOSE
81 
82 #ifdef CPPL_DEBUG
83  if( i<0 || j<0 || M<=i || N<=j ){
84  std::cerr << "[ERROR] zssmatrix::fput"
85  << "(const long&, lconst ong&, const std::complex<double>&)" << std::endl
86  << "The required component is out of the matrix size."
87  << std::endl
88  << "Your input was (" << i << "," << j << ")." << std::endl;
89  exit(1);
90  }
91 
92  if(VOL==CAP){
93  std::cerr << "[ERROR] zssmatrix::fput"
94  << "(const long&, const long&, const std::complex<double>&)" << std::endl
95  << "There is no free space to set a new entry." << std::endl;
96  exit(1);
97  }
98 
99  for(long c=0; c<VOL; c++){
100  if(Indx[c]==i && Jndx[c]==j){
101  std::cerr << "[ERROR] zssmatrix::fput"
102  << "(const long&, const long&, const std::complex<double>&)" << std::endl
103  << "The required component is already listed." << std::endl
104  << "Your input was (" << i << "," << j << ")." << std::endl;
105  exit(1);
106  }
107  }
108 #endif//CPPL_DEBUG
109 
110  Indx[VOL]=i; Jndx[VOL]=j; Array[VOL]=v;
111  VOL++;
112 }
113 
117 
118 //=============================================================================
120 inline void zssmatrix::add(const long& i, const long& j, const std::complex<double>& v)
121 {
122 #ifdef CPPL_VERBOSE
123  std::cerr << "# [MARK] zssmatrix::add(const long&, const long&, const std::complex<double>&)"
124  << std::endl;
125 #endif//CPPL_VERBOSE
126 
127 #ifdef CPPL_DEBUG
128  if( i<0 || j<0 || M<=i || N<=j ){
129  std::cerr << "[ERROR] zssmatrix::add(long&, long&, std::complex<double>&)" << std::endl
130  << "The required component is out of the matrix size."
131  << std::endl
132  << "Your input was (" << i << "," << j << ")." << std::endl;
133  exit(1);
134  }
135 #endif//CPPL_DEBUG
136 
138  for(long c=0; c<VOL; c++){
139  if(Indx[c]==i && Jndx[c]==j){ Array[c]+=v; return; }
140  }
141 
143  if(VOL==CAP){
144  expand(CPPL_SS_SECTOR);
145 #ifdef CPPL_DEBUG
146  std::cerr << "[NOTE] zssmatrix::add(long&, long&, std::complex<double>&) "
147  << "The matrix volume was automatically expanded." << std::endl;
148 #endif//CPPL_DEBUG
149  }
150 
151  Indx[VOL]=i; Jndx[VOL]=j; Array[VOL]=v;
152  VOL++;
153 }
154 
155 //=============================================================================
157 inline void zssmatrix::sub(const long& i, const long& j, const std::complex<double>& v)
158 {
159 #ifdef CPPL_VERBOSE
160  std::cerr << "# [MARK] zssmatrix::sub(const long&, const long&, const std::complex<double>&)"
161  << std::endl;
162 #endif//CPPL_VERBOSE
163 
164 #ifdef CPPL_DEBUG
165  if( i<0 || j<0 || M<=i || N<=j ){
166  std::cerr << "[ERROR] zssmatrix::sub(long&, long&, std::complex<double>&)" << std::endl
167  << "The required component is out of the matrix size."
168  << std::endl
169  << "Your input was (" << i << "," << j << ")." << std::endl;
170  exit(1);
171  }
172 #endif//CPPL_DEBUG
173 
175  for(long c=0; c<VOL; c++){
176  if(Indx[c]==i && Jndx[c]==j){ Array[c]-=v; return; }
177  }
178 
180  if(VOL==CAP){
181  expand(CPPL_SS_SECTOR);
182 #ifdef CPPL_DEBUG
183  std::cerr << "[NOTE] zssmatrix::sub(long&, long&, std::complex<double>&) "
184  << "The matrix volume was automatically expanded." << std::endl;
185 #endif//CPPL_DEBUG
186  }
187 
188  Indx[VOL]=i; Jndx[VOL]=j; Array[VOL]=-v;
189  VOL++;
190 }
191 
192 //=============================================================================
194 inline void zssmatrix::mult(const long& i, const long& j, const std::complex<double>& v)
195 {
196 #ifdef CPPL_VERBOSE
197  std::cerr << "# [MARK] zssmatrix::mult(const long&, const long&, const std::complex<double>&)"
198  << std::endl;
199 #endif//CPPL_VERBOSE
200 
201 #ifdef CPPL_DEBUG
202  if( i<0 || j<0 || M<=i || N<=j ){
203  std::cerr << "[ERROR] zssmatrix::mult(long&, long&, std::complex<double>&)" << std::endl
204  << "The required component is out of the matrix size."
205  << std::endl
206  << "Your input was (" << i << "," << j << ")." << std::endl;
207  exit(1);
208  }
209 #endif//CPPL_DEBUG
210 
212  for(long c=0; c<VOL; c++){
213  if(Indx[c]==i && Jndx[c]==j){ Array[c]*=v; return; }
214  }
215 
217  return;
218 }
219 
220 //=============================================================================
222 inline void zssmatrix::div(const long& i, const long& j, const std::complex<double>& v)
223 {
224 #ifdef CPPL_VERBOSE
225  std::cerr << "# [MARK] zssmatrix::div(const long&, const long&, const std::complex<double>&)"
226  << std::endl;
227 #endif//CPPL_VERBOSE
228 
229 #ifdef CPPL_DEBUG
230  if( i<0 || j<0 || M<=i || N<=j ){
231  std::cerr << "[ERROR] zssmatrix::div(long&, long&, std::complex<double>&)" << std::endl
232  << "The required component is out of the matrix size."
233  << std::endl
234  << "Your input was (" << i << "," << j << ")." << std::endl;
235  exit(1);
236  }
237 #endif//CPPL_DEBUG
238 
240  for(long c=0; c<VOL; c++){
241  if(Indx[c]==i && Jndx[c]==j){ Array[c]/=v; return; }
242  }
243 
245  return;
246 }
247 
251 
252 //=============================================================================
254 inline void zssmatrix::del(const long& i, const long& j)
255 {
256 #ifdef CPPL_VERBOSE
257  std::cerr << "# [MARK] zssmatrix::del(const long&, const long&)"
258  << std::endl;
259 #endif//CPPL_VERBOSE
260 
261 #ifdef CPPL_DEBUG
262  if( i<0 || j<0 || M<=i || N<=j ){
263  std::cerr << "[ERROR] zssmatrix::del(long&, long&, std::complex<double>&)" << std::endl
264  << "The required component is out of the matrix size."
265  << std::endl
266  << "Your input was (" << i << "," << j << ")." << std::endl;
267  exit(1);
268  }
269 #endif//CPPL_DEBUG
270 
271  for(long c=0; c<VOL; c++){
272  if(Indx[c]==i && Jndx[c]==j){
273  VOL--;
274  Indx[c]=Indx[VOL]; Jndx[c]=Jndx[VOL]; Array[c]=Array[VOL];
275  return;
276  }
277  }
278 
279  std::cerr << "[WARNING] zssmatrix::del(long&, long&, std::complex<double>&) "
280  << "The required component was not listed. "
281  << "Your input was (" << i << "," << j << ")." << std::endl;
282 }
283 
284 //=============================================================================
286 inline void zssmatrix::fdel(const long& c)
287 {
288 #ifdef CPPL_VERBOSE
289  std::cerr << "# [MARK] zssmatrix::fdel(const long&)"
290  << std::endl;
291 #endif//CPPL_VERBOSE
292 
293 #ifdef CPPL_DEBUG
294  if( c<0 || c>VOL ){
295  std::cerr << "[ERROR] zssmatrix::fdel(const long&)" << std::endl
296  << "The required element is out of the matrix volume."
297  << std::endl
298  << "Your input was (" << c << ")." << std::endl;
299  exit(1);
300  }
301 
302  std::cerr << "# [NOTE] zssmatrix::fdel(const long&) "
303  << "The (" << Indx[c] << "," << Jndx[c]
304  << ") component is going to be deleted." << std::endl;
305 #endif//CPPL_DEBUG
306 
307  VOL--;
308  Indx[c]=Indx[VOL]; Jndx[c]=Jndx[VOL]; Array[c]=Array[VOL];
309 }
310 
314 
315 //=============================================================================
316 inline std::ostream& operator<<(std::ostream& s, const zssmatrix& mat)
317 {
318 #ifdef CPPL_VERBOSE
319  std::cerr << "# [MARK] operator<<(std::ostream&, const zssmatrix&)"
320  << std::endl;
321 #endif//CPPL_VERBOSE
322 
323  long c;
324  for(long i=0; i<mat.M; i++){
325  for(long j=0; j<mat.N; j++){
326  c=0;
327  while(c<mat.VOL){
328  if(mat.Indx[c]==i && mat.Jndx[c]==j){ break; }
329  c++;
330  }
331  if(c!=mat.VOL){ s << mat.Array[c] << " "; }
332  else{ s << "x "; }
333  }
334  s << std::endl;
335  }
336 
337  return s;
338 }
339 
343 
344 //=============================================================================
345 inline void zssmatrix::write(const char* filename) const
346 {
347 #ifdef CPPL_VERBOSE
348  std::cerr << "# [MARK] zssmatrix::write(const char*) const"
349  << std::endl;
350 #endif//CPPL_VERBOSE
351 
352  std::ofstream s(filename, std::ios::trunc);
353 
354  s << "zssmatrix" << " " << M << " " << N << " " << CAP << std::endl;
355  for(long c=0; c<VOL; c++){
356  s << Indx[c] << " " << Jndx[c] << " " << Array[c] << std::endl;
357  }
358  s.close();
359 }
360 
361 //=============================================================================
362 inline void zssmatrix::read(const char *filename)
363 {
364 #ifdef CPPL_VERBOSE
365  std::cerr << "# [MARK] zssmatrix::read(const char*)"
366  << std::endl;
367 #endif//CPPL_VERBOSE
368 
369  std::ifstream s( filename );
370  if(!s){
371  std::cerr << "[ERROR] zssmatrix::read(const char*) " << std::endl
372  << "The file \"" << filename << "\" can not be opened."
373  << std::endl;
374  exit(1);
375  }
376 
377  std::string id;
378  s >> id;
379  if( id != "zssmatrix" ){
380  std::cerr << "[ERROR] zssmatrix::read(const char*) " << std::endl
381  << "The type name of the file \"" << filename
382  << "\" is not zssmatrix." << std::endl
383  << "Its type name was " << id << " ." << std::endl;
384  exit(1);
385  }
386 
387  s >> M >> N >> CAP;
388  resize(M, N, CAP);
389 
390  long pos, tmp;
391  while(!s.eof() && VOL<CAP){
392  s >> Indx[VOL] >> Jndx[VOL] >> Array[VOL];
393  pos =s.tellg(); s >> tmp; s.seekg(pos);
394  VOL++;
395  }
396 
397  if(!s.eof()){
398  std::cerr << "[ERROR] zssmatrix::read(const char*) " << std::endl
399  << "There is something is wrong with the file \""
400  << filename << " ." << std::endl
401  << "Most likely, there are too many data components "
402  << "over the context." << std::endl;
403  exit(1);
404  }
405  s.close();
406 }
void fdel(const long &)
Definition: zssmatrix-io.hpp:286
void fput(const long &, const long &, const std::complex< double > &)
Definition: zssmatrix-io.hpp:75
void expand(const long &)
Definition: zssmatrix-misc.hpp:136
void add(const long &, const long &, const std::complex< double > &)
Definition: zssmatrix-io.hpp:120
void mult(const long &, const long &, const std::complex< double > &)
Definition: zssmatrix-io.hpp:194
void sub(const long &, const long &, const std::complex< double > &)
Definition: zssmatrix-io.hpp:157
std::complex< double > operator()(const long &, const long &) const
Definition: zssmatrix-io.hpp:3
void resize(const long &, const long &, const long &)
Definition: zssmatrix-misc.hpp:104
void div(const long &, const long &, const std::complex< double > &)
Definition: zssmatrix-io.hpp:222
Complex Double-precision Sparse Matrix Class.
Definition: zssmatrix.hpp:3
void put(const long &, const long &, const std::complex< double > &)
Definition: zssmatrix-io.hpp:38
void del(const long &, const long &)
Definition: zssmatrix-io.hpp:254
std::complex< double > * Array
1D Array to store vector data
Definition: _zrovector.hpp:8