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