diagonal_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 diagonal_matrix_h
00023 #define diagonal_matrix_h
00024 
00025 #include "matrix.h"
00026 #include "types.h"
00027 #include "_memory.h"
00028 
00029 namespace fmatvec {
00030 
00039   template <class AT> class Matrix<Diagonal,Ref,Ref,AT> {
00040 
00041       protected:
00042 
00044 
00045         Memory<AT> memory;
00046         AT *ele;
00047         int n;
00048 
00049         void deepCopy(const Matrix<Diagonal,Ref,Ref,AT> &x);
00050 
00051         Matrix(int n_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), n(n_) {
00052         }
00053 
00054         const AT* elePtr(int i) const {
00055           return ele+i;
00056         }
00057 
00058         AT* elePtr(int i) {
00059           return ele+i;
00060         }
00061 
00063 
00064       public:
00065 
00070         Matrix() : memory(), ele(0), n(0) { }
00071 
00072 //        template<class Ini=All<AT> >
00073 //          Matrix(int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
00074 //        template<class Ini=All<AT> >
00075 //          Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
00076 
00077         Matrix(int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
00078         Matrix(int n_, Init ini=INIT, const AT &a=0) : memory(n_), ele((AT*)memory.get()), n(n_) { init(a); }
00079         Matrix(int n_, Eye ini, const AT &a=1) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini,a); }
00080         Matrix(int m_, int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
00081 
00089         Matrix(const Matrix<Diagonal,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) ,n(A.n) {
00090         }
00091 
00094         ~Matrix() {
00095         }
00096 
00097         Matrix<Diagonal,Ref,Ref,AT>& resize() { 
00098           n = 0;
00099           memory.resize(0);
00100           ele = 0;
00101           return *this;
00102         }
00103 
00104         Matrix<Diagonal,Ref,Ref,AT>& resize(int n_, Noinit) { 
00105           n = n_;
00106           memory.resize(n);
00107           ele = (AT*)memory.get();
00108           return *this;
00109         }
00110  
00111         Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Init ini=INIT, const AT &a=0) { resize(n,Noinit()).init(a); }
00112         Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Eye ini, const AT &a=1) { resize(n,Noinit()).init(ini,a); }
00113  
00120         inline Matrix<Diagonal,Ref,Ref,AT>& operator<<(const Matrix<Diagonal,Ref,Ref,AT> &A);
00121 
00128         inline Matrix<Diagonal,Ref,Ref,AT>& operator>>(const Matrix<Diagonal,Ref,Ref,AT> &A);
00129 
00138         inline Matrix<Diagonal,Ref,Ref,AT>& operator=(const Matrix<Diagonal,Ref,Ref,AT> &A);
00139 
00151         const AT& operator()(int i, int j) const {
00152 
00153 #ifndef FMATVEC_NO_BOUNDS_CHECK
00154           assert(i>=0);
00155           assert(j>=0);
00156           assert(i<n);
00157           assert(j<n);
00158 #endif
00159 
00160           return e(i,j);
00161         };
00162 
00167         const AT& operator()(int i) const {
00168 
00169 #ifndef FMATVEC_NO_BOUNDS_CHECK
00170           assert(i>=0);
00171           assert(i<n);
00172 #endif
00173 
00174           return e(i);
00175         };
00176 
00187         AT& operator()(int i) {
00188 
00189 #ifndef FMATVEC_NO_BOUNDS_CHECK
00190           assert(i>=0);
00191           assert(i<n);
00192 #endif
00193 
00194           return e(i);
00195         };
00196 
00197         const AT& e(int i, int j) const {
00198           static AT zero=0;
00199           return i==j ? ele[i] : zero;
00200         };
00201 
00202         const AT& e(int i) const {
00203           return ele[i];
00204         };
00205 
00206         AT& e(int i) {
00207           return ele[i];
00208         };
00209 
00214         const AT* operator()() const {
00215           return ele;
00216         };
00217 
00223         AT* operator()() {
00224           return ele;
00225         };
00226 
00231         int rows() const {return n;};
00232 
00237         int cols() const {return n;};
00238 
00243         int size() const {return n;};
00244 
00252   const CBLAS_ORDER blasOrder() const {
00253     return  CblasColMajor;
00254         };
00255 
00261         Matrix<Diagonal,Ref,Ref,AT> copy() const;
00262 
00270         Matrix<Diagonal,Ref,Ref,AT>& init(const AT &a=0);
00271         inline Matrix<Diagonal,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
00272         inline Matrix<Diagonal,Ref,Ref,AT>& init(Eye, const AT &a=1) { return init(a); }
00273         inline Matrix<Diagonal,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
00274 
00279         operator std::vector<std::vector<AT> >();
00280     };
00281 
00282   template <class AT>
00283     Matrix<Diagonal,Ref,Ref,AT>& Matrix<Diagonal,Ref,Ref,AT>::operator>>(const Matrix<Diagonal,Ref,Ref,AT> &A) { 
00284       n=A.n;
00285       memory = A.memory;
00286       ele = A.ele;
00287 
00288       return *this;
00289     }
00290 
00291   template <class AT>
00292     Matrix<Diagonal,Ref,Ref,AT>& Matrix<Diagonal,Ref,Ref,AT>::operator=(const Matrix<Diagonal,Ref,Ref,AT> &A) { 
00293       if(!ele) {
00294         n = A.n;
00295         memory.resize(n);
00296         ele = (AT*)memory.get();
00297       } 
00298       else {
00299 #ifndef FMATVEC_NO_SIZE_CHECK
00300         assert(n == A.n);
00301 #endif
00302       }
00303 
00304       deepCopy(A);
00305 
00306       return *this;
00307     }
00308 
00309 
00310   template <class AT>
00311     Matrix<Diagonal,Ref,Ref,AT>& Matrix<Diagonal,Ref,Ref,AT>::operator<<(const Matrix<Diagonal,Ref,Ref,AT> &A) { 
00312 
00313       if(n!=A.n) {
00314         n = A.n;
00315         memory.resize(n);
00316         ele = (AT*)memory.get();
00317       } 
00318 
00319       deepCopy(A);
00320 
00321       return *this;
00322     }
00323 
00324   template <class AT>
00325     Matrix<Diagonal,Ref,Ref,AT>&  Matrix<Diagonal,Ref,Ref,AT>::init(const AT &val) {
00326       for(int i=0; i<rows(); i++) 
00327         e(i) = val;
00328       return *this;
00329     }
00330 
00331   template <class AT>
00332     Matrix<Diagonal,Ref,Ref,AT> Matrix<Diagonal,Ref,Ref,AT>::copy() const {
00333 
00334       Matrix<Diagonal,Ref,Ref,AT> A(n);
00335 
00336       A.deepCopy(*this);
00337 
00338       return A;
00339     }
00340 
00341   template <class AT>
00342     void Matrix<Diagonal,Ref,Ref,AT>::deepCopy(const Matrix<Diagonal,Ref,Ref,AT> &A) { 
00343       for(int i=0; i<n; i++)
00344         e(i) = A.e(i);
00345     }
00346 
00347   template <class AT>
00348     Matrix<Diagonal,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() {
00349       std::vector<std::vector<AT> > ret(rows());
00350       for(int r=0; r<rows(); r++) {
00351         ret[r].resize(cols());
00352         for(int c=0; c<cols(); c++)
00353           ret[r][c]=e(r,c);
00354       }
00355       return ret;
00356     }
00357 
00358 }
00359 
00360 #endif