00001
00003 inline double dssmatrix::operator()(const long& i, const long& j) const
00004 {
00005 #ifdef CPPL_VERBOSE
00006 std::cerr << "# [MARK] dssmatrix::operator()(const long&, const long&) const"
00007 << std::endl;
00008 #endif//CPPL_VERBOSE
00009
00010 #ifdef CPPL_DEBUG
00011 if( i<0 || j<0 || M<=i || N<=j ){
00012 std::cerr << "[ERROR] dssmatrix::operator()(long, long)" << std::endl
00013 << "The required component is out of the matrix size."
00014 << std::endl
00015 << "Your input was (" << i << "," << j << ")." << std::endl;
00016 exit(1);
00017 }
00018 #endif//CPPL_DEBUG
00019
00020 for(long c=0; c<VOL; c++){
00021 if(Indx[c]==i && Jndx[c]==j){ return Array[c]; }
00022 }
00023
00025 std::cerr << "[ERROR] dssmatrix::operator()(long, long)" << std::endl
00026 << "The required component is out of the matrix size."
00027 << std::endl
00028 << "Your input was (" << i << "," << j << ")." << std::endl;
00029 exit(1);
00030 }
00031
00035
00036
00038 inline void dssmatrix::put(const long& i, const long& j, const double& v)
00039 {
00040 #ifdef CPPL_VERBOSE
00041 std::cerr << "# [MARK] dssmatrix::put(const long&, const long&, const double&)"
00042 << std::endl;
00043 #endif//CPPL_VERBOSE
00044
00045 #ifdef CPPL_DEBUG
00046 if( i<0 || j<0 || M<=i || N<=j ){
00047 std::cerr << "[ERROR] dssmatrix::put(long&, long&, double&)" << std::endl
00048 << "The required component is out of the matrix size."
00049 << std::endl
00050 << "Your input was (" << i << "," << j << ")." << std::endl;
00051 exit(1);
00052 }
00053 #endif//CPPL_DEBUG
00054
00056 for(long c=0; c<VOL; c++){
00057 if(Indx[c]==i && Jndx[c]==j){ Array[c]=v; return; }
00058 }
00059
00061 if(VOL==CAP){
00062 expand(CPPL_SS_SECTOR);
00063 #ifdef CPPL_DEBUG
00064 std::cerr << "[NOTE] dssmatrix::put(long&, long&, double&) "
00065 << "The matrix volume was automatically expanded." << std::endl;
00066 #endif//CPPL_DEBUG
00067 }
00068
00069 Indx[VOL]=i; Jndx[VOL]=j; Array[VOL]=v;
00070 VOL++;
00071 }
00072
00073
00075 inline void dssmatrix::fput(const long& i, const long& j, const double& v)
00076 {
00077 #ifdef CPPL_VERBOSE
00078 std::cerr << "# [MARK] dssmatrix::fput(const long&, const long&, const double&)"
00079 << std::endl;
00080 #endif//CPPL_VERBOSE
00081
00082 #ifdef CPPL_DEBUG
00083 if( i<0 || j<0 || M<=i || N<=j ){
00084 std::cerr << "[ERROR] dssmatrix::fput"
00085 << "(const long&, lconst ong&, const double&)" << std::endl
00086 << "The required component is out of the matrix size."
00087 << std::endl
00088 << "Your input was (" << i << "," << j << ")." << std::endl;
00089 exit(1);
00090 }
00091
00092 if(VOL==CAP){
00093 std::cerr << "[ERROR] dssmatrix::fput"
00094 << "(const long&, const long&, const double&)" << std::endl
00095 << "There is no free space to set a new entry." << std::endl;
00096 exit(1);
00097 }
00098
00099 for(long c=0; c<VOL; c++){
00100 if(Indx[c]==i && Jndx[c]==j){
00101 std::cerr << "[ERROR] dssmatrix::fput"
00102 << "(const long&, const long&, const double&)" << std::endl
00103 << "The required component is already listed." << std::endl
00104 << "Your input was (" << i << "," << j << ")." << std::endl;
00105 exit(1);
00106 }
00107 }
00108 #endif//CPPL_DEBUG
00109
00110 Indx[VOL]=i; Jndx[VOL]=j; Array[VOL]=v;
00111 VOL++;
00112 }
00113
00117
00118
00120 inline void dssmatrix::add(const long& i, const long& j, const double& v)
00121 {
00122 #ifdef CPPL_VERBOSE
00123 std::cerr << "# [MARK] dssmatrix::add(const long&, const long&, const double&)"
00124 << std::endl;
00125 #endif//CPPL_VERBOSE
00126
00127 #ifdef CPPL_DEBUG
00128 if( i<0 || j<0 || M<=i || N<=j ){
00129 std::cerr << "[ERROR] dssmatrix::add(long&, long&, double&)" << std::endl
00130 << "The required component is out of the matrix size."
00131 << std::endl
00132 << "Your input was (" << i << "," << j << ")." << std::endl;
00133 exit(1);
00134 }
00135 #endif//CPPL_DEBUG
00136
00138 for(long c=0; c<VOL; c++){
00139 if(Indx[c]==i && Jndx[c]==j){ Array[c]+=v; return; }
00140 }
00141
00143 if(VOL==CAP){
00144 expand(CPPL_SS_SECTOR);
00145 #ifdef CPPL_DEBUG
00146 std::cerr << "[NOTE] dssmatrix::add(long&, long&, double&) "
00147 << "The matrix volume was automatically expanded." << std::endl;
00148 #endif//CPPL_DEBUG
00149 }
00150
00151 Indx[VOL]=i; Jndx[VOL]=j; Array[VOL]=v;
00152 VOL++;
00153 }
00154
00155
00157 inline void dssmatrix::sub(const long& i, const long& j, const double& v)
00158 {
00159 #ifdef CPPL_VERBOSE
00160 std::cerr << "# [MARK] dssmatrix::sub(const long&, const long&, const double&)"
00161 << std::endl;
00162 #endif//CPPL_VERBOSE
00163
00164 #ifdef CPPL_DEBUG
00165 if( i<0 || j<0 || M<=i || N<=j ){
00166 std::cerr << "[ERROR] dssmatrix::sub(long&, long&, double&)" << std::endl
00167 << "The required component is out of the matrix size."
00168 << std::endl
00169 << "Your input was (" << i << "," << j << ")." << std::endl;
00170 exit(1);
00171 }
00172 #endif//CPPL_DEBUG
00173
00175 for(long c=0; c<VOL; c++){
00176 if(Indx[c]==i && Jndx[c]==j){ Array[c]-=v; return; }
00177 }
00178
00180 if(VOL==CAP){
00181 expand(CPPL_SS_SECTOR);
00182 #ifdef CPPL_DEBUG
00183 std::cerr << "[NOTE] dssmatrix::sub(long&, long&, double&) "
00184 << "The matrix volume was automatically expanded." << std::endl;
00185 #endif//CPPL_DEBUG
00186 }
00187
00188 Indx[VOL]=i; Jndx[VOL]=j; Array[VOL]=-v;
00189 VOL++;
00190 }
00191
00192
00194 inline void dssmatrix::mult(const long& i, const long& j, const double& v)
00195 {
00196 #ifdef CPPL_VERBOSE
00197 std::cerr << "# [MARK] dssmatrix::mult(const long&, const long&, const double&)"
00198 << std::endl;
00199 #endif//CPPL_VERBOSE
00200
00201 #ifdef CPPL_DEBUG
00202 if( i<0 || j<0 || M<=i || N<=j ){
00203 std::cerr << "[ERROR] dssmatrix::mult(long&, long&, double&)" << std::endl
00204 << "The required component is out of the matrix size."
00205 << std::endl
00206 << "Your input was (" << i << "," << j << ")." << std::endl;
00207 exit(1);
00208 }
00209 #endif//CPPL_DEBUG
00210
00212 for(long c=0; c<VOL; c++){
00213 if(Indx[c]==i && Jndx[c]==j){ Array[c]*=v; return; }
00214 }
00215
00217 return;
00218 }
00219
00220
00222 inline void dssmatrix::div(const long& i, const long& j, const double& v)
00223 {
00224 #ifdef CPPL_VERBOSE
00225 std::cerr << "# [MARK] dssmatrix::div(const long&, const long&, const double&)"
00226 << std::endl;
00227 #endif//CPPL_VERBOSE
00228
00229 #ifdef CPPL_DEBUG
00230 if( i<0 || j<0 || M<=i || N<=j ){
00231 std::cerr << "[ERROR] dssmatrix::div(long&, long&, double&)" << std::endl
00232 << "The required component is out of the matrix size."
00233 << std::endl
00234 << "Your input was (" << i << "," << j << ")." << std::endl;
00235 exit(1);
00236 }
00237 #endif//CPPL_DEBUG
00238
00240 for(long c=0; c<VOL; c++){
00241 if(Indx[c]==i && Jndx[c]==j){ Array[c]/=v; return; }
00242 }
00243
00245 return;
00246 }
00247
00251
00252
00254 inline void dssmatrix::del(const long& i, const long& j)
00255 {
00256 #ifdef CPPL_VERBOSE
00257 std::cerr << "# [MARK] dssmatrix::del(const long&, const long&)"
00258 << std::endl;
00259 #endif//CPPL_VERBOSE
00260
00261 #ifdef CPPL_DEBUG
00262 if( i<0 || j<0 || M<=i || N<=j ){
00263 std::cerr << "[ERROR] dssmatrix::del(long&, long&, double&)" << std::endl
00264 << "The required component is out of the matrix size."
00265 << std::endl
00266 << "Your input was (" << i << "," << j << ")." << std::endl;
00267 exit(1);
00268 }
00269 #endif//CPPL_DEBUG
00270
00271 for(long c=0; c<VOL; c++){
00272 if(Indx[c]==i && Jndx[c]==j){
00273 VOL--;
00274 Indx[c]=Indx[VOL]; Jndx[c]=Jndx[VOL]; Array[c]=Array[VOL];
00275 return;
00276 }
00277 }
00278
00279 std::cerr << "[WARNING] dssmatrix::del(long&, long&, double&) "
00280 << "The required component was not listed. "
00281 << "Your input was (" << i << "," << j << ")." << std::endl;
00282 }
00283
00284
00286 inline void dssmatrix::fdel(const long& c)
00287 {
00288 #ifdef CPPL_VERBOSE
00289 std::cerr << "# [MARK] dssmatrix::fdel(const long&)"
00290 << std::endl;
00291 #endif//CPPL_VERBOSE
00292
00293 #ifdef CPPL_DEBUG
00294 if( c<0 || c>VOL ){
00295 std::cerr << "[ERROR] dssmatrix::fdel(const long&)" << std::endl
00296 << "The required element is out of the matrix volume."
00297 << std::endl
00298 << "Your input was (" << c << ")." << std::endl;
00299 exit(1);
00300 }
00301
00302 std::cerr << "# [NOTE] dssmatrix::fdel(const long&) "
00303 << "The (" << Indx[c] << "," << Jndx[c]
00304 << ") component is going to be deleted." << std::endl;
00305 #endif//CPPL_DEBUG
00306
00307 VOL--;
00308 Indx[c]=Indx[VOL]; Jndx[c]=Jndx[VOL]; Array[c]=Array[VOL];
00309 }
00310
00314
00315
00316 inline std::ostream& operator<<(std::ostream& s, const dssmatrix& mat)
00317 {
00318 #ifdef CPPL_VERBOSE
00319 std::cerr << "# [MARK] operator<<(std::ostream&, const dssmatrix&)"
00320 << std::endl;
00321 #endif//CPPL_VERBOSE
00322
00323 long c;
00324 for(long i=0; i<mat.M; i++){
00325 for(long j=0; j<mat.N; j++){
00326 c=0;
00327 while(c<mat.VOL){
00328 if(mat.Indx[c]==i && mat.Jndx[c]==j){ break; }
00329 c++;
00330 }
00331 if(c!=mat.VOL){ s << mat.Array[c] << " "; }
00332 else{ s << "x "; }
00333 }
00334 s << std::endl;
00335 }
00336
00337 return s;
00338 }
00339
00343
00344
00345 inline void dssmatrix::write(const char* filename) const
00346 {
00347 #ifdef CPPL_VERBOSE
00348 std::cerr << "# [MARK] dssmatrix::write(const char*) const"
00349 << std::endl;
00350 #endif//CPPL_VERBOSE
00351
00352 std::ofstream s(filename, std::ios::trunc);
00353
00354 s << "dssmatrix" << " " << M << " " << N << " " << CAP << std::endl;
00355 for(long c=0; c<VOL; c++){
00356 s << Indx[c] << " " << Jndx[c] << " " << Array[c] << std::endl;
00357 }
00358 s.close();
00359 }
00360
00361
00362 inline void dssmatrix::read(const char* filename)
00363 {
00364 #ifdef CPPL_VERBOSE
00365 std::cerr << "# [MARK] dssmatrix::read(const char*)"
00366 << std::endl;
00367 #endif//CPPL_VERBOSE
00368
00369 std::ifstream s( filename );
00370 if(!s){
00371 std::cerr << "[ERROR] dssmatrix::read(const char*) " << std::endl
00372 << "The file \"" << filename << "\" can not be opened."
00373 << std::endl;
00374 exit(1);
00375 }
00376
00377 std::string id;
00378 s >> id;
00379 if( id != "dssmatrix" ){
00380 std::cerr << "[ERROR] dssmatrix::read(const char*) " << std::endl
00381 << "The type name of the file \"" << filename
00382 << "\" is not dssmatrix." << std::endl
00383 << "Its type name was " << id << " ." << std::endl;
00384 exit(1);
00385 }
00386
00387 s >> M >> N >> CAP;
00388 resize(M, N, CAP);
00389
00390 long pos, tmp;
00391 while(!s.eof() && VOL<CAP){
00392 s >> Indx[VOL] >> Jndx[VOL] >> Array[VOL];
00393 pos =s.tellg(); s >> tmp; s.seekg(pos);
00394 VOL++;
00395 }
00396
00397 if(!s.eof()){
00398 std::cerr << "[ERROR] dssmatrix::read(const char*) " << std::endl
00399 << "There is something is wrong with the file \""
00400 << filename << " ." << std::endl
00401 << "Most likely, there are too many data components "
00402 << "over the context." << std::endl;
00403 exit(1);
00404 }
00405 s.close();
00406 }