nummat.hpp

Go to the documentation of this file.
00001 /* Kernel Independent Fast Multipole Method
00002    Copyright (C) 2004 Lexing Ying, New York University
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2, or (at your option)
00007 any later version.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT
00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00012 for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; see the file COPYING.  If not, write to the Free
00016 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00017 02111-1307, USA.  */
00019 #ifndef _NUMMAT_HPP_
00020 #define _NUMMAT_HPP_
00021 
00022 #include "numvec.hpp"
00023 
00025 template <class F>
00027 class NumMat
00028 {
00029 public:
00031   int _m;
00033   int _n;
00035   bool _owndata;
00037   F* _data;
00038 public:
00040   NumMat(int m=0, int n=0): _m(m), _n(n), _owndata(true) {
00041          if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00042   }
00045   NumMat(int m, int n, bool owndata, F* data): _m(m), _n(n), _owndata(owndata) {
00046          if(_owndata) {
00047                 if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00048                 if(_m>0 && _n>0) memcpy( _data, data, _m*_n*sizeof(F) );
00049          } else {
00050                 _data = data;
00051          }
00052   }
00056   NumMat(const NumMat& C): _m(C._m), _n(C._n), _owndata(C._owndata) {
00057          if(_owndata) {
00058                 if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00059                 if(_m>0 && _n>0) memcpy( _data, C._data, _m*_n*sizeof(F) );
00060          } else {
00061                 _data = C._data;
00062          }
00063   }
00065   ~NumMat() { 
00066          if(_owndata) { 
00067                 if(_m>0 && _n>0) { delete[] _data; _data = NULL; } 
00068          }
00069   }
00071   NumMat& operator=(const NumMat& C) {
00072          if(_owndata) { 
00073                 if(_m>0 && _n>0) { delete[] _data; _data = NULL; } 
00074          }
00075          _m = C._m; _n=C._n; _owndata=C._owndata;
00076          if(_owndata) {
00077                 if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00078                 if(_m>0 && _n>0) memcpy( _data, C._data, _m*_n*sizeof(F) );
00079          } else {
00080                 _data = C._data;
00081          }
00082          return *this;
00083   }
00085   void resize(int m, int n)  {
00086          assert( _owndata==true );
00087          if(_m!=m || _n!=n) {
00088                 if(_m>0 && _n>0) { delete[] _data; _data = NULL; } 
00089                 _m = m; _n = n;
00090                 if(_m>0 && _n>0) { _data = new F[_m*_n]; assert( _data!=NULL ); memset(_data, 0, _m*_n*sizeof(F)); } else _data=NULL;
00091          }
00092   }
00094   const F& operator()(int i, int j) const  { 
00095          assert( i>=0 && i<_m && j>=0 && j<_n );
00096          return _data[i+j*_m];
00097   }
00099   F& operator()(int i, int j)  { 
00100          assert( i>=0 && i<_m && j>=0 && j<_n );
00101          return _data[i+j*_m];
00102   }
00104   F* data() const { return _data; }
00106   F* clmdata(int j) { return &(_data[j*_m]); }
00108   int m() const { return _m; }
00110   int n() const { return _n; }
00111 };
00112 
00114 template <class F> inline ostream& operator<<( ostream& os, const NumMat<F>& mat)
00115 {
00116   os<<mat.m()<<" "<<mat.n()<<endl;
00117   os.setf(ios_base::scientific, ios_base::floatfield);
00118   for(int i=0; i<mat.m(); i++) {
00119          for(int j=0; j<mat.n(); j++)
00120                 os<<" "<<mat(i,j);
00121          os<<endl;
00122   }
00123   return os;
00124 }
00126 template <class F> inline void setvalue(NumMat<F>& M, F val)
00127 {
00128   for(int i=0; i<M.m(); i++)
00129          for(int j=0; j<M.n(); j++)
00130                 M(i,j) = val;
00131 }
00133 template <class F> inline void clear(NumMat<F>& M)
00134 {
00135   memset(M.data(), 0, M.m()*M.n()*sizeof(F));
00136 }
00137 
00139 typedef NumMat<bool>   BolNumMat;
00141 typedef NumMat<int>    IntNumMat;
00143 typedef NumMat<double> DblNumMat; 
00144 
00145 /*
00146   void getColumn(int j, Vector<F>& vec)  {
00147   assert( j>=0 && j<n() );
00148   vec.resize(m());
00149   for(int i=0; i<m(); i++)
00150   vec(i) = (*this)(i,j);
00151   }
00152   void getRow(int i, Vector<F>& vec)  {
00153   assert( i>=0 && i<m() );
00154   vec.resize(n());
00155   for(int j=0; j<n(); j++)
00156   vec(j) = (*this)(i,j);
00157   }
00158   void setColumn(int j, Vector<F>& vec)  {
00159   assert( j>=0 && j<n() );
00160   assert( vec.length() == m() );
00161   for(int i=0; i<m(); i++)
00162   (*this)(i,j) = vec(i);
00163   }
00164   void setRow(int i, Vector<F>& vec)  {
00165   assert( i>=0 && i<m());
00166   assert( vec.length() == n());
00167   for(int j=0; j<n(); j++)
00168   (*this)(i,j) = vec(j);
00169   }
00170 */
00171 
00172 
00173 
00174 #endif
00175 
00176 
00177 
00178 

Generated on Sun Dec 4 18:13:12 2005 for common by  doxygen 1.4.5