CPPLapack
 All Classes Files Functions Variables Friends
zgsmatrix-misc.hpp
Go to the documentation of this file.
00001 //=============================================================================
00002 /*! clear all the matrix data and set the sizes 0 */
00003 inline void zgsmatrix::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 zgsmatrix& zgsmatrix::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 zgsmatrix::chsign()
00025 {VERBOSE_REPORT;
00026   for(std::vector<zcomponent>::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 zgsmatrix::copy(const zgsmatrix& 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 zgsmatrix::shallow_copy(const _zgsmatrix& 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 zgsmatrix& zgsmatrix::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 zgsmatrix::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<zcomponent>::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<zcomponent>::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 zgsmatrix::isListed(const long& i, const long& j)
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 /*! return the element number of the component */
00169 inline long zgsmatrix::number(const long& i, const long& j)
00170 {VERBOSE_REPORT;
00171 #ifdef  CPPL_DEBUG
00172   if( i<0 || j<0 || m<=i || n<=j ){
00173     ERROR_REPORT;
00174     std::cerr << "The required component is out of the matrix size." << std::endl
00175               << "Your input was (" << i << "," << j << ")." << std::endl;
00176     exit(1);
00177   }
00178 #endif//CPPL_DEBUG
00179   
00180   for(std::vector<uint32_t>::iterator p=rows[i].begin(); p!=rows[i].end(); p++){
00181     if( long(data[*p].j)==j ){ return *p; }
00182   }
00183   
00184   return -1;
00185 }
00186 
00187 //=============================================================================
00188 /*! erase components less than DBL_MIN */
00189 inline void zgsmatrix::diet(const double eps)
00190 {VERBOSE_REPORT;
00191   for(std::vector<zcomponent>::reverse_iterator it=data.rbegin(); it!=data.rend(); it++){
00192     if( fabs(it->v.real())<eps && fabs(it->v.imag())<eps ){ del(data.rend()-it-1); }
00193   }
00194 }
00195 
00196 //=============================================================================
00197 /*! health checkup */
00198 inline void zgsmatrix::checkup()
00199 {VERBOSE_REPORT;
00200   //////////////// data ////////////////
00201   //////// check i,j ////////
00202   for(std::vector<zcomponent>::const_iterator it=data.begin(); it!=data.end(); it++){
00203     if( long(it->i)>=m || long(it->j)>=n ){
00204       ERROR_REPORT;
00205       std::cerr << "A component, (" << it->i << ", " << it->j << "), is out of matrix size." << std::endl;
00206       exit(1);
00207     }
00208   }
00209   //////// check double listing ////////
00210   
00211   //////////////// rows ////////////////
00212   //////////////// cols ////////////////
00213   
00214   //////////////// NOTE ////////////////
00215   std::cerr << "# [NOTE] zgsmatrix::checkup(): This sparse matrix is fine." << std::endl;
00216 }
00217 
00218 ///////////////////////////////////////////////////////////////////////////////
00219 ///////////////////////////////////////////////////////////////////////////////
00220 ///////////////////////////////////////////////////////////////////////////////
00221 
00222 //=============================================================================
00223 /*! get row of the matrix */
00224 inline _zrovector zgsmatrix::row(const long& _m) const
00225 {VERBOSE_REPORT;
00226 #ifdef  CPPL_DEBUG
00227   if( _m<0 || _m>m ){
00228     ERROR_REPORT;
00229     std::cerr << "Input row number must be between 0 and " << m << "." << std::endl
00230               << "Your input was " << _m << "." << std::endl;
00231     exit(1);
00232   }
00233 #endif//CPPL_DEBUG
00234   
00235   zrovector vec( zrovector(n).zero() );
00236   for(std::vector<uint32_t>::const_iterator p=rows[_m].begin(); p!=rows[_m].end(); p++){
00237     vec(data[*p].j) =data[*p].v;
00238   }
00239   return _(vec);
00240 }
00241 
00242 //=============================================================================
00243 /*! get column of the matrix */
00244 inline _zcovector zgsmatrix::col(const long& _n) const
00245 {VERBOSE_REPORT;
00246 #ifdef  CPPL_DEBUG
00247   if( _n<0 || _n>n ){
00248     ERROR_REPORT;
00249     std::cerr << "Input row number must be between 0 and " << n << "." << std::endl
00250               << "Your input was " << _n << "." << std::endl;
00251     exit(1);
00252   }
00253 #endif//CPPL_DEBUG
00254   
00255   zcovector vec( zcovector(m).zero() );
00256   for(std::vector<uint32_t>::const_iterator p=cols[_n].begin(); p!=cols[_n].end(); p++){
00257     vec(data[*p].i) =data[*p].v;
00258   }
00259   return _(vec);
00260 }
00261 
00262 ///////////////////////////////////////////////////////////////////////////////
00263 ///////////////////////////////////////////////////////////////////////////////
00264 ///////////////////////////////////////////////////////////////////////////////
00265 
00266 //=============================================================================
00267 /*! swap two matrices */
00268 inline void swap(zgsmatrix& A, zgsmatrix& B)
00269 {VERBOSE_REPORT;
00270   std::swap(A.n,B.n);
00271   std::swap(A.m,B.m);
00272   std::swap(A.data,B.data);
00273   std::swap(A.rows,B.rows);
00274   std::swap(A.cols,B.cols);
00275 }
00276 
00277 //=============================================================================
00278 /*! convert user object to smart-temporary object */
00279 inline _zgsmatrix _(zgsmatrix& mat)
00280 {VERBOSE_REPORT;
00281   _zgsmatrix newmat;
00282   
00283   //////// shallow copy ////////
00284   newmat.n =mat.n;
00285   newmat.m =mat.m;
00286   std::swap(newmat.data,mat.data);
00287   std::swap(newmat.rows,mat.rows);
00288   std::swap(newmat.cols,mat.cols);
00289 
00290   //////// nullify ////////
00291   mat.m =0;
00292   mat.n =0;
00293   
00294   return newmat;
00295 }
 All Classes Files Functions Variables Friends