GenMatrix.h

00001 //===========================================================================
00002 // SINTEF Multilevel B-spline Approximation library - version 1.1
00003 //
00004 // Copyright (C) 2000-2005 SINTEF ICT, Applied Mathematics, Norway.
00005 //
00006 // This program is free software; you can redistribute it and/or          
00007 // modify it under the terms of the GNU General Public License            
00008 // as published by the Free Software Foundation version 2 of the License. 
00009 //
00010 // This program is distributed in the hope that it will be useful,        
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00013 // GNU General Public License for more details.                           
00014 //
00015 // You should have received a copy of the GNU General Public License      
00016 // along with this program; if not, write to the Free Software            
00017 // Foundation, Inc.,                                                      
00018 // 59 Temple Place - Suite 330,                                           
00019 // Boston, MA  02111-1307, USA.                                           
00020 //
00021 // Contact information: e-mail: tor.dokken@sintef.no                      
00022 // SINTEF ICT, Department of Applied Mathematics,                         
00023 // P.O. Box 124 Blindern,                                                 
00024 // 0314 Oslo, Norway.                                                     
00025 //
00026 // Other licenses are also available for this software, notably licenses
00027 // for:
00028 // - Building commercial software.                                        
00029 // - Building software whose source code you wish to keep private.        
00030 //===========================================================================
00031 #ifndef _GenMatrix_h_
00032 #define _GenMatrix_h_
00033 
00034 #include <iostream>
00035 
00036 //#define ARRAY_CHECK 1
00037 
00038 #ifdef ARRAY_CHECK
00039 static void myMessage(int i, int j) {
00040   std::cout << "Matrix out of range: (" << i << "," << j << ")" << std::endl; 
00041   std::cout << " press [return]" << std::endl;
00042   getchar();
00043 }
00044 #endif
00045 
00046 //===========================================================================
00056 //===========================================================================
00057 
00058 template <class Type>
00059 class GenMatrix {
00060   Type** arr_;
00061   int noX_, noY_;
00062   int rX_, rY_; // reserved space
00063 public:
00064   GenMatrix() {arr_ = NULL; noX_ = noY_ = rX_ = rY_ = 0;}
00065   GenMatrix(int noX, int  noY)
00066   {noX_ = noY_ = rX_ = rY_ = 0; arr_ = NULL; resize(noX, noY);}
00067   ~GenMatrix() {clear();}
00068 
00069 
00070   GenMatrix(const GenMatrix& G) { // copy constructor
00071     // By purpose, the copy constructor is not implemented.
00072     // Use the init function instead.
00073     std::cout << "GenMatrix:: copy constructor not implemented, use init()" << std::endl;
00074     std::cout << "EXIT FROM GenMatrix:: copy constructor" << std::endl;
00075 
00076     exit(-1);
00077   }
00078   
00079   
00080   void init(const GenMatrix& G) { // instead of copy constructor
00081     clear();
00082     reserve(G.rX_,G.rY_);
00083     resize(G.noX_,G.noY_);
00084     for (int j = 0; j < noY_; j++) {
00085       for (int i = 0; i < noX_; i++) {
00086         arr_[j][i] = G.arr_[j][i];
00087       }
00088     }
00089   }
00090 
00091 
00092   void swap(GenMatrix& mat) {
00093     std::swap(arr_, mat.arr_);
00094 
00095     std::swap(noX_, mat.noX_);
00096     std::swap(noY_, mat.noY_);
00097     std::swap(rX_ , mat.rX_);
00098     std::swap(rY_ , mat.rY_);
00099   }
00100 
00101   void operator += (GenMatrix& mat) {
00102     for (int j = 0; j < noY_; j++) {
00103       for (int i = 0; i < noX_; i++) {
00104         arr_[j][i] += mat.arr_[j][i];
00105       }
00106     }
00107   }
00108 
00109   Type norm_2() const
00110   {
00111       Type temp;
00112       Type res = 0;
00113       for (int j = 0; j < noY_; ++j) {
00114           for (int i = 0; i < noX_; ++i) {
00115               temp = arr_[j][i];
00116               res = temp*temp;
00117           }
00118       }
00119       return temp;
00120   }
00121 
00122 
00123   // Temporary ?
00124   void operator += (double offset) {
00125     for (int j = 0; j < noY_; j++) {
00126       for (int i = 0; i < noX_; i++) {
00127         arr_[j][i] += offset;
00128       }
00129     }
00130   }
00131   
00132   // Note that resize does not preserve content (and no fill with zeros is done)
00133   void resize(int noX, int noY) {
00134     if (noX > rX_ || noY > rY_) {
00135       clear();
00136       rX_ = noX; rY_ = noY; // reserved = allocated
00137       arr_ = new Type*[rY_];
00138       for (int j = 0; j < rY_; j++)
00139         arr_[j] = new Type[rX_];
00140     }
00141     noX_ = noX; noY_ = noY;
00142   }
00143   
00144   void reserve(int rX, int rY) {resize(rX, rY); noX_ = 0; noY_ = 0;}
00145 
00146   void clear() {
00147     if (arr_) {
00148       for (int j = 0; j < rY_; j++)
00149         delete [] arr_[j];
00150       delete [] arr_;
00151       arr_ = NULL;
00152     }
00153     rX_ = rY_ = noX_ = noY_ = 0;
00154   }
00155   
00156   inline int noX() const {return noX_;}
00157   inline int noY() const {return noY_;}
00158   void capacity(int& rX, int& rY) const {rX = rX_; rY = rY_;}
00159   
00160   inline const Type& operator()(int ii, int jj) const {
00161     
00162 #ifdef ARRAY_CHECK
00163     int i = ii+1;
00164     int j = jj+1;
00165     if (i < 0 || i >= noX_ || j < 0 || j >= noY_)
00166       myMessage(ii,jj);
00167 #endif
00168     return arr_[jj+1][ii+1];
00169   }
00170   
00171   inline       Type& operator()(int ii, int jj) {
00172     
00173 #ifdef ARRAY_CHECK
00174     int i = ii+1;
00175     int j = jj+1;
00176     if (i < 0 || i >= noX_ || j < 0 || j >= noY_)
00177       myMessage(ii,jj);
00178 #endif
00179     return arr_[jj+1][ii+1];
00180   }
00181   
00182   
00183   void fill(Type val) {
00184     for (int j = 0; j < noY_; j++)
00185       for (int i = 0; i < noX_; i++)
00186         arr_[j][i] = val;
00187   }
00188   
00189   
00190   void print() const {
00191     std::cout << "Matrix..." << std::endl;
00192     for (int j = 0; j < noY_; j++) {
00193       for (int i = 0; i < noX_; i++) {
00194         std::cout << arr_[j][i] << " ";
00195       }
00196       std::cout << std::endl;
00197     }
00198   }
00199   
00200   
00201   void printBitmap() const {
00202     //cout << "GenMatrix::printBitmap... " << arr_[noY_-1][0] << endl;
00203     for (int j = 0; j < noY_; j++) {
00204       for (int i = 0; i < noX_; i++) {
00205         if (arr_[j][i] == 0)
00206           std::cout << 0;
00207         else
00208           std::cout << 1;
00209       }
00210       std::cout << std::endl;
00211     }  
00212   }
00213 };
00214 
00215 #endif

Generated on Wed Nov 28 12:27:19 2007 for MBA by  doxygen 1.5.1