CPPLapack
 All Classes Files Functions Variables Friends
zgsmatrix-io.hpp
Go to the documentation of this file.
00001 //=============================================================================
00002 /*! operator() for const object */
00003 inline comple zgsmatrix::operator()(const long& i, const long& j) const
00004 {VERBOSE_REPORT;
00005 #ifdef  CPPL_DEBUG
00006   if( i<0 || j<0 || m<=i || n<=j ){
00007     ERROR_REPORT;
00008     std::cerr << "The required component is out of the matrix size." << std::endl
00009               << "Your input was (" << i << "," << j << ")." << std::endl;
00010     exit(1);
00011   }
00012 #endif//CPPL_DEBUG
00013   
00014   //// search (i,j) component ////
00015   for(std::vector<uint32_t>::const_iterator p=rows[i].begin(); p!=rows[i].end(); p++){
00016     if(long(data[*p].j)==j){ return data[*p].v; }
00017   }
00018   
00019   //// (i,j) component was not found ////
00020   return comple(0.0,0.0);
00021 }
00022 
00023 //=============================================================================
00024 /*! operator() for const object */
00025 inline comple& zgsmatrix::operator()(const long& i, const long& j)
00026 {VERBOSE_REPORT;
00027 #ifdef  CPPL_DEBUG
00028   if( i<0 || j<0 || m<=i || n<=j ){
00029     ERROR_REPORT;
00030     std::cerr << "The required component is out of the matrix size." << std::endl
00031               << "Your input was (" << i << "," << j << ")." << std::endl;
00032     exit(1);
00033   }
00034 #endif//CPPL_DEBUG
00035   
00036   //////// search (i,j) component ////////
00037   for(std::vector<uint32_t>::iterator p=rows[i].begin(); p!=rows[i].end(); p++){
00038     if(long(data[*p].j)==j){ return data[*p].v; }
00039   }
00040   
00041   //////// (i,j) component not found ////////
00042   rows[i].push_back(data.size());
00043   cols[j].push_back(data.size());
00044   data.push_back( zcomponent(i,j, comple(0.,0.)) );
00045   return data.back().v;
00046 }
00047 
00048 ///////////////////////////////////////////////////////////////////////////////
00049 ///////////////////////////////////////////////////////////////////////////////
00050 ///////////////////////////////////////////////////////////////////////////////
00051 
00052 //=============================================================================
00053 /*! put value without isListed check */
00054 inline zgsmatrix& zgsmatrix::put(const long& i, const long& j, const comple& v)
00055 {VERBOSE_REPORT;
00056 #ifdef  CPPL_DEBUG
00057   if( i<0 || j<0 || m<=i || n<=j ){
00058     ERROR_REPORT;
00059     std::cerr << "The required component is out of the matrix size." << std::endl
00060               << "Your input was (" << i << "," << j << ")." << std::endl;
00061     exit(1);
00062   }
00063   
00064   if( isListed(i,j) ){
00065     ERROR_REPORT;
00066     std::cerr << "The required component is already listed." << std::endl
00067               << "Your input was (" << i << "," << j << ")." << std::endl;
00068     exit(1);
00069   }
00070 #endif//CPPL_DEBUG
00071   
00072   //// push ////
00073   rows[i].push_back(data.size());
00074   cols[j].push_back(data.size());
00075   data.push_back(zcomponent(i,j,v));
00076   
00077   return *this;
00078 }
00079 
00080 ///////////////////////////////////////////////////////////////////////////////
00081 ///////////////////////////////////////////////////////////////////////////////
00082 ///////////////////////////////////////////////////////////////////////////////
00083 
00084 //=============================================================================
00085 /*! delete the entry of a component */
00086 inline zgsmatrix& zgsmatrix::del(const long i, const long j)
00087 {VERBOSE_REPORT;
00088 #ifdef  CPPL_DEBUG
00089   if( i<0 || j<0 || m<=i || n<=j ){
00090     ERROR_REPORT;
00091     std::cerr << "The required component is out of the matrix size." << std::endl
00092               << "Your input was (" << i << "," << j << ")." << std::endl;
00093     exit(1);
00094   }
00095 #endif//CPPL_DEBUG
00096   
00097   //// search (i,j) component ////
00098   for(std::vector<uint32_t>::iterator p=rows[i].begin(); p!=rows[i].end(); p++){
00099     if(long(data[*p].j)==j){//exists
00100       //// save position ////
00101       uint32_t c(*p);
00102       uint32_t C(data.size()-1);
00103       
00104       //// data translation ////
00105       data[c]=data.back();
00106       data.pop_back();
00107       
00108       //// remove from List ////
00109       rows[i].erase(p);
00110       for(std::vector<uint32_t>::iterator q=cols[j].begin(); q!=cols[j].end(); q++){
00111         if(*q==c){ cols[j].erase(q); break; }
00112       }
00113       
00114       //// modify the entry of translated component ////
00115       long I(data[c].i), J(data[c].j);
00116       for(std::vector<uint32_t>::iterator q=rows[I].begin(); q!=rows[I].end(); q++){
00117         if(*q==C){ *q=c; break; }
00118       }
00119       for(std::vector<uint32_t>::iterator q=cols[J].begin(); q!=cols[J].end(); q++){
00120         if(*q==C){ *q=c; break; }
00121       }
00122       return *this;
00123     }
00124   }
00125   
00126 #ifdef  CPPL_DEBUG
00127   std::cerr << "# [NOTE] zgsmatrix::del(long&, long&): The required component was not listed. Your input was (" << i << "," << j << ")." << std::endl;
00128 #endif//CPPL_DEBUG
00129   
00130   return *this;
00131 }
00132 
00133 //=============================================================================
00134 /*! delete the entry of an element */
00135 inline zgsmatrix& zgsmatrix::del(const long c)
00136 {VERBOSE_REPORT;
00137 #ifdef  CPPL_DEBUG
00138   if( c<0 || c>=long(data.size()) ){
00139     ERROR_REPORT;
00140     std::cerr << "The required element is out of the matrix volume." << std::endl
00141               << "Your input was (" << c << ")." << std::endl;
00142     exit(1);
00143   }
00144 #endif//CPPL_DEBUG
00145   
00146   if( c==long(data.size()-1) ){//if c is the last element
00147     long i(data[c].i), j(data[c].j);
00148     for(std::vector<uint32_t>::iterator q=rows[i].begin(); q!=rows[i].end(); q++){
00149       if(long(*q)==c){ rows[i].erase(q); break; }
00150     }
00151     for(std::vector<uint32_t>::iterator q=cols[j].begin(); q!=cols[j].end(); q++){
00152       if(long(*q)==c){ cols[j].erase(q); break; }
00153     }
00154     data.pop_back();
00155   }
00156 
00157   else{//if c is NOT the last element
00158     //// data translation ////
00159     uint32_t C(data.size()-1);
00160     long i(data[c].i), j(data[c].j), I(data.back().i), J(data.back().j);
00161     data[c]=data.back();
00162     //// remove entry of component ////
00163     for(std::vector<uint32_t>::iterator q=rows[i].begin(); q!=rows[i].end(); q++){
00164       if(long(*q)==c){ rows[i].erase(q); break; }
00165     }
00166     for(std::vector<uint32_t>::iterator q=cols[j].begin(); q!=cols[j].end(); q++){
00167       if(long(*q)==c){ cols[j].erase(q); break; }
00168     }
00169     //// modify the entry of translated component ////
00170     for(std::vector<uint32_t>::iterator q=rows[I].begin(); q!=rows[I].end(); q++){
00171       if(*q==C){ *q=c; break; }
00172     }
00173     for(std::vector<uint32_t>::iterator q=cols[J].begin(); q!=cols[J].end(); q++){
00174       if(*q==C){ *q=c; break; }
00175     }
00176     //// pop_back ////
00177     data.pop_back();
00178   }
00179   
00180   return *this;
00181 }
00182 
00183 ///////////////////////////////////////////////////////////////////////////////
00184 ///////////////////////////////////////////////////////////////////////////////
00185 ///////////////////////////////////////////////////////////////////////////////
00186 
00187 //=============================================================================
00188 inline std::ostream& operator<<(std::ostream& s, const zgsmatrix& mat)
00189 {VERBOSE_REPORT;
00190   for(long i=0; i<mat.m; i++){
00191     for(long j=0; j<mat.n; j++){
00192       std::vector<uint32_t>::const_iterator q;
00193       for(q=mat.rows[i].begin(); q!=mat.rows[i].end(); q++){
00194         if(long(mat.data[*q].j)==j){ break; }
00195       }
00196       if(q!=mat.rows[i].end()){ s << " " << mat.data[*q].v; }
00197       else{ s << " x"; }
00198     }
00199     s << std::endl;
00200   }
00201   
00202   return s;
00203 }
00204 
00205 ///////////////////////////////////////////////////////////////////////////////
00206 ///////////////////////////////////////////////////////////////////////////////
00207 ///////////////////////////////////////////////////////////////////////////////
00208 
00209 //=============================================================================
00210 inline void zgsmatrix::write(const char* filename) const
00211 {VERBOSE_REPORT;
00212   std::ofstream ofs(filename, std::ios::trunc);
00213   ofs.setf(std::cout.flags());
00214   ofs.precision(std::cout.precision());
00215   ofs.width(std::cout.width());
00216   ofs.fill(std::cout.fill());
00217   
00218   ofs << "#zgsmatrix" << " " << m << " " << n << std::endl;
00219   for(std::vector<zcomponent>::const_iterator it=data.begin(); it!=data.end(); it++){
00220     ofs << it->i << " " << it->j << " " << it->v << std::endl;
00221   }
00222   ofs.close();
00223 }
00224 
00225 //=============================================================================
00226 inline void zgsmatrix::read(const char* filename)
00227 {VERBOSE_REPORT;
00228   std::ifstream s( filename );
00229   if(!s){
00230     ERROR_REPORT;
00231     std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
00232     exit(1);
00233   }
00234 
00235   std::string id;
00236   s >> id;
00237   if( id != "zgsmatrix" && id != "#zgsmatrix" ){
00238     ERROR_REPORT;
00239     std::cerr << "The type name of the file \"" << filename << "\" is not zgsmatrix." << std::endl
00240               << "Its type name was " << id << " ." << std::endl;
00241     exit(1);
00242   }
00243   
00244   s >> m >> n;
00245   resize(m, n);
00246   
00247   comple val;
00248   long i, j,  pos, tmp;
00249   while(!s.eof()){
00250     s >> i >> j >> val;
00251     put(i,j, val);
00252     pos =s.tellg();
00253     s >> tmp;
00254     s.seekg(pos);
00255   }
00256   
00257   if(!s.eof()){
00258     ERROR_REPORT;
00259     std::cerr << "There is something is wrong with the file \"" << filename << " ." << std::endl
00260               << "Most likely, there are too many data components over the context." << std::endl;
00261     exit(1);
00262   }
00263   s.close();
00264 }
 All Classes Files Functions Variables Friends