CPPLapack
 All Classes Files Functions Variables Friends Pages
dssmatrix-misc.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! clear all the matrix data and set the sizes 0 */
3 inline void dssmatrix::clear()
4 {CPPL_VERBOSE_REPORT;
5  n =0;
6  data.clear();
7  line.clear();
8 }
9 
10 //=============================================================================
11 /*! change the matrix into a zero matrix */
13 {CPPL_VERBOSE_REPORT;
14  data.resize(0);
15  for(CPPL_INT i=0; i<n; i++){ line[i].resize(0); }
16  return *this;
17 }
18 
19 //=============================================================================
20 /*! change the matrix into an identity matrix */
22 {CPPL_VERBOSE_REPORT;
23  zero();
24  for(CPPL_INT i=0; i<n; i++){
25  put(i,i,1.);
26  }
27  return *this;
28 }
29 
30 //=============================================================================
31 /*! change sign(+/-) of the matrix */
32 inline void dssmatrix::chsign()
33 {CPPL_VERBOSE_REPORT;
34  const std::vector<dcomponent>::iterator data_end =data.end();
35  for(std::vector<dcomponent>::iterator it=data.begin(); it!=data_end; it++){
36  it->v =-it->v;
37  }
38 }
39 
40 //=============================================================================
41 /*! make a deep copy of the matrix */
42 inline void dssmatrix::copy(const dssmatrix& mat)
43 {CPPL_VERBOSE_REPORT;
44  n =mat.n;
45  data =mat.data;
46  line =mat.line;
47 }
48 
49 //=============================================================================
50 /*! make a shallow copy of the matrix\n
51  This function is not designed to be used in project codes. */
52 inline void dssmatrix::shallow_copy(const _dssmatrix& mat)
53 {CPPL_VERBOSE_REPORT;
54  data.clear();
55  line.clear();
56 
57  n =mat.n;
58  data.swap(mat.data);
59  line.swap(mat.line);
60 
61  mat.nullify();
62 }
63 
64 //=============================================================================
65 /*! resize the matrix */
66 inline dssmatrix& dssmatrix::resize(const CPPL_INT& _n, const CPPL_INT _c, const CPPL_INT _l)
67 {CPPL_VERBOSE_REPORT;
68 #ifdef CPPL_DEBUG
69  if( _n<0 || _c<0 || _l<0 ){
70  ERROR_REPORT;
71  std::cerr << "Matrix sizes, the length of arrays, and line size must be positive integers. " << std::endl
72  << "Your input was (" << _n << "," << _c << "," << _l << ")." << std::endl;
73  exit(1);
74  }
75 #endif//CPPL_DEBUG
76 
77  n =_n;
78  data.resize(0);
79  data.reserve(_c);
80  line.resize(n);
81  for(CPPL_INT i=0; i<n; i++){
82  line[i].resize(0);
83  line[i].reserve(_l);
84  }
85 
86  return *this;
87 }
88 
89 //=============================================================================
90 /*! stretch the matrix size */
91 inline void dssmatrix::stretch(const CPPL_INT& dn)
92 {CPPL_VERBOSE_REPORT;
93 #ifdef CPPL_DEBUG
94  if( n+dn<0 ){
95  ERROR_REPORT;
96  std::cerr << "The new matrix size must be larger than zero." << std::endl
97  << "Your input was (" << dn << ")." << std::endl;
98  exit(1);
99  }
100 #endif//CPPL_DEBUG
101 
102  //////// zero ////////
103  if(dn==0){ return; }
104 
105  //////// non-zero ////////
106  n +=dn;
107 
108  if(dn<0){
109  //// delete components over the new size ////
110  const std::vector<dcomponent>::reverse_iterator data_rend =data.rend();
111  for(std::vector<dcomponent>::reverse_iterator it=data.rbegin(); it!=data_rend; it++){
112  if( it->i>=n ){ del( CPPL_INT(data_rend-it-1) ); }
113  }
114  //// shrink line ////
115  for(CPPL_INT i=0; i<-dn; i++){
116  line.pop_back();
117  }
118  }
119  else{//dn>0
120  //// expand line ////
121  for(CPPL_INT i=0; i<dn; i++){
122  line.push_back( std::vector<CPPL_INT>(0) );
123  }
124  }
125 }
126 
127 //=============================================================================
128 /*! check if the component is listed */
129 inline bool dssmatrix::isListed(const CPPL_INT& i, const CPPL_INT& j) const
130 {CPPL_VERBOSE_REPORT;
131 #ifdef CPPL_DEBUG
132  if( i<0 || j<0 || n<=i || n<=j ){
133  ERROR_REPORT;
134  std::cerr << "The required component is out of the matrix size." << std::endl
135  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << n << "x" << n << "." << std::endl;
136  exit(1);
137  }
138 #endif//CPPL_DEBUG
139 
140  const CPPL_INT ii(std::max(i,j)), jj(std::min(i,j));
141 
142  const std::vector<CPPL_INT>::const_iterator line_ii_end =line[ii].end();
143  for(std::vector<CPPL_INT>::const_iterator p=line[ii].begin(); p!=line_ii_end; p++){
144  if(data[*p].i==ii && data[*p].j==jj){ return 1; }
145  }
146 
147  return 0;
148 }
149 
150 //=============================================================================
151 /*! return the element number of the component */
152 inline CPPL_INT dssmatrix::number(const CPPL_INT& i, const CPPL_INT& j) const
153 {CPPL_VERBOSE_REPORT;
154 #ifdef CPPL_DEBUG
155  if( i<0 || j<0 || n<=i || n<=j ){
156  ERROR_REPORT;
157  std::cerr << "The required component is out of the matrix size." << std::endl
158  << "Your input is (" << i << "," << j << "), whereas the matrix size is " << n << "x" << n << "." << std::endl;
159  exit(1);
160  }
161 #endif//CPPL_DEBUG
162 
163  const CPPL_INT ii(std::max(i,j)), jj(std::min(i,j));
164 
165  const std::vector<CPPL_INT>::const_iterator line_ii_end =line[ii].end();
166  for(std::vector<CPPL_INT>::const_iterator p=line[ii].begin(); p!=line_ii_end; p++){
167  if(data[*p].i==ii && data[*p].j==jj){ return *p; }
168  }
169 
170  return -1;
171 }
172 
173 ///////////////////////////////////////////////////////////////////////////////
174 ///////////////////////////////////////////////////////////////////////////////
175 ///////////////////////////////////////////////////////////////////////////////
176 
177 //=============================================================================
178 /*! get row of the matrix */
179 inline _drovector dssmatrix::row(const CPPL_INT& _m) const
180 {CPPL_VERBOSE_REPORT;
181 #ifdef CPPL_DEBUG
182  if( _m<0 || _m>m ){
183  ERROR_REPORT;
184  std::cerr << "Input row number must be between 0 and " << m << "." << std::endl
185  << "Your input was " << _m << "." << std::endl;
186  exit(1);
187  }
188 #endif//CPPL_DEBUG
189 
190  drovector vec(n);
191  vec.zero();
192 
193  const std::vector<CPPL_INT>::const_iterator line__m_end =line[_m].end();
194  for(std::vector<CPPL_INT>::const_iterator p=line[_m].begin(); p!=line__m_end; p++){
195  if(data[*p].i==_m){
196  vec(data[*p].j) =data[*p].v;
197  }
198  else{
199  vec(data[*p].i) =data[*p].v;
200  }
201  }
202  return _(vec);
203 }
204 
205 //=============================================================================
206 /*! get column of the matrix */
207 inline _dcovector dssmatrix::col(const CPPL_INT& _n) const
208 {CPPL_VERBOSE_REPORT;
209 #ifdef CPPL_DEBUG
210  if( _n<0 || _n>n ){
211  ERROR_REPORT;
212  std::cerr << "Input row number must be between 0 and " << n << "." << std::endl
213  << "Your input was " << _n << "." << std::endl;
214  exit(1);
215  }
216 #endif//CPPL_DEBUG
217 
218  dcovector vec(m);
219  vec.zero();
220 
221  const std::vector<CPPL_INT>::const_iterator line__n_end =line[_n].end();
222  for(std::vector<CPPL_INT>::const_iterator p=line[_n].begin(); p!=line__n_end; p++){
223  if(data[*p].i==_n){
224  vec(data[*p].j) =data[*p].v;
225  }
226  else{
227  vec(data[*p].i) =data[*p].v;
228  }
229  }
230  return _(vec);
231 }
232 
233 ///////////////////////////////////////////////////////////////////////////////
234 ///////////////////////////////////////////////////////////////////////////////
235 ///////////////////////////////////////////////////////////////////////////////
236 
237 //=============================================================================
238 /*! erase components less than DBL_MIN */
239 inline void dssmatrix::diet(const double eps)
240 {CPPL_VERBOSE_REPORT;
241  const std::vector<dcomponent>::reverse_iterator data_rend =data.rend();
242  for(std::vector<dcomponent>::reverse_iterator it=data.rbegin(); it!=data_rend; it++){
243  if( fabs(it->v)<eps ){ del( CPPL_INT(data_rend-it-1) ); }
244  }
245 }
246 
247 //=============================================================================
248 /*! reorder components so that all diagonal componets are placed in front */
249 /*
250 inline CPPL_INT dssmatrix::diag_front()
251 {CPPL_VERBOSE_REPORT;
252  //////// set initial dsize ////////
253  CPPL_INT dsize(0);
254  const std::vector<dcomponent>::iterator data_end =data.end();
255  for(std::vector<dcomponent>::iterator it=data.begin(); it!=data.end(); it++){
256  if(it->i==it->j){ dsize++; }
257  else{ break; }
258  }
259 
260  //////// swapping loop ////////
261  for(std::vector<dcomponent>::reverse_iterator it=data.rbegin(); it!=data.rend()-dsize; it++){
262  if(it->i==it->j){//is diag
263  CPPL_INT c(data.rend()-it-1);//current it's index
264  CPPL_INT i(data[dsize].i), j(data[dsize].j), k(it->i);
265  //// search (k,k) line ////
266  for(std::vector<CPPL_INT>::iterator p=line[k].begin(); p!=line[k].end(); p++){
267  if(CPPL_INT(data[*p].i)==k && CPPL_INT(data[*p].j)==k){ *p=dsize; }
268  }
269  //// search (i,j) line ////
270  for(std::vector<CPPL_INT>::iterator p=line[i].begin(); p!=line[i].end(); p++){
271  if(CPPL_INT(*p)==dsize){ *p=c; }
272  }
273  //// search (j,i) line ////
274  if(i!=j){
275  for(std::vector<CPPL_INT>::iterator p=line[j].begin(); p!=line[j].end(); p++){
276  if(CPPL_INT(*p)==dsize){ *p=c; }
277  }
278  }
279  else{//i==j
280  it--;
281  }
282  //// swap ////
283  std::swap(data[dsize],data[c]);
284  //// update ////
285  dsize++;
286  }
287  }
288 
289  return dsize;
290 }
291 */
292 
293 //=============================================================================
294 /*! reorder components */
295 inline void dssmatrix::reorder(const bool mode)
296 {CPPL_VERBOSE_REPORT;
297  //// sort data ////
298  if(mode==0){
299  std::sort(data.begin(), data.end(), dcomponent::ilt);
300  }
301  else{
302  std::sort(data.begin(), data.end(), dcomponent::jlt);
303  }
304  //// rebuild line ////
305  rebuild();
306 }
307 
308 //=============================================================================
309 /*! rebuild line */
310 inline void dssmatrix::rebuild()
311 {CPPL_VERBOSE_REPORT;
312  //// clear line ////
313  for(CPPL_INT i=0; i<n; i++){ line[i].resize(0); }
314 
315  //// build line ////
316  CPPL_INT c(0);
317  const std::vector<dcomponent>::iterator data_end =data.end();
318  for(std::vector<dcomponent>::iterator it=data.begin(); it!=data_end; it++){
319  line[it->i].push_back(c);
320  if( (it->i) != (it->j) ){
321  line[it->j].push_back(c);
322  }
323  c++;
324  }
325 }
326 
327 ///////////////////////////////////////////////////////////////////////////////
328 ///////////////////////////////////////////////////////////////////////////////
329 ///////////////////////////////////////////////////////////////////////////////
330 
331 //=============================================================================
332 /*! health checkup */
333 inline void dssmatrix::checkup()
334 {CPPL_VERBOSE_REPORT;
335  //////// write ////////
336  const std::vector<dcomponent>::const_iterator data_end =data.end();
337  for(std::vector<dcomponent>::const_iterator it=data.begin(); it!=data_end; it++){
338  std::cerr << "array[" << it-data.begin() << "] = (" << it->i << "," << it->j << ") = " << it->v << std::endl;
339  }
340  std::cerr << std::endl;
341 
342  for(CPPL_INT i=0; i<n; i++){
343  std::cerr << "line[" << i << "] =" << std::flush;
344  const size_t line_i_size =line[i].size();
345  for(size_t k=0; k<line_i_size; k++){
346  std::cerr << " " << line[i][k] << std::flush;
347  }
348  std::cerr << std::endl;
349  }
350  std::cerr << std::endl;
351 
352  //////// Elements ////////
353  for(std::vector<dcomponent>::const_iterator it=data.begin(); it!=data_end; it++){
354  //// m bound ////
355  if(it->i>=n){
356  ERROR_REPORT;
357  std::cerr << "The indx of the " << it-data.begin() << "th element is out of the matrix size." << std::endl
358  << "Its i index was " << it->i << "." << std::endl;
359  exit(1);
360  }
361 
362  //// n bound ////
363  if(it->j>=n){
364  ERROR_REPORT;
365  std::cerr << "The jndx of the " << it-data.begin() << "th element is out of the matrix size." << std::endl
366  << "Its j index was " << it->j << "." << std::endl;
367  exit(1);
368  }
369 
370  //// double-listed ////
371  for(std::vector<dcomponent>::const_iterator IT=it+1; IT!=data_end; IT++){
372  if( it->i==IT->i && it->j==IT->j ){
373  ERROR_REPORT;
374  std::cerr << "The (" << it->i << ", " << it->j << ") component is double-listed at the " << it-data.begin() << "th and the" << IT-data.begin() << "the elements."<< std::endl;
375  exit(1);
376  }
377  }
378  }
379 
380  //////// NOTE ////////
381  std::cerr << "# [NOTE]@dssmatrix::checkup(): This symmetric sparse matrix is fine." << std::endl;
382 }
383 
384 ///////////////////////////////////////////////////////////////////////////////
385 ///////////////////////////////////////////////////////////////////////////////
386 ///////////////////////////////////////////////////////////////////////////////
387 
388 //=============================================================================
389 /*! swap two matrices */
390 inline void swap(dssmatrix& A, dssmatrix& B)
391 {CPPL_VERBOSE_REPORT;
392  std::swap(A.n,B.n);
393  std::swap(A.data,B.data);
394  std::swap(A.line,B.line);
395 }
396 
397 //=============================================================================
398 /*! convert user object to smart-temporary object */
399 inline _dssmatrix _(dssmatrix& mat)
400 {CPPL_VERBOSE_REPORT;
401  _dssmatrix newmat;
402 
403  //////// shallow copy ////////
404  newmat.n =mat.n;
405  std::swap(newmat.data,mat.data);
406  std::swap(newmat.line,mat.line);
407 
408  //////// nullify ////////
409  mat.n =0;
410 
411  return newmat;
412 }
std::vector< dcomponent > data
matrix data
Definition: dssmatrix.hpp:11
_dssmatrix _(dssmatrix &mat)
_dcovector col(const CPPL_INT &) const
void copy(const dssmatrix &)
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)
std::vector< dcomponent > data
matrix data
Definition: _dssmatrix.hpp:12
dssmatrix & del(const CPPL_INT, const CPPL_INT)
dssmatrix & put(const CPPL_INT &, const CPPL_INT &, const double &)
bool isListed(const CPPL_INT &, const CPPL_INT &) const
(DO NOT USE) Smart-temporary Real Double-precision Symmetric Sparse Matrix Class
Definition: _dssmatrix.hpp:3
dcovector & zero()
_dgematrix i(const _dgbmatrix &mat)
void reorder(const bool=0)
static bool jlt(const dcomponent &, const dcomponent &)
Definition: dcomponent.hpp:47
static bool ilt(const dcomponent &, const dcomponent &)
Definition: dcomponent.hpp:40
dssmatrix & identity()
void shallow_copy(const _dssmatrix &)
void diet(const double=DBL_MIN)
Real Double-precision Row Vector Class.
Definition: drovector.hpp:3
_drovector row(const CPPL_INT &) const
drovector & zero()
(DO NOT USE) Smart-temporary Real Double-precision Row Vector Class
Definition: _drovector.hpp:3
CPPL_INT n
matrix column size
Definition: _dssmatrix.hpp:11
void swap(dssmatrix &A, dssmatrix &B)
void clear()
dssmatrix & zero()
Real Double-precision Column Vector Class.
Definition: dcovector.hpp:3
friend _dssmatrix _(dssmatrix &)
Real Double-precision Symmetric Sparse Matrix Class.
Definition: dssmatrix.hpp:3
CPPL_INT n
matrix column size
Definition: dssmatrix.hpp:10
CPPL_INT const & m
matrix row size
Definition: dssmatrix.hpp:9
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:13
(DO NOT USE) Smart-temporary Real Double-precision Column Vector Class
Definition: _dcovector.hpp:3
void nullify() const
void stretch(const CPPL_INT &)