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