fixed_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 fixed_symmetric_matrix_h
00023 #define fixed_symmetric_matrix_h
00024 
00025 #include "types.h"
00026 
00027 namespace fmatvec {
00028 
00037   template <int M, class AT> class Matrix<Symmetric,Fixed<M>,Fixed<M>,AT> {
00038 
00039     protected:
00040 
00042 
00043       AT ele[M][M];
00044 
00045       template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A); 
00046       template <class Row> inline void deepCopy(const Matrix<Symmetric,Row,Row,AT> &A); 
00047 
00049 
00050     public:
00051 
00052 //      template<class Ini=All<AT> >
00053 //      Matrix(Ini ini=All<AT>()) {
00054 //        init(ini);
00055 //      }
00056 //      template<class Ini=All<AT> >
00057 //      Matrix(int m_, int n_, Ini ini=All<AT>()) {
00058 //        init(ini);
00059 //      }
00060 
00061       Matrix(Noinit ini) { }
00062       Matrix(Init ini=INIT, const AT &a=0) { init(a); }
00063       Matrix(Eye ini, const AT &a=1) { init(ini,a); }
00064       Matrix(int m, int n, Noinit ini) { }
00065       Matrix(int m, int n, Init ini, const AT &a=0) { init(a); }
00066       Matrix(int m, int n, Eye ini, const AT &a=1) { init(ini,a); }
00067 
00068       explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,AT>&  A) {
00069         deepCopy(A);
00070       }
00071 
00072       template<class Row>
00073       Matrix(const Matrix<Symmetric,Row,Row,AT> &A) {
00074 
00075 #ifndef FMATVEC_NO_SIZE_CHECK
00076         assert(A.size() == M); 
00077 #endif
00078 
00079         deepCopy(A);
00080       }
00081 
00082       template<class Type, class Row, class Col>
00083       explicit Matrix(const Matrix<Type,Row,Col,AT> &A) {
00084 
00085 #ifndef FMATVEC_NO_SIZE_CHECK
00086         assert(A.rows() == M); 
00087         assert(A.cols() == M);
00088 #endif
00089 
00090         deepCopy(A);
00091       }
00092 
00104       AT& operator()(int i, int j) {
00105 #ifndef FMATVEC_NO_BOUNDS_CHECK
00106         assert(i>=0);
00107         assert(j>=0);
00108         assert(i<M);
00109         assert(j<M);
00110 #endif
00111 
00112         return e(i,j);
00113       };
00114 
00119       const AT& operator()(int i, int j) const {
00120 #ifndef FMATVEC_NO_BOUNDS_CHECK
00121         assert(i>=0);
00122         assert(j>=0);
00123         assert(i<M);
00124         assert(j<M);
00125 #endif
00126 
00127         return e(i,j);
00128       };
00129 
00130       AT& ei(int i, int j) {
00131         return ele[i][j];
00132       };
00133 
00134       const AT& ei(int i, int j) const {
00135         return ele[i][j];
00136       };
00137 
00138       AT& ej(int i, int j) {
00139         return ele[j][i];
00140       };
00141 
00142       const AT& ej(int i, int j) const {
00143         return ele[j][i];
00144       };
00145 
00146        AT& e(int i, int j) {
00147         return j > i ? ej(i,j) : ei(i,j);
00148       };
00149 
00150       const AT& e(int i, int j) const {
00151         return j > i ? ej(i,j) : ei(i,j);
00152       };
00153 
00159       AT* operator()() {return ele;};
00160 
00165       const AT* operator()() const {return ele;};
00166 
00171       int size() const {return M;};
00172 
00177       int rows() const {return M;};
00178 
00183       int cols() const {return M;};
00184 
00189       int ldim() const {return M;};
00190 
00198       const CBLAS_ORDER blasOrder() const {
00199         return  CblasColMajor;
00200       };
00201 
00209       const CBLAS_UPLO blasUplo() const {
00210         return  CblasLower;
00211       };
00212 
00220       inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(const AT &a=0); 
00221       inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Init, const AT &a=0) { return init(a); }
00222       inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Eye, const AT &a=1);
00223       inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Noinit, const AT &a=0) { return *this; }
00224 
00229       inline operator std::vector<std::vector<AT> >();
00230   };
00231 
00232   template <int M, class AT>
00233     inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>&  Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::init(const AT &val) {
00234 
00235       for(int i=0; i<M; i++) 
00236         for(int j=i; j<M; j++) 
00237           ej(i,j) = val;
00238 
00239       return *this;
00240     }
00241 
00242   template <int M, class AT>
00243     inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>&  Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::init(Eye, const AT &val) {
00244 
00245       for(int i=0; i<size(); i++) {
00246         ej(i,i) = val;
00247         for(int j=i+1; j<size(); j++) {
00248           ej(i,j) = 0; 
00249         }
00250       }
00251       return *this;
00252     }
00253 
00254   template <int M, class AT>
00255     inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::operator std::vector<std::vector<AT> >() {
00256       std::vector<std::vector<AT> > ret(rows());
00257       for(int r=0; r<rows(); r++) {
00258         ret[r].resize(cols());
00259         for(int c=0; c<cols(); c++)
00260           ret[r][c]=operator()(r,c);
00261       }
00262       return ret;
00263     }
00264 
00266 
00267   template <int M, class AT> template <class Type, class Row, class Col>
00268     inline void Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) { 
00269       for(int i=0; i<M; i++) 
00270         for(int j=i; j<M; j++)
00271           ej(i,j) = A.e(i,j);
00272     }
00273 
00274   template <int M, class AT> template <class Row>
00275     inline void Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::deepCopy(const Matrix<Symmetric,Row,Row,AT> &A) { 
00276       for(int i=0; i<M; i++) 
00277         for(int j=i; j<M; j++)
00278           ej(i,j) = A.ej(i,j);
00279     }
00280 
00282 
00283 }
00284 
00285 #endif