CPPLapack
 All Classes Files Functions Variables Friends
zgbmatrix-io.hpp
Go to the documentation of this file.
00001 //=============================================================================
00002 /*! operator() for non-const object */
00003 inline comple& zgbmatrix::operator()(const long& i, const long& j)
00004 {VERBOSE_REPORT;
00005 #ifdef  CPPL_DEBUG
00006   if( i<0 || j<0 || m<=i || n<=j || i-j>kl || j-i>ku ){
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   //return array[ku+i+(kl+ku)*j];
00015   return darray[j][ku-j+i];
00016 }
00017 
00018 //=============================================================================
00019 /*! operator() for const object */
00020 inline comple zgbmatrix::operator()(const long& i, const long& j) const
00021 {VERBOSE_REPORT;
00022 #ifdef  CPPL_DEBUG
00023   if( i<0 || j<0 || m<=i || n<=j || i-j>kl || j-i>ku ){
00024     ERROR_REPORT;
00025     std::cerr << "The required component is out of the matrix size." << std::endl
00026               << "Your input was (" << i << "," << j << ")." << std::endl;
00027     exit(1);
00028   }
00029 #endif//CPPL_DEBUG
00030   
00031   //return array[ku+i+(kl+ku)*j];
00032   return darray[j][ku-j+i];
00033 }
00034 
00035 ///////////////////////////////////////////////////////////////////////////////
00036 ///////////////////////////////////////////////////////////////////////////////
00037 ///////////////////////////////////////////////////////////////////////////////
00038 
00039 //=============================================================================
00040 /*! set value for const object */
00041 inline zgbmatrix& zgbmatrix::set(const long& i, const long& j, const comple& v)
00042 {VERBOSE_REPORT;
00043 #ifdef  CPPL_DEBUG
00044   if( i<0 || j<0 || m<=i || n<=j || i-j>kl || j-i>ku ){
00045     ERROR_REPORT;
00046     std::cerr << "The required component is out of the matrix size." << std::endl
00047               << "Your input was (" << i << "," << j << ")." << std::endl;
00048     exit(1);
00049   }
00050 #endif//CPPL_DEBUG
00051   
00052   //array[ku+i+(kl+ku)*j] =v;
00053   darray[j][ku-j+i] =v;
00054   
00055   return *this;
00056 }
00057 
00058 ///////////////////////////////////////////////////////////////////////////////
00059 ///////////////////////////////////////////////////////////////////////////////
00060 ///////////////////////////////////////////////////////////////////////////////
00061 
00062 //=============================================================================
00063 inline std::ostream& operator<<(std::ostream& s, const zgbmatrix& mat)
00064 {VERBOSE_REPORT;
00065   for(long i=0; i<mat.m; i++){
00066     for(long j=0; j<mat.n; j++){
00067       if( i-j>mat.kl || j-i>mat.ku ){ s << " x"; }
00068       else{ s << " " << mat(i,j); }
00069     }
00070     s << std::endl;
00071   }
00072   
00073   return s;
00074 }
00075 
00076 ///////////////////////////////////////////////////////////////////////////////
00077 ///////////////////////////////////////////////////////////////////////////////
00078 ///////////////////////////////////////////////////////////////////////////////
00079 
00080 //=============================================================================
00081 inline void zgbmatrix::write(const char* filename) const
00082 {VERBOSE_REPORT;
00083   std::ofstream ofs(filename, std::ios::trunc);
00084   ofs.setf(std::cout.flags());
00085   ofs.precision(std::cout.precision());
00086   ofs.width(std::cout.width());
00087   ofs.fill(std::cout.fill());
00088   
00089   ofs << "#zgbmatrix" << " " << m << " " << n << " " << kl << " " << ku << std::endl;
00090   for(long i=0; i<m; i++){
00091     for(long j=std::max(long(0),i-kl); j<std::min(n,i+ku+1); j++){
00092       ofs << operator()(i,j) << " ";
00093     }
00094     ofs << std::endl;
00095   }
00096   
00097   ofs.close();
00098 }
00099 
00100 //=============================================================================
00101 inline void zgbmatrix::read(const char* filename)
00102 {VERBOSE_REPORT;
00103   std::ifstream s( filename );
00104   if(!s){
00105     ERROR_REPORT;
00106     std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
00107     exit(1);
00108   }
00109 
00110   std::string id;
00111   s >> id;
00112   if( id != "zgbmatrix" && id != "#zgbmatrix" ){
00113     ERROR_REPORT;
00114     std::cerr << "The type name of the file \"" << filename << "\" is not zgbmatrix." << std::endl
00115               << "Its type name was " << id << " ." << std::endl;
00116     exit(1);
00117   }
00118   
00119   s >> m >> n >> kl >> ku;
00120   resize(m, n, kl, ku);
00121   for(long i=0; i<m; i++){
00122     for(long j=std::max(long(0),i-kl); j<std::min(n,i+ku+1); j++){
00123       s >> operator()(i,j);
00124     }
00125   }
00126   if(s.eof()){
00127     ERROR_REPORT;
00128     std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
00129               << "Most likely, there is not enough data components, or a linefeed code or space code is missing at the end of the last line." << std::endl;
00130     exit(1);
00131   }
00132   
00133   s >> id;
00134   if(!s.eof()){
00135     ERROR_REPORT;
00136     std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
00137               << "Most likely, there are extra data components." << std::endl;
00138     exit(1);
00139   }
00140   
00141   s.close();
00142 }
 All Classes Files Functions Variables Friends