MatSparse.h

00001 //===========================================================================
00002 // SINTEF LSMG 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 _MATSPARSE_H
00032 #define _MATSPARSE_H
00033 
00034 
00035 #include <BitMatrix.h>
00036 
00037 #include <vector>
00038 #include <fstream>
00039 
00040 // ???? #define LSMG_real float
00041 
00042 class MatSparse {
00043 private:
00044   int noR_, noC_;         // matrix dimensions
00045   int p_;                 // number of non-zeros
00046   std::vector<int> irow_; // the offset in a_ of the first non-zero for each row.
00047                           // Thus, irow_(0) == 0 always.
00048                           // (extended by one; see constructor)
00049   std::vector<int> jcol_; // The column no. of a given offset
00050   std::vector<double> a_; // the p_ non-zero elements
00051 public:
00052   MatSparse() : noR_(0), noC_(0), p_(0) {}
00053 
00054   MatSparse(int n1, int n2, int num_nonzero)
00055     : noR_(n1), noC_(n2), p_(num_nonzero), irow_(n1+1), jcol_(num_nonzero), a_(num_nonzero) {
00056     irow_[noR_] = num_nonzero;
00057   }
00058   
00059   ~MatSparse(){}
00060 
00061   void init(const BitMatrix& pattern); // Init the sparse structure from a bitmap pattern
00062 
00063   // Init the sparse structure from an entry-array, possibly sorted lexicographically
00064   void init(std::vector<std::pair<int,int> >& entries, int m, int n, bool sorted = false);
00065 
00066   void init(const MatSparse& mat, bool copyElements=false); // Serves as a "copy constructor" if copyElements=true
00067 
00068   void operator += (const MatSparse& mat); // Add a matrix (assumes sparsity pattern of mat is exactly like this).
00069   void operator *= (double fac);
00070 
00071   int noRows() const {return noR_;}
00072   int noColumns() const {return noC_;};
00073   
00074         double& operator () (int i, int j);       // inefficient !!!
00075   const double& operator () (int i, int j) const; // inefficient !!!
00076 
00077   double getVal(int i, int j) const; // as aove but returns zero when not in sparsity pattern inefficient !!!
00078 
00079   int getOffset(int i, int j) const; // and thus for access operator below
00080 
00081   void resize(int n1, int n2, int num_nonzero);
00082 
00083   int& irow(int k) {return irow_[k];} // 0 <= k <= noR_ index of a_ (last element is past-the-end)
00084   const int& irow(int k) const {return irow_[k];}
00085         int& jcol(int k) {return jcol_[k];} // 0 <= k <= p_-1
00086   const int& jcol(int k) const {return jcol_[k];}
00087         double& operator () (int k) {return a_[k];} // 0 <= k <= p_-1
00088   const double& operator () (int k) const {return a_[k];}
00089 
00090   double norml2() const;
00091   bool isSymmetric() const; // assumes m=n
00092   bool isDiagonallyDominant() const; 
00093   void printPattern(std::ostream& os) const;
00094   int noNonZeros() const {return p_;}
00095 
00096   void print(std::ostream& os) const; // debug
00097   void printMatlab(std::ostream& os) const; // debug
00098   void printPatternGnuplot(std::ostream& os) const; // debug
00099 };
00100 #endif

Generated on Wed Nov 28 12:27:29 2007 for LSMG by  doxygen 1.5.1