CPPLapack
 All Classes Files Functions Variables Friends Pages
zhsmatrix-io.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! operator() for const object */
3 inline comple zhsmatrix::operator()(const CPPL_INT& i, const CPPL_INT& j) const
4 {CPPL_VERBOSE_REPORT;
5 #ifdef CPPL_DEBUG
6  if( i<0 || j<0 || n<=i || n<=j ){
7  ERROR_REPORT;
8  std::cerr << "The required component is out of the matrix size." << std::endl
9  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << n << "x" << n << "." << std::endl;
10  exit(1);
11  }
12 #endif//CPPL_DEBUG
13 
14  //// search (i,j) component ////
15  const CPPL_INT ii(std::max(i,j)), jj(std::min(i,j));
16 
17  const std::vector<CPPL_INT>::const_iterator line_ii_end =line[ii].end();
18  for(std::vector<CPPL_INT>::const_iterator p=line[ii].begin(); p!=line_ii_end; p++){
19  if(data[*p].i==ii && data[*p].j==jj){
20  if( i>j ){ return data[*p].v; }//ii=i
21  else{ return std::conj(data[*p].v); }//ii=j
22  }
23  }
24 
25  //// (i,j) component was not found ////
26  return comple(0.0,0.0);
27 }
28 
29 //=============================================================================
30 /*! operator() */
31 inline zhecomplex zhsmatrix::operator()(const CPPL_INT& i, const CPPL_INT& j)
32 {CPPL_VERBOSE_REPORT;
33 #ifdef CPPL_DEBUG
34  if( i<0 || j<0 || n<=i || n<=j ){
35  ERROR_REPORT;
36  std::cerr << "The required component is out of the matrix size." << std::endl
37  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << n << "x" << n << "." << std::endl;
38  exit(1);
39  }
40 #endif//CPPL_DEBUG
41 
42  //////// search (i,j) component ////////
43  const CPPL_INT ii(std::max(i,j)), jj(std::min(i,j));
44 
45  const std::vector<CPPL_INT>::const_iterator line_ii_end =line[ii].end();
46  for(std::vector<CPPL_INT>::const_iterator p=line[ii].begin(); p!=line_ii_end; p++){
47  if(data[*p].i==ii && data[*p].j==jj){
48  return zhecomplex(i,j, data[*p].v);
49  }
50  }
51 
52  //////// (i,j) component not found ////////
53  if(i>j){
54  line[i].push_back(CPPL_INT(data.size()));
55  line[j].push_back(CPPL_INT(data.size()));
56  data.push_back(zcomponent(i,j,comple(0.,0.)));
57  }
58  else if(i<j){
59  line[i].push_back(CPPL_INT(data.size()));
60  line[j].push_back(CPPL_INT(data.size()));
61  data.push_back(zcomponent(j,i,comple(0.,0.)));
62  }
63  else{//i==j
64  line[i].push_back(CPPL_INT(data.size()));
65  data.push_back(zcomponent(i,i,comple(0.,0.)));
66  }
67 
68  return zhecomplex(i,j, data.back().v);
69 }
70 
71 ///////////////////////////////////////////////////////////////////////////////
72 ///////////////////////////////////////////////////////////////////////////////
73 ///////////////////////////////////////////////////////////////////////////////
74 
75 //=============================================================================
76 /*! put value with volume cheack without isListed check */
77 inline zhsmatrix& zhsmatrix::put(const CPPL_INT& i, const CPPL_INT& j, const comple& v)
78 {CPPL_VERBOSE_REPORT;
79 #ifdef CPPL_DEBUG
80  if( i<0 || j<0 || n<=i || n<=j ){
81  ERROR_REPORT;
82  std::cerr << "The required component is out of the matrix size." << std::endl
83  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << n << "x" << n << "." << std::endl;
84  exit(1);
85  }
86 
87  if( isListed(i,j) ){
88  ERROR_REPORT;
89  std::cerr << "The required component is already listed." << std::endl
90  << "Your input was (" << i << "," << j << ")." << std::endl;
91  exit(1);
92  }
93 #endif//CPPL_DEBUG
94 
95  //// push ////
96  if(i==j){
97  line[i].push_back(CPPL_INT(data.size()));
98  data.push_back(zcomponent(i,j,v));
99  }
100  else if(i>j){
101  line[i].push_back(CPPL_INT(data.size()));
102  line[j].push_back(CPPL_INT(data.size()));
103  data.push_back(zcomponent(i,j,v));
104  }
105  else{//i<j
106  line[i].push_back(CPPL_INT(data.size()));
107  line[j].push_back(CPPL_INT(data.size()));
108  data.push_back(zcomponent(j,i,conj(v)));
109  }
110 
111  return *this;
112 }
113 
114 ///////////////////////////////////////////////////////////////////////////////
115 ///////////////////////////////////////////////////////////////////////////////
116 ///////////////////////////////////////////////////////////////////////////////
117 
118 //=============================================================================
119 /*! delete the entry of a component */
120 inline zhsmatrix& zhsmatrix::del(const CPPL_INT i, const CPPL_INT j)
121 {CPPL_VERBOSE_REPORT;
122 #ifdef CPPL_DEBUG
123  if( i<0 || j<0 || n<=i || n<=j ){
124  ERROR_REPORT;
125  std::cerr << "The required component is out of the matrix size." << std::endl
126  << "Your input was (" << i << "," << j << ")." << std::endl;
127  exit(1);
128  }
129 #endif//CPPL_DEBUG
130 
131  const CPPL_INT ii(std::max(i,j)), jj(std::min(i,j));
132 
133  //////// search (i,j) component ////////
134  const std::vector<CPPL_INT>::iterator line_ii_end =line[ii].end();
135  for(std::vector<CPPL_INT>::iterator p=line[ii].begin(); p!=line_ii_end; p++){
136  if( data[*p].i==ii && data[*p].j==jj ){//exists
137  //// save position ////
138  CPPL_INT c =*p;
139  CPPL_INT C =CPPL_INT(data.size()-1);
140 
141  //// data translation ////
142  data[c]=data.back();
143  data.pop_back();
144 
145  //// remove from List ////
146  line[ii].erase(p);
147  if(i!=j){//off-diagonal
148  const std::vector<CPPL_INT>::iterator line_jj_end =line[jj].end();
149  for(std::vector<CPPL_INT>::iterator pj=line[jj].begin(); pj!=line_jj_end; pj++){
150  if(*pj==c){ line[jj].erase(pj); break; }
151  }
152  }
153 
154  //// modify the entry of translated component ////
155  CPPL_INT I(data[c].i), J(data[c].j);
156  const std::vector<CPPL_INT>::iterator line_I_end =line[I].end();
157  for(std::vector<CPPL_INT>::iterator q=line[I].begin(); q!=line_I_end; q++){
158  if(*q==C){ *q=c; break; }
159  }
160  if(I!=J){//off-diagonal
161  const std::vector<CPPL_INT>::iterator line_J_end =line[J].end();
162  for(std::vector<CPPL_INT>::iterator q=line[J].begin(); q!=line_J_end; q++){
163  if(*q==C){ *q=c; break; }
164  }
165  }
166  return *this;
167  }
168  }
169 
170 #ifdef CPPL_DEBUG
171  std::cerr << "# [NOTE]@zhsmatrix::del(CPPL_INT&, CPPL_INT&): The required component was not listed. Your input was (" << i << "," << j << ")." << std::endl;
172 #endif//CPPL_DEBUG
173 
174  return *this;
175 }
176 
177 //=============================================================================
178 /*! delete the entry of an element */
179 inline zhsmatrix& zhsmatrix::del(const CPPL_INT c)
180 {CPPL_VERBOSE_REPORT;
181 #ifdef CPPL_DEBUG
182  if( c<0 || c>=CPPL_INT(data.size()) ){
183  ERROR_REPORT;
184  std::cerr << "The required element is out of the matrix volume." << std::endl
185  << "Your input was (" << c << ")." << std::endl;
186  exit(1);
187  }
188 #endif//CPPL_DEBUG
189 
190  if( c==CPPL_INT(data.size()-1) ){//if c is the last element
191  CPPL_INT i(data[c].i), j(data[c].j);
192  const std::vector<CPPL_INT>::iterator line_i_end =line[i].end();
193  for(std::vector<CPPL_INT>::iterator q=line[i].begin(); q!=line_i_end; q++){
194  if( *q==c ){ line[i].erase(q); break; }
195  }
196  if(i!=j){//off-diagonal
197  const std::vector<CPPL_INT>::iterator line_j_end =line[j].end();
198  for(std::vector<CPPL_INT>::iterator q=line[j].begin(); q!=line_j_end; q++){
199  if( *q==c ){ line[j].erase(q); break; }
200  }
201  }
202  data.pop_back();
203  }
204 
205  else{//c is NOT the last element
206  //// data translation ////
207  CPPL_INT C =CPPL_INT(data.size()-1);
208  CPPL_INT i(data[c].i), j(data[c].j), I(data.back().i), J(data.back().j);
209  data[c]=data.back();
210  //std::cerr << "c=" << c << " i=" << i << " j=" << j << " C=" << vol << " I=" << I << " J=" << J << std::endl;
211  //// remove entry of component ////
212  const std::vector<CPPL_INT>::iterator line_i_end =line[i].end();
213  for(std::vector<CPPL_INT>::iterator q=line[i].begin(); q!=line_i_end; q++){
214  if( *q==c ){ line[i].erase(q); break; }
215  }
216  if(i!=j){//off-diagonal
217  const std::vector<CPPL_INT>::iterator line_j_end =line[j].end();
218  for(std::vector<CPPL_INT>::iterator q=line[j].begin(); q!=line_j_end; q++){
219  if( *q==c ){ line[j].erase(q); break; }
220  }
221  }
222  //// modify the entry of translated component ////
223  const std::vector<CPPL_INT>::iterator line_I_end =line[I].end();
224  for(std::vector<CPPL_INT>::iterator q=line[I].begin(); q!=line_I_end; q++){
225  if(*q==C){ *q=c; break; }
226  }
227  if(I!=J){//off-diagonal
228  const std::vector<CPPL_INT>::iterator line_J_end =line[J].end();
229  for(std::vector<CPPL_INT>::iterator q=line[J].begin(); q!=line_J_end; q++){
230  if(*q==C){ *q=c; break; }
231  }
232  }
233  //// pop_back ////
234  data.pop_back();
235  }
236 
237  return *this;
238 }
239 
240 ///////////////////////////////////////////////////////////////////////////////
241 ///////////////////////////////////////////////////////////////////////////////
242 ///////////////////////////////////////////////////////////////////////////////
243 
244 //=============================================================================
245 inline std::ostream& operator<<(std::ostream& s, const zhsmatrix& mat)
246 {CPPL_VERBOSE_REPORT;
247  for(CPPL_INT i=0; i<mat.n; i++){
248  for(CPPL_INT j=0; j<=i; j++){
249  CPPL_INT c =mat.number(i,j);
250  if(c<0){
251  s << " x ";
252  }
253  else{
254  s << " " << mat.data[c].v << " ";
255  }
256  }
257  for(CPPL_INT j=i+1; j<mat.n; j++){
258  CPPL_INT c =mat.number(i,j);
259  if(c<0){
260  s << "{x}";
261  }
262  else{
263  s << "{" << std::conj(mat.data[c].v) << "}";
264  }
265  }
266  s << std::endl;
267  }
268 
269  return s;
270 }
271 
272 ///////////////////////////////////////////////////////////////////////////////
273 ///////////////////////////////////////////////////////////////////////////////
274 ///////////////////////////////////////////////////////////////////////////////
275 
276 //=============================================================================
277 inline void zhsmatrix::write(const char* filename) const
278 {CPPL_VERBOSE_REPORT;
279  std::ofstream ofs(filename, std::ios::trunc);
280  ofs.setf(std::cout.flags());
281  ofs.precision(std::cout.precision());
282  ofs.width(std::cout.width());
283  ofs.fill(std::cout.fill());
284 
285  ofs << "#zhsmatrix " << n << " " << data.size() << std::endl;
286 
287  const std::vector<zcomponent>::const_iterator data_end =data.end();
288  for(std::vector<zcomponent>::const_iterator it=data.begin(); it!=data_end; it++){
289  ofs << it->i << " " << it->j << " " << it->v << std::endl;
290  }
291 
292  ofs.close();
293 }
294 
295 //=============================================================================
296 inline void zhsmatrix::read(const char* filename)
297 {CPPL_VERBOSE_REPORT;
298  std::ifstream s( filename );
299  if(!s){
300  ERROR_REPORT;
301  std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
302  exit(1);
303  }
304 
305  std::string id;
306  s >> id;
307  if( id != "zhsmatrix" && id != "#zhsmatrix" ){
308  ERROR_REPORT;
309  std::cerr << "The type name of the file \"" << filename << "\" is not zhsmatrix." << std::endl
310  << "Its type name was " << id << " ." << std::endl;
311  exit(1);
312  }
313 
314  //////// n, size ////////
315  size_t size;
316  s >> n >> size;
317  resize(n);
318  data.reserve(size);
319 
320  //////// i, j, v ////////
321  CPPL_INT i, j;
322  comple v;
323  for(size_t k=0; k<size; k++){
324  s >> i >> j >> v;
325  put(i,j, v);
326  }
327 
328  //////// check ////////
329  s >> i;
330  if(!s.eof()){
331  ERROR_REPORT;
332  std::cerr << "There is something is wrong with the file \"" << filename << " ." << std::endl
333  << "Most likely, there are too many data components over the context." << std::endl;
334  exit(1);
335  }
336  s.close();
337 }
bool isListed(const CPPL_INT &, const CPPL_INT &) const
zhsmatrix & put(const CPPL_INT &, const CPPL_INT &, const comple &)
_dgematrix i(const _dgbmatrix &mat)
CPPL_INT number(const CPPL_INT &, const CPPL_INT &) const
CPPL_INT n
matrix column size
Definition: zhsmatrix.hpp:10
_zcovector conj(const _zcovector &vec)
void read(const char *)
std::vector< zcomponent > data
matrix data
Definition: zhsmatrix.hpp:11
comple operator()(const CPPL_INT &, const CPPL_INT &) const
Definition: zhsmatrix-io.hpp:3
Component Class for Complex Double-precision Sparse Matrix Classes.
Definition: zcomponent.hpp:3
std::vector< std::vector< CPPL_INT > > line
vector of vector to store the entry information of component for each row and column ...
Definition: zhsmatrix.hpp:12
std::ostream & operator<<(std::ostream &s, const zhsmatrix &mat)
(DO NOT USE) Complex-double Class for Hermitian matrices
Definition: zhecomplex.hpp:3
Complex Double-precision Hermitian Sparse Matrix Class.
Definition: zhsmatrix.hpp:3
zhsmatrix & resize(const CPPL_INT &, const CPPL_INT=0, const CPPL_INT=0)
void write(const char *) const
zhsmatrix & del(const CPPL_INT, const CPPL_INT)