CPPLapack
 All Classes Files Functions Variables Friends
dgbmatrix-io.hpp
Go to the documentation of this file.
00001 //=============================================================================
00002 /*! operator() for non-const object */
00003 inline double& dgbmatrix::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 double dgbmatrix::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 dgbmatrix& dgbmatrix::set(const long& i, const long& j, const double& v) //const
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   return *this;
00055 }
00056 
00057 ///////////////////////////////////////////////////////////////////////////////
00058 ///////////////////////////////////////////////////////////////////////////////
00059 ///////////////////////////////////////////////////////////////////////////////
00060 
00061 //=============================================================================
00062 inline std::ostream& operator<<(std::ostream& s, const dgbmatrix& mat)
00063 {VERBOSE_REPORT;
00064   for(long i=0; i<mat.m; i++){
00065     for(long j=0; j<mat.n; j++){
00066       if( i-j>mat.kl || j-i>mat.ku ){ s << " x"; }
00067       else{ s << " " << mat(i,j); }
00068     }
00069     s << std::endl;
00070   }
00071   
00072   return s;
00073 }
00074 
00075 ///////////////////////////////////////////////////////////////////////////////
00076 ///////////////////////////////////////////////////////////////////////////////
00077 ///////////////////////////////////////////////////////////////////////////////
00078 
00079 //=============================================================================
00080 inline void dgbmatrix::write(const char *filename) const
00081 {VERBOSE_REPORT;
00082   std::ofstream ofs(filename, std::ios::trunc);
00083   ofs.setf(std::cout.flags());
00084   ofs.precision(std::cout.precision());
00085   ofs.width(std::cout.width());
00086   ofs.fill(std::cout.fill());
00087   
00088   ofs << "#dgbmatrix" << " " << m << " " << n << " " << kl << " " << ku << std::endl;
00089   for(long i=0; i<m; i++){
00090     for(long j=std::max(long(0),i-kl); j<std::min(n,i+ku+1); j++){
00091       ofs << operator()(i,j) << " ";
00092     }
00093     ofs << std::endl;
00094   }
00095   
00096   ofs.close();
00097 }
00098 
00099 //=============================================================================
00100 inline void dgbmatrix::read(const char* filename)
00101 {VERBOSE_REPORT;
00102   std::ifstream s( filename );
00103   if(!s){
00104     ERROR_REPORT;
00105     std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
00106     exit(1);
00107   }
00108 
00109   std::string id;
00110   s >> id;
00111   if( id != "dgbmatrix" && id != "#dgbmatrix" ){
00112     ERROR_REPORT;
00113     std::cerr << "The type name of the file \"" << filename << "\" is not dgbmatrix." << std::endl
00114               << "Its type name was " << id << " ." << std::endl;
00115     exit(1);
00116   }
00117   
00118   s >> m >> n >> kl >> ku;
00119   resize(m, n, kl, ku);
00120   for(long i=0; i<m; i++){
00121     for(long j=std::max(long(0),i-kl); j<std::min(n,i+ku+1); j++){
00122       s >> operator()(i,j);
00123     }
00124   }
00125   if(s.eof()){
00126     ERROR_REPORT;
00127     std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
00128               << "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;
00129     exit(1);
00130   }
00131   
00132   s >> id;
00133   if(!s.eof()){
00134     ERROR_REPORT;
00135     std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
00136               << "Most likely, there are extra data components." << std::endl;
00137     exit(1);
00138   }
00139   
00140   s.close();
00141 }
 All Classes Files Functions Variables Friends