band_matrix.h
00001 /* Copyright (C) 2003-2005  Martin Förg
00002 
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  * 
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00016  *
00017  * Contact:
00018  *   martin.o.foerg@googlemail.com
00019  *
00020  */
00021 
00022 #ifndef general_band_matrix_h
00023 #define general_band_matrix_h
00024 
00025 #include "vector.h"
00026 #include "types.h"
00027 #include "_memory.h"
00028 #include <cstdlib>
00029 
00030 namespace fmatvec {
00031 
00040   template <class AT> class Matrix<GeneralBand,Ref,Ref,AT> {
00041 
00043 
00044     protected:
00045 
00046       Memory<AT> memory;
00047       AT *ele;
00048       int n;
00049       int kl;
00050       int ku;
00051 
00052       inline void deepCopy(const Matrix<GeneralBand,Ref,Ref,AT> &A); 
00053 
00054       Matrix(int n_, int kl_, int ku_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), n(n_), kl(kl_), ku(ku_) {
00055       }
00056 
00058 
00059     public:
00060 
00065       Matrix() : memory(), ele(0), n(0), kl(0), ku(0) {
00066       }
00067 
00068 //      template<class Ini=All<AT> >
00069 //        Matrix(int n_, int kl_, int ku_, Ini ini=All<AT>()) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) {  
00070 //          init(ini);
00071 //        }
00072 //      template<class Ini=All<AT> >
00073 //        Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_*(n_+n_+1)), ele((AT*)memory.get()), n(n_), kl(n_), ku(n_) {  
00074 //          init(ini);
00075 //        }
00076 
00077       Matrix(int n_, int kl_, int ku_, Noinit) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { }
00078       Matrix(int n_, int kl_, int ku_, Init ini=INIT, const AT &a=0) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(a); }
00079       Matrix(int n_, int kl_, int ku_, Eye ini, const AT &a=1) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(ini,a); }
00080       Matrix(int m_, int n_, int kl_, int ku_, Noinit) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { }
00081       Matrix(int m_, int n_, int kl_, int ku_, Init ini=INIT, const AT &a=0) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(a); }
00082       Matrix(int m_, int n_, int kl_, int ku_, Eye ini, const AT &a=1) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(ini,a); }
00083 
00091       Matrix(const Matrix<GeneralBand,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) , n(A.n), kl(A.kl), ku(A.ku) {
00092       }
00093 
00103       Matrix(int n_, int kl_, int ku_, AT* ele_) : memory(), ele(ele_), n(n_), kl(kl_), ku(ku_) { 
00104       }
00105 
00108       ~Matrix() {
00109       }
00110 
00111       Matrix<GeneralBand,Ref,Ref,AT>& resize() { 
00112           n = kl = ku = 0;
00113           memory.resize(0);
00114           ele = 0;
00115           return *this;
00116       }
00117 
00118       Matrix<GeneralBand,Ref,Ref,AT>& resize(int n_, int kl_, int ku_, Noinit) { 
00119           n = n_; kl = kl_; ku = ku_;
00120           memory.resize(n*(kl+ku+1));
00121           ele = (AT*)memory.get();
00122           return *this;
00123       }
00124 
00125       Matrix<GeneralBand,Ref,Ref,AT>& resize(int n, int kl, int ku=0, Init ini=INIT, const AT &a=0) { return resize(n,kl,ku,Noinit()).init(a); }
00126 
00127       Matrix<GeneralBand,Ref,Ref,AT>& resize(int n, int kl, int ku, Eye ini, const AT &a=1) { return resize(n,kl,ku,Noinit()).init(a); }
00128 
00137       inline Matrix<GeneralBand,Ref,Ref,AT>& operator=(const Matrix<GeneralBand,Ref,Ref,AT> &A);
00138 
00145       inline Matrix<GeneralBand,Ref,Ref,AT>& operator<<(const Matrix<GeneralBand,Ref,Ref,AT> &A);
00146 
00153       inline Matrix<GeneralBand,Ref,Ref,AT>& operator>>(const Matrix<GeneralBand,Ref,Ref,AT> &A);
00154 
00166       const AT& operator()(int i, int j) const {
00167 #ifndef FMATVEC_NO_BOUNDS_CHECK
00168         assert(i>=0);
00169         assert(j>=0);
00170         assert(i<n);
00171         assert(j<n);
00172         //     assert(i-j<=kl);
00173         //      assert(i-j>=-ku);
00174 #endif
00175         static AT zero=0;
00176 
00177         return ((i-j>kl) || (i-j<-ku)) ? zero : ele[ku+i+j*(kl+ku)];
00178       };
00179 
00185       AT* operator()() {return ele;};
00186 
00191       const AT* operator()() const {return ele;};
00192 
00197       int size() const {return n;};
00198 
00203       int rows() const {return n;};
00204 
00209       int cols() const {return n;};
00210 
00218       const CBLAS_ORDER blasOrder() const {
00219               return  CblasColMajor;
00220       };
00221 
00226       inline const Vector<Ref,AT> operator()(int i) const;
00227 
00235       inline Vector<Ref,AT> operator()(int i);
00236 
00242       inline Matrix<GeneralBand,Ref,Ref,AT> copy() const;
00243 
00251       inline Matrix<GeneralBand,Ref,Ref,AT>& init(const AT &a=0);
00252       inline Matrix<GeneralBand,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
00253       inline Matrix<GeneralBand,Ref,Ref,AT>& init(Eye, const AT &a=1);
00254       inline Matrix<GeneralBand,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
00255 
00260       inline operator std::vector<std::vector<AT> >();
00261   };
00262 
00263   template <class AT>
00264     inline Matrix<GeneralBand,Ref,Ref,AT>& Matrix<GeneralBand,Ref,Ref,AT>::operator>>(const Matrix<GeneralBand,Ref,Ref,AT> &A) { 
00265 
00266       n=A.n;
00267       memory = A.memory;
00268       ele = A.ele;
00269 
00270       return *this;
00271     }
00272 
00273   template <class AT>
00274     inline Matrix<GeneralBand,Ref,Ref,AT>& Matrix<GeneralBand,Ref,Ref,AT>::operator=(const Matrix<GeneralBand,Ref,Ref,AT> &A) { 
00275 
00276      if(!ele) {
00277        n = A.n;
00278        memory.resize(n*n);
00279        ele = (AT*)memory.get();
00280     } 
00281      else {
00282 #ifndef FMATVEC_NO_SIZE_CHECK
00283        assert(n == A.n);
00284 #endif
00285      }
00286 
00287       deepCopy(A);
00288 
00289       return *this;
00290     }
00291 
00292   template <class AT>
00293     inline Matrix<GeneralBand,Ref,Ref,AT>& Matrix<GeneralBand,Ref,Ref,AT>::operator<<(const Matrix<GeneralBand,Ref,Ref,AT> &A) { 
00294 
00295       if(n!=A.n) {
00296         n = A.n;
00297         memory.resize(n*n);
00298         ele = (AT*)memory.get();
00299       } 
00300 
00301       deepCopy(A);
00302 
00303       return *this;
00304     }
00305 
00306   template <class AT>
00307     inline Matrix<GeneralBand,Ref,Ref,AT>&  Matrix<GeneralBand,Ref,Ref,AT>::init(const AT& val) {
00308       for(int i=0; i<kl+ku+1; i++)
00309         for(int j=0; j<n; j++)
00310           ele[i+j*(kl+ku+1)] = val;
00311       return *this;
00312     }
00313 
00314   template <class AT>
00315     inline Matrix<GeneralBand,Ref,Ref,AT>&  Matrix<GeneralBand,Ref,Ref,AT>::init(Eye, const AT &val) {
00316       for(int i=0; i<n; i++)
00317         ele[i] = val;
00318       return *this;
00319     }
00320 
00321   template <class AT>
00322     inline Vector<Ref,AT> Matrix<GeneralBand,Ref,Ref,AT>::operator()(int i) {
00323 #ifndef FMATVEC_NO_BOUNDS_CHECK
00324       assert(i<=ku);
00325       assert(i>=-kl);
00326 #endif
00327 
00328       //return Vector<Ref,AT>(n-i,ku+kl+1,memory,ele[ku-i + (i>0?i:0)*(kl+ku+1)]);
00329       return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
00330       //(ku-i,i>0?i:0));
00331     }
00332 
00333   template <class AT>
00334     inline const Vector<Ref,AT> Matrix<GeneralBand,Ref,Ref,AT>::operator()(int i) const {
00335 #ifndef FMATVEC_NO_BOUNDS_CHECK
00336       assert(i<=ku);
00337       assert(i>=-kl);
00338 #endif
00339 
00340       return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
00341       //(ku-i,i>0?i:0));
00342     }
00343 
00344   template <class AT>
00345     inline Matrix<GeneralBand,Ref,Ref,AT> Matrix<GeneralBand,Ref,Ref,AT>::copy() const {
00346 
00347       Matrix<GeneralBand,Ref,Ref,AT> A(n,kl,ku);
00348       A.deepCopy(*this);
00349 
00350       return A;
00351     }
00352 
00353   template <class AT>
00354     inline Matrix<GeneralBand,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() {
00355       std::vector<std::vector<AT> > ret(rows());
00356       for(int r=0; r<rows(); r++) {
00357         ret[r].resize(cols());
00358         for(int c=0; c<cols(); c++)
00359           ret[r][c]=operator()(r,c);
00360       }
00361       return ret;
00362     }
00363 
00365 
00366   template <class AT>
00367     inline void Matrix<GeneralBand,Ref,Ref,AT>::deepCopy(const Matrix<GeneralBand,Ref,Ref,AT> &A) { 
00368 
00369       for(int i=0; i<kl+ku+1; i++)
00370         for(int j=0; j<n; j++)
00371           ele[i+j*(kl+ku+1)] = A.ele[i+j*(kl+ku+1)];
00372     }
00373 
00375 
00376 }
00377 
00378 #endif