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