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