var_symmetric_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 var_symmetric_matrix_h
00023 #define var_symmetric_matrix_h
00024 
00025 #include "types.h"
00026 
00027 namespace fmatvec {
00028 
00037   template <class AT> class Matrix<Symmetric,Var,Var,AT> {
00038 
00039     protected:
00040 
00042 
00043       int M;
00044 
00045       AT *ele;
00046 
00047       template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A); 
00048       template <class Row> inline void deepCopy(const Matrix<Symmetric,Row,Row,AT> &A); 
00049 
00051 
00052     public:
00053 
00058       Matrix() : M(0), ele(0) {
00059       }
00060 
00061 //      template<class Ini=All<AT> >
00062 //        Matrix(int m, Ini ini=All<AT>()) : M(m), ele(new AT[M*M]) { 
00063 //          init(ini);
00064 //        }
00065 //      template<class Ini=All<AT> >
00066 //        Matrix(int m, int n, Ini ini=All<AT>()) : M(m), ele(new AT[M*M]) {
00067 //          init(ini);
00068 //        }
00069 
00070       Matrix(int m, Noinit) : M(m), ele(new AT[M*M]) { }
00071       Matrix(int m, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*M]) { init(a); }
00072       Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
00073       Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*M]) { }
00074       Matrix(int m, int n, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*M]) { init(a); }
00075       Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
00076 
00084       Matrix(const Matrix<Symmetric,Var,Var,AT> &A) : M(A.M), ele(new AT[M*M])  {
00085         deepCopy(A);
00086       }
00087 
00088       template<class Row>
00089       Matrix(const Matrix<Symmetric,Row,Row,AT> &A) : M(A.size()), ele(new AT[M*M])   {
00090         deepCopy(A);
00091       }
00092 
00093       template<int M>
00094       explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,AT>&  A) : M(A.M), ele(new AT[M*M]) {
00095         deepCopy(A);
00096       }
00097 
00098       template<class Type, class Row, class Col>
00099       explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*M])  {
00100 
00101 #ifndef FMATVEC_NO_SIZE_CHECK
00102         assert(A.rows() == M); 
00103         assert(A.cols() == M);
00104 #endif
00105 
00106         deepCopy(A);
00107       }
00108 
00111       ~Matrix() {
00112         delete[] ele;
00113       }
00114 
00115       Matrix<Symmetric,Var,Var,AT>& resize() { 
00116         delete[] ele;
00117         M = 0;
00118         ele = 0;
00119         return *this;
00120       }
00121 
00122       Matrix<Symmetric,Var,Var,AT>& resize(int m, Noinit) { 
00123         delete[] ele;
00124         M = m;
00125         ele = new AT[M*M];
00126         return *this;
00127       }
00128 
00129       Matrix<Symmetric,Var,Var,AT>& resize(int m, Init ini=INIT, const AT &a=0) { return resize(m,Noinit()).init(a); }
00130 
00131       Matrix<Symmetric,Var,Var,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
00132 
00139       inline Matrix<Symmetric,Var,Var,AT>& operator=(const Matrix<Symmetric,Var,Var,AT> &A);
00140 
00141       template<class Type, class Row, class Col>
00142         inline Matrix<Symmetric,Var,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
00143 
00144       template<class Type, class Row, class Col>
00145         inline Matrix<Symmetric,Var,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
00146 
00158       AT& operator()(int i, int j) {
00159 #ifndef FMATVEC_NO_BOUNDS_CHECK
00160         assert(i>=0);
00161         assert(j>=0);
00162         assert(i<M);
00163         assert(j<M);
00164 #endif
00165 
00166         return e(i,j);
00167       };
00168 
00173       const AT& operator()(int i, int j) const {
00174 #ifndef FMATVEC_NO_BOUNDS_CHECK
00175         assert(i>=0);
00176         assert(j>=0);
00177         assert(i<M);
00178         assert(j<M);
00179 #endif
00180 
00181         return e(i,j);
00182       };
00183 
00184       AT& ei(int i, int j) {
00185         return ele[i+j*M];
00186       };
00187 
00188       const AT& ei(int i, int j) const {
00189         return ele[i+j*M];
00190       };
00191 
00192       AT& ej(int i, int j) {
00193         return ele[i*M+j];
00194       };
00195 
00196       const AT& ej(int i, int j) const {
00197         return ele[i*M+j];
00198       };
00199 
00200        AT& e(int i, int j) {
00201         return j > i ? ej(i,j) : ei(i,j);
00202       };
00203 
00204       const AT& e(int i, int j) const {
00205         return j > i ? ej(i,j) : ei(i,j);
00206       };
00207 
00213       AT* operator()() {return ele;};
00214 
00219       const AT* operator()() const {return ele;};
00220 
00225       int size() const {return M;};
00226 
00231       int rows() const {return M;};
00232 
00237       int cols() const {return M;};
00238 
00243       int ldim() const {return M;};
00244 
00252       const CBLAS_ORDER blasOrder() const {
00253         return  CblasColMajor;
00254       };
00255 
00263       const CBLAS_UPLO blasUplo() const {
00264         return  CblasLower;
00265       };
00266 
00274       inline Matrix<Symmetric,Var,Var,AT>& init(const AT &a=0); 
00275       inline Matrix<Symmetric,Var,Var,AT>& init(Init, const AT &a=0) { return init(a); }
00276       inline Matrix<Symmetric,Var,Var,AT>& init(Eye eye, const AT &a=1);
00277       inline Matrix<Symmetric,Var,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
00278 
00283       inline operator std::vector<std::vector<AT> >();
00284   };
00285 
00286   template <class AT>
00287     inline Matrix<Symmetric,Var,Var,AT>& Matrix<Symmetric,Var,Var,AT>::operator=(const Matrix<Symmetric,Var,Var,AT> &A) { 
00288 
00289       if(!ele) {
00290         delete[] ele;
00291         M = A.size(); 
00292         ele = new AT[M*M];
00293       } else {
00294 #ifndef FMATVEC_NO_SIZE_CHECK
00295         assert(M == A.size());
00296 #endif
00297       }
00298 
00299       deepCopy(A);
00300 
00301       return *this;
00302     }
00303 
00304   template <class AT> template <class Type, class Row, class Col>
00305     inline Matrix<Symmetric,Var,Var,AT>& Matrix<Symmetric,Var,Var,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) { 
00306 
00307       if(!ele) {
00308         delete[] ele;
00309         M = A.rows(); 
00310         ele = new AT[M*M];
00311       } else {
00312 #ifndef FMATVEC_NO_SIZE_CHECK
00313         assert(A.rows() == A.cols());
00314         assert(M == A.rows());
00315 #endif
00316       }
00317 
00318       deepCopy(A);
00319 
00320       return *this;
00321     }
00322 
00323   template <class AT> template <class Type, class Row, class Col>
00324     inline Matrix<Symmetric,Var,Var,AT>& Matrix<Symmetric,Var,Var,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) { 
00325 
00326 #ifndef FMATVEC_NO_SIZE_CHECK
00327       assert(A.rows() == A.cols());
00328 #endif
00329 
00330       if(M!=A.rows()) {
00331         delete[] ele;
00332         M = A.rows(); 
00333         ele = new AT[M*M];
00334       } 
00335 
00336       deepCopy(A);
00337 
00338       return *this;
00339     }
00340 
00341   template <class AT>
00342     inline Matrix<Symmetric,Var,Var,AT>&  Matrix<Symmetric,Var,Var,AT>::init(const AT &val) {
00343 
00344       for(int i=0; i<M; i++) 
00345         for(int j=i; j<M; j++) 
00346           ej(i,j) = val;
00347 
00348       return *this;
00349     }
00350 
00351   template <class AT>
00352     inline Matrix<Symmetric,Var,Var,AT>&  Matrix<Symmetric,Var,Var,AT>::init(Eye, const AT &val) {
00353 
00354       for(int i=0; i<size(); i++) {
00355         ej(i,i) = val;
00356         for(int j=i+1; j<size(); j++) {
00357           ej(i,j) = 0; 
00358         }
00359       }
00360       return *this;
00361     }
00362 
00363   template <class AT>
00364     inline Matrix<Symmetric,Var,Var,AT>::operator std::vector<std::vector<AT> >() {
00365       std::vector<std::vector<AT> > ret(rows());
00366       for(int r=0; r<rows(); r++) {
00367         ret[r].resize(cols());
00368         for(int c=0; c<cols(); c++)
00369           ret[r][c]=operator()(r,c);
00370       }
00371       return ret;
00372     }
00373 
00375 
00376   template <class AT> template <class Type, class Row, class Col>
00377     inline void Matrix<Symmetric,Var,Var,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) { 
00378       for(int i=0; i<M; i++) 
00379         for(int j=i; j<M; j++) 
00380           ej(i,j) = A.e(i,j);
00381     }
00382 
00383   template <class AT> template <class Row>
00384     inline void Matrix<Symmetric,Var,Var,AT>::deepCopy(const Matrix<Symmetric,Row,Row,AT> &A) { 
00385       for(int i=0; i<M; i++) 
00386         for(int j=i; j<M; j++) 
00387           ej(i,j) = A.ej(i,j);
00388     }
00389 
00391 
00392 }
00393 
00394 #endif