numtns.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 _NUMTNS_HPP_
00020 #define _NUMTNS_HPP_
00021 
00022 #include "nummat.hpp"
00023 
00024 
00026 template <class F>
00028 
00033 class NumTns
00034 {
00035 public:
00037   int _m;
00039   int _n;
00041   int _p;
00043   bool _owndata;
00045   F* _data;
00046 public:
00048   NumTns(int m=0, int n=0, int p=0): _m(m), _n(n), _p(p), _owndata(true) {
00049          if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00050   }
00051   NumTns(int m, int n, int p, bool owndata, F* data): _m(m), _n(n), _p(p), _owndata(owndata) {
00052          if(_owndata) {
00053                 if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00054                 if(_m>0 && _n>0 && _p>0) { for(int i=0; i<_m*_n*_p; i++) _data[i] = data[i]; }
00055          } else {
00056                 _data = data;
00057          }
00058   }
00059   NumTns(const NumTns& C): _m(C._m), _n(C._n), _p(C._p), _owndata(C._owndata) {
00060          if(_owndata) {
00061                 if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00062                 if(_m>0 && _n>0 && _p>0) { for(int i=0; i<_m*_n*_p; i++) _data[i] = C._data[i]; }
00063          } else {
00064                 _data = C._data;
00065          }
00066   }
00067   ~NumTns() { 
00068          if(_owndata) { 
00069                 if(_m>0 && _n>0) { delete[] _data; _data = NULL; } 
00070          }
00071   }
00072   NumTns& operator=(const NumTns& C) {
00073          if(_owndata) { 
00074                 if(_m>0 && _n>0 && _p>0) { delete[] _data; _data = NULL; } 
00075          }
00076          _m = C._m; _n=C._n; _p=C._p; _owndata=C._owndata;
00077          if(_owndata) {
00078                 if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00079                 if(_m>0 && _n>0 && _p>0) { for(int i=0; i<_m*_n*_p; i++) _data[i] = C._data[i]; }
00080          } else {
00081                 _data = C._data;
00082          }
00083          return *this;
00084   }
00085   void resize(int m, int n, int p)  {
00086          assert( _owndata==true );
00087          if(_m!=m || _n!=n || _p!=p) {
00088                 if(_m>0 && _n>0 && _p>0) { delete[] _data; _data = NULL; } 
00089                 _m = m; _n = n; _p=p;
00090                 if(_m>0 && _n>0 && _p>0) { _data = new F[_m*_n*_p]; assert( _data!=NULL ); } else _data=NULL;
00091          }
00092   }
00093   const F& operator()(int i, int j, int k) const  { 
00094          assert( i>=0 && i<_m && j>=0 && j<_n && k>=0 && k<_p);
00095          return _data[i+j*_m+k*_m*_n];
00096   }
00097   F& operator()(int i, int j, int k)  { 
00098          assert( i>=0 && i<_m && j>=0 && j<_n && k>=0 && k<_p);
00099          return _data[i+j*_m+k*_m*_n];
00100   }
00101   
00102   F* data() const { return _data; }
00103   int m() const { return _m; }
00104   int n() const { return _n; }
00105   int p() const { return _p; }
00106 };
00107 
00108 template <class F> inline ostream& operator<<( ostream& os, const NumTns<F>& tns)
00109 {
00110   os<<tns.m()<<" "<<tns.n()<<" "<<tns.p()<<endl;
00111   os.setf(ios_base::scientific, ios_base::floatfield);
00112   for(int i=0; i<tns.m(); i++) {
00113          for(int j=0; j<tns.n(); j++) {
00114                 for(int k=0; k<tns.p(); k++) {
00115                   os<<" "<<tns(i,j,k);
00116                 }
00117                 os<<endl;
00118          }
00119          os<<endl;
00120   }
00121   return os;
00122 }
00123 template <class F> inline void setvalue(NumTns<F>& M, F val)
00124 {
00125   for(int i=0; i<M.m(); i++)
00126          for(int j=0; j<M.n(); j++)
00127                 for(int k=0; k<M.p(); k++)
00128                   M(i,j,k) = val;
00129   return;
00130 }
00131 
00132 typedef NumTns<bool>   BolNumTns;
00133 typedef NumTns<char>   ChrNumTns;
00134 typedef NumTns<int>    IntNumTns;
00135 typedef NumTns<double> DblNumTns;
00136 typedef NumTns<cpx>    CpxNumTns;
00137 
00138 
00139 
00140 #endif
00141 
00142 
00143 
00144 

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