CPPLapack
 All Classes Files Functions Variables Friends
dgsmatrix-misc.hpp
Go to the documentation of this file.
00001 //=============================================================================
00002 /*! clear all the matrix data and set the sizes 0 */
00003 inline void dgsmatrix::clear()
00004 {VERBOSE_REPORT;
00005   m =0;
00006   n =0;
00007   data.clear();
00008   rows.clear();
00009   cols.clear();
00010 }
00011 
00012 //=============================================================================
00013 /*! change the matrix into a zero matrix */
00014 inline dgsmatrix& dgsmatrix::zero()
00015 {VERBOSE_REPORT;
00016   data.resize(0);
00017   for(long i=0; i<m; i++){ rows[i].resize(0); }
00018   for(long j=0; j<n; j++){ cols[j].resize(0); }
00019   return *this;
00020 }
00021 
00022 //=============================================================================
00023 /*! change sign(+/-) of the matrix */
00024 inline void dgsmatrix::chsign()
00025 {VERBOSE_REPORT;
00026   for(std::vector<dcomponent>::iterator it=data.begin(); it!=data.end(); it++){
00027     it->v =-it->v;
00028   }
00029 }
00030 
00031 //=============================================================================
00032 /*! make a deep copy of the matrix */
00033 inline void dgsmatrix::copy(const dgsmatrix& mat)
00034 {VERBOSE_REPORT;
00035   m =mat.m;
00036   n =mat.n;
00037   data =mat.data;
00038   rows =mat.rows;
00039   cols =mat.cols;
00040 }
00041 
00042 //=============================================================================
00043 /*! make a shallow copy of the matrix\n
00044   This function is not designed to be used in project codes. */
00045 inline void dgsmatrix::shallow_copy(const _dgsmatrix& mat)
00046 {VERBOSE_REPORT;
00047   data.clear();
00048   rows.clear();
00049   cols.clear();
00050   
00051   m =mat.m;
00052   n =mat.n;
00053   data.swap(mat.data);
00054   rows.swap(mat.rows);
00055   cols.swap(mat.cols);
00056   
00057   mat.nullify();
00058 }
00059 
00060 //=============================================================================
00061 /*! resize the matrix */
00062 inline dgsmatrix& dgsmatrix::resize(const long& _m, const long& _n, const long _c, const long _l)
00063 {VERBOSE_REPORT;
00064 #ifdef  CPPL_DEBUG
00065   if( _m<0 || _n<0 || _c<0 ){
00066     ERROR_REPORT;
00067     std::cerr << "Matrix sizes and the length of arrays must be positive integers. " << std::endl
00068               << "Your input was (" << _m << "," << _n << "," << _c << "," << _l << ")." << std::endl;
00069     exit(1);
00070   }
00071 #endif//CPPL_DEBUG
00072   
00073   m =_m;
00074   n =_n;
00075   data.resize(0);
00076   data.reserve(_c);
00077   rows.resize(m);
00078   for(long i=0; i<m; i++){
00079     rows[i].resize(0);
00080     rows[i].reserve(_l);
00081   }
00082   cols.resize(n);
00083   for(long i=0; i<n; i++){
00084     cols[i].resize(0);
00085     cols[i].reserve(_l);
00086   }
00087   
00088   return *this;
00089 }
00090 
00091 //=============================================================================
00092 /*! stretch the matrix size */
00093 inline void dgsmatrix::stretch(const long& dm, const long& dn)
00094 {VERBOSE_REPORT;
00095 #ifdef  CPPL_DEBUG
00096   if( m+dm<0 || n+dn<0 ){
00097     ERROR_REPORT;
00098     std::cerr << "The new matrix size must be larger than zero. " << std::endl
00099               << "Your input was (" << dm << ", " << dn << ")." << std::endl;
00100     exit(1);
00101   }
00102 #endif//CPPL_DEBUG
00103   
00104   //////// zero ////////
00105   if(dm==0 && dn==0){ return; }
00106   
00107   //////// non-zero ////////
00108   m +=dm;
00109   n +=dn;
00110   
00111   //// for rows ////
00112   if(dm<0){
00113     //// delete components over the new size ////
00114     for(std::vector<dcomponent>::reverse_iterator it=data.rbegin(); it!=data.rend(); it++){
00115       if( long(it->i)>=m ){ del(data.rend()-it-1); }
00116     }
00117     //// shrink rows ////
00118     for(long i=0; i<-dm; i++){
00119       rows.pop_back();
00120     }
00121   }
00122   else{//dm>0
00123     //// expand rows ////
00124     for(long i=0; i<dm; i++){
00125       rows.push_back( std::vector<uint32_t>(0) );
00126     }
00127   }
00128   
00129   //// for cols ////
00130   if(dn<0){
00131     //// delete components over the new size ////
00132     for(std::vector<dcomponent>::reverse_iterator it=data.rbegin(); it!=data.rend(); it++){
00133       if( long(it->j)>=n ){ del(data.rend()-it-1); }
00134     }
00135     for(long j=0; j<-dn; j++){
00136       cols.pop_back();
00137     }
00138   }
00139   else{//dn>0
00140     //// expand cols ////
00141     for(long j=0; j<dn; j++){
00142       cols.push_back( std::vector<uint32_t>(0) );
00143     }
00144   }
00145 }
00146 
00147 //=============================================================================
00148 /*! check if the component is listed */
00149 inline bool dgsmatrix::isListed(const long& i, const long& j) const
00150 {VERBOSE_REPORT;
00151 #ifdef  CPPL_DEBUG
00152   if( i<0 || j<0 || m<=i || n<=j ){
00153     ERROR_REPORT;
00154     std::cerr << "The required component is out of the matrix size." << std::endl
00155               << "Your input was (" << i << "," << j << ")." << std::endl;
00156     exit(1);
00157   }
00158 #endif//CPPL_DEBUG
00159   
00160   for(std::vector<uint32_t>::const_iterator p=rows[i].begin(); p!=rows[i].end(); p++){
00161     if(long(data[*p].j)==j){ return 1; }
00162   }
00163   
00164   return 0;
00165 }
00166 
00167 
00168 //=============================================================================
00169 /*! return the element number of the component */
00170 inline long dgsmatrix::number(const long& i, const long& j)
00171 {VERBOSE_REPORT;
00172 #ifdef  CPPL_DEBUG
00173   if( i<0 || j<0 || m<=i || n<=j ){
00174     ERROR_REPORT;
00175     std::cerr << "The required component is out of the matrix size." << std::endl
00176               << "Your input was (" << i << "," << j << ")." << std::endl;
00177     exit(1);
00178   }
00179 #endif//CPPL_DEBUG
00180 
00181   for(std::vector<uint32_t>::iterator p=rows[i].begin(); p!=rows[i].end(); p++){
00182     if(long(data[*p].j)==j){ return *p; }
00183   }
00184   
00185   return -1;
00186 }
00187 
00188 ///////////////////////////////////////////////////////////////////////////////
00189 ///////////////////////////////////////////////////////////////////////////////
00190 ///////////////////////////////////////////////////////////////////////////////
00191 
00192 //=============================================================================
00193 /*! get row of the matrix */
00194 inline _drovector dgsmatrix::row(const long& _m) const
00195 {VERBOSE_REPORT;
00196 #ifdef  CPPL_DEBUG
00197   if( _m<0 || _m>m ){
00198     ERROR_REPORT;
00199     std::cerr << "Input row number must be between 0 and " << m << "." << std::endl
00200               << "Your input was " << _m << "." << std::endl;
00201     exit(1);
00202   }
00203 #endif//CPPL_DEBUG
00204   
00205   drovector vec(n);
00206   vec.zero();
00207   for(std::vector<uint32_t>::const_iterator p=rows[_m].begin(); p!=rows[_m].end(); p++){
00208     vec(data[*p].j) =data[*p].v;
00209   }
00210   return _(vec);
00211 }
00212 
00213 //=============================================================================
00214 /*! get column of the matrix */
00215 inline _dcovector dgsmatrix::col(const long& _n) const
00216 {VERBOSE_REPORT;
00217 #ifdef  CPPL_DEBUG
00218   if( _n<0 || _n>n ){
00219     ERROR_REPORT;
00220     std::cerr << "Input row number must be between 0 and " << n << "." << std::endl
00221               << "Your input was " << _n << "." << std::endl;
00222     exit(1);
00223   }
00224 #endif//CPPL_DEBUG
00225   
00226   dcovector vec(m);
00227   vec.zero();
00228   for(std::vector<uint32_t>::const_iterator p=cols[_n].begin(); p!=cols[_n].end(); p++){
00229     vec(data[*p].i) =data[*p].v;
00230   }
00231   return _(vec);
00232 }
00233 
00234 //=============================================================================
00235 /*! erase components less than DBL_MIN */
00236 inline void dgsmatrix::diet(const double eps)
00237 {VERBOSE_REPORT;
00238   for(std::vector<dcomponent>::reverse_iterator it=data.rbegin(); it!=data.rend(); it++){
00239     if( fabs(it->v)<eps ){ del(data.rend()-it-1); }
00240   }
00241 }
00242 
00243 ///////////////////////////////////////////////////////////////////////////////
00244 ///////////////////////////////////////////////////////////////////////////////
00245 ///////////////////////////////////////////////////////////////////////////////
00246 
00247 //=============================================================================
00248 /*! health checkup */
00249 inline void dgsmatrix::checkup()
00250 {VERBOSE_REPORT;
00251   //////// write ////////
00252   for(std::vector<dcomponent>::const_iterator it=data.begin(); it!=data.end(); it++){
00253     std::cerr << "array[" << it-data.begin() << "] = (" << it->i << "," << it->j << ") = " << it->v << std::endl;
00254   }
00255   std::cerr << std::endl;
00256   
00257   for(long i=0; i<m; i++){
00258     std::cerr << "rows[" << i << "] =" << std::flush;
00259     for(unsigned long k=0; k<rows[i].size(); k++){
00260       std::cerr << " " << rows[i][k] << std::flush;
00261     }
00262     std::cerr << std::endl;
00263   }
00264   std::cerr << std::endl;
00265   
00266   for(long j=0; j<n; j++){
00267     std::cerr << "cols[" << j << "] =" << std::flush;
00268     for(unsigned long k=0; k<cols[j].size(); k++){
00269       std::cerr << " " << cols[j][k] << std::flush;
00270     }
00271     std::cerr << std::endl;
00272   }
00273   
00274   //////// Elements ////////
00275   for(std::vector<dcomponent>::const_iterator it=data.begin(); it!=data.end(); it++){
00276     //// m bound ////
00277     if(long(it->i)>=m){
00278       ERROR_REPORT;
00279       std::cerr << "The indx of the " << it-data.begin() << "th element is out of the matrix size." << std::endl
00280                 << "Its i index was " << it->i << "." << std::endl;
00281       exit(1);
00282     }
00283     
00284     //// n bound ////
00285     if(long(it->j)>=n){
00286       ERROR_REPORT;
00287       std::cerr << "The indx of the " << it-data.begin() << "th element is out of the matrix size." << std::endl
00288                 << "Its j index was " << it->j << "." << std::endl;
00289       exit(1);
00290     }
00291     
00292     //// double-listed ////
00293     for(std::vector<dcomponent>::const_iterator IT=it+1; IT!=data.end(); IT++){
00294       if( it->i==IT->i && it->j==IT->j ){
00295         ERROR_REPORT;
00296         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;
00297         exit(1);
00298       }
00299     }
00300   }
00301   
00302   //////// ijc consistence ////////
00303   
00304   
00305   //////// NOTE ////////
00306   std::cerr << "# [NOTE]@dgsmatrix::checkup(): This sparse matrix is fine." << std::endl;
00307 }
00308 
00309 ///////////////////////////////////////////////////////////////////////////////
00310 ///////////////////////////////////////////////////////////////////////////////
00311 ///////////////////////////////////////////////////////////////////////////////
00312 
00313 //=============================================================================
00314 /*! swap two matrices */
00315 inline void swap(dgsmatrix& A, dgsmatrix& B)
00316 {VERBOSE_REPORT;
00317   std::swap(A.n,B.n);
00318   std::swap(A.m,B.m);
00319   std::swap(A.data,B.data);
00320   std::swap(A.rows,B.rows);
00321   std::swap(A.cols,B.cols);
00322 }
00323 
00324 //=============================================================================
00325 /*! convert user object to smart-temporary object */
00326 inline _dgsmatrix _(dgsmatrix& mat)
00327 {VERBOSE_REPORT;
00328   _dgsmatrix newmat;
00329   
00330   //////// shallow copy ////////
00331   newmat.n =mat.n;
00332   newmat.m =mat.m;
00333   std::swap(newmat.data,mat.data);
00334   std::swap(newmat.rows,mat.rows);
00335   std::swap(newmat.cols,mat.cols);
00336   
00337   //////// nullify ////////
00338   mat.m =0;
00339   mat.n =0;
00340   
00341   return newmat;
00342 }
00343 
 All Classes Files Functions Variables Friends