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 symmetric_matrix_h
00023 #define symmetric_matrix_h
00024 
00025 #include "types.h"
00026 #include "matrix.h"
00027 #include "_memory.h"
00028 
00029 namespace fmatvec {
00030 
00039   template <class AT> class Matrix<Symmetric,Ref,Ref,AT> {
00040 
00041     protected:
00042 
00044 
00045       Memory<AT> memory;
00046       AT *ele;
00047       int n;
00048       int lda;
00049 
00050       template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A); 
00051       inline void deepCopy(const Matrix<Symmetric,Ref,Ref,AT> &A); 
00052 
00053       const AT* elePtr(int i, int j) const {
00054         return  j > i ? ele+i*lda+j : ele+i+j*lda; 
00055       };
00056 
00057       AT* elePtr(int i, int j) {
00058         return  j > i ? ele+i*lda+j : ele+i+j*lda; 
00059       };
00060 
00061       Matrix(int n_, int lda_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), n(n_), lda(lda_) {
00062       }
00063 
00065 
00066     public:
00067 
00072       Matrix() : memory(), ele(0), n(0), lda(0) {
00073       }
00074 
00075 //      template<class Ini=All<AT> >
00076 //        Matrix(int n_, Ini ini=All<AT>()) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) {
00077 //          init(ini);
00078 //        }
00079 //      template<class Ini=All<AT> >
00080 //        Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) {
00081 //          init(ini);
00082 //        }
00083 
00084       Matrix(int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
00085       Matrix(int n_, Init ini=INIT, const AT &a=0) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
00086       Matrix(int n_, Eye ini, const AT &a=1) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(ini,a); }
00087       Matrix(int m_, int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
00088       Matrix(int m_, int n_, Init ini=INIT, const AT &a=0) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
00089       Matrix(int m_, int n_, Eye ini, const AT &a=1) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(ini,a); }
00090 
00098       Matrix(const Matrix<Symmetric,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) , n(A.n), lda(A.lda) { }
00099 
00100 
00105       explicit Matrix(const Matrix<General,Ref,Ref,AT>&  A) : memory(A.memory), ele(A.ele) , n(A.n), lda(A.lda) {
00106 #ifndef FMATVEC_NO_SIZE_CHECK
00107         assert(A.rows() == A.cols());
00108 #endif
00109       }
00110 
00111       template<class Type, class Row, class Col>
00112       explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : memory(A.rows()*A.cols()), ele((AT*)memory.get()), n(A.cols()), lda(A.cols()) {
00113 #ifndef FMATVEC_NO_SIZE_CHECK
00114         assert(A.rows() == A.cols());
00115 #endif
00116 
00117         deepCopy(A);
00118       }
00119 
00127       Matrix(int n_, AT* ele_) : memory(), ele(ele_), n(n_), lda(n_) { }
00128 
00131       ~Matrix() { }
00132 
00133       Matrix<Symmetric,Ref,Ref,AT>& resize() { 
00134         n = lda = 0;
00135         memory.resize(0);
00136         ele = 0;
00137         return *this;
00138       }
00139 
00140       Matrix<Symmetric,Ref,Ref,AT>& resize(int n_, Noinit) { 
00141         n = n_; lda = n;
00142         memory.resize(n*n);
00143         ele = (AT*)memory.get();
00144         return *this;
00145       }
00146 
00147       Matrix<Symmetric,Ref,Ref,AT>& resize(int n, Init ini=INIT, const AT &a=0) { return resize(n,Noinit()).init(a); }
00148 
00149       Matrix<Symmetric,Ref,Ref,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
00150 
00159       inline Matrix<Symmetric,Ref,Ref,AT>& operator=(const Matrix<Symmetric,Ref,Ref,AT> &A);
00160 
00161       template<class Type, class Row, class Col>
00162         inline Matrix<Symmetric,Ref,Ref,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
00163 
00170       template<class Type, class Row, class Col>
00171         inline Matrix<Symmetric,Ref,Ref,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
00172 
00179       inline Matrix<Symmetric,Ref,Ref,AT>& operator>>(const Matrix<Symmetric,Ref,Ref,AT> &A);
00180 
00192       AT& operator()(int i, int j) {
00193 #ifndef FMATVEC_NO_BOUNDS_CHECK
00194         assert(i>=0);
00195         assert(j>=0);
00196         assert(i<n);
00197         assert(j<n);
00198 #endif
00199         return e(i,j);
00200       };
00201 
00206       const AT& operator()(int i, int j) const {
00207 #ifndef FMATVEC_NO_BOUNDS_CHECK
00208         assert(i>=0);
00209         assert(j>=0);
00210         assert(i<n);
00211         assert(j<n);
00212 #endif
00213 
00214         return e(i,j);
00215       };
00216 
00217       AT& ei(int i, int j) {
00218         return ele[i+j*lda];
00219       };
00220 
00221       const AT& ei(int i, int j) const {
00222         return ele[i+j*lda];
00223       };
00224 
00225       AT& ej(int i, int j) {
00226         return ele[i*lda+j];
00227       };
00228 
00229       const AT& ej(int i, int j) const {
00230         return ele[i*lda+j];
00231       };
00232 
00233       AT& e(int i, int j) {
00234         return j > i ? ej(i,j) : ei(i,j);
00235       };
00236 
00237       const AT& e(int i, int j) const {
00238         return j > i ? ej(i,j) : ei(i,j);
00239       };
00240 
00246       AT* operator()() {return ele;};
00247 
00252       const AT* operator()() const {return ele;};
00253 
00258       int size() const {return n;};
00259 
00264       int rows() const {return n;};
00265 
00270       int cols() const {return n;};
00271 
00276       int ldim() const {return lda;};
00277 
00285       const CBLAS_ORDER blasOrder() const {
00286         return  CblasColMajor;
00287       };
00288 
00296       const CBLAS_UPLO blasUplo() const {
00297         return  CblasLower;
00298       };
00299 
00305       inline Matrix<Symmetric,Ref,Ref,AT> copy() const;
00306 
00314       inline Matrix<Symmetric,Ref,Ref,AT>& init(const AT &a); 
00315       inline Matrix<Symmetric,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); };
00316       inline Matrix<Symmetric,Ref,Ref,AT>& init(Eye, const AT &a=1);
00317       inline Matrix<Symmetric,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
00318 
00330       inline Matrix<General,Ref,Ref,AT> operator()(const Index &I, const Index &J);
00331 
00336       inline const Matrix<General,Ref,Ref,AT> operator()(const Index &I, const Index &J) const;
00337 
00349       inline Matrix<Symmetric,Ref,Ref,AT> operator()(const Index &I);
00350 
00355       inline const Matrix<Symmetric,Ref,Ref,AT> operator()(const Index &I) const;
00356 
00368       inline Matrix<General,Ref,Ref,AT> operator()(int i1, int j1, int i2, int j2);
00369 
00374       inline const Matrix<General,Ref,Ref,AT> operator()(int i1, int j1, int i2, int j2) const;
00375 
00380       inline operator std::vector<std::vector<AT> >();
00381   };
00382 
00383   template <class AT>
00384     inline Matrix<Symmetric,Ref,Ref,AT>& Matrix<Symmetric,Ref,Ref,AT>::operator>>(const Matrix<Symmetric,Ref,Ref,AT> &A) { 
00385 
00386       n=A.n;
00387       memory = A.memory;
00388       ele = A.ele;
00389       lda = A.lda;
00390 
00391       return *this;
00392     }
00393 
00394   template <class AT>
00395     inline Matrix<Symmetric,Ref,Ref,AT>& Matrix<Symmetric,Ref,Ref,AT>::operator=(const Matrix<Symmetric,Ref,Ref,AT> &A) { 
00396 
00397       if(!ele) {
00398         n = A.size();
00399         lda = n;
00400         memory.resize(n*n);
00401         ele = (AT*)memory.get();
00402       } else {
00403 #ifndef FMATVEC_NO_SIZE_CHECK
00404         assert(n == A.size());
00405 #endif
00406       }
00407 
00408       deepCopy(A);
00409 
00410       return *this;
00411     }
00412 
00413   template <class AT> template< class Type, class Row, class Col>
00414     inline Matrix<Symmetric,Ref,Ref,AT>& Matrix<Symmetric,Ref,Ref,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) { 
00415       
00416       if(!ele) {
00417         n = A.rows();
00418         lda = n;
00419         memory.resize(n*n);
00420         ele = (AT*)memory.get();
00421       } else {
00422 #ifndef FMATVEC_NO_SIZE_CHECK
00423         assert(A.rows() == A.cols());
00424         assert(n == A.rows());
00425 #endif
00426       }
00427 
00428       deepCopy(A);
00429 
00430       return *this;
00431     }
00432 
00433 
00434   template <class AT> template< class Type, class Row, class Col>
00435     inline Matrix<Symmetric,Ref,Ref,AT>& Matrix<Symmetric,Ref,Ref,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) { 
00436       
00437 #ifndef FMATVEC_NO_SIZE_CHECK
00438       assert(A.rows() == A.cols());
00439 #endif
00440 
00441       if(n!=A.rows()) {
00442         n = A.rows();
00443         lda = n;
00444         memory.resize(n*n);
00445         ele = (AT*)memory.get();
00446       } 
00447 
00448       deepCopy(A);
00449 
00450       return *this;
00451     }
00452 
00453   template <class AT>
00454     inline Matrix<Symmetric,Ref,Ref,AT>&  Matrix<Symmetric,Ref,Ref,AT>::init(const AT &val) {
00455 
00456       for(int i=0; i<rows(); i++) 
00457         for(int j=i; j<cols(); j++) 
00458           ej(i,j) = val;
00459 
00460       return *this;
00461     }
00462 
00463   template <class AT>
00464     inline Matrix<Symmetric,Ref,Ref,AT>&  Matrix<Symmetric,Ref,Ref,AT>::init(Eye, const AT &val) {
00465 
00466       for(int i=0; i<size(); i++) {
00467         ej(i,i) = val;
00468         for(int j=i+1; j<size(); j++) {
00469           ej(i,j) = 0; 
00470         }
00471       }
00472       return *this;
00473     }
00474 
00475   template <class AT>
00476     inline Matrix<Symmetric,Ref,Ref,AT> Matrix<Symmetric,Ref,Ref,AT>::copy() const {
00477 
00478       Matrix<Symmetric,Ref,Ref,AT> A(n,NONINIT);
00479       A.deepCopy(*this);
00480 
00481       return A;
00482     }
00483 
00484 
00485   template <class AT> 
00486     inline const Matrix<General,Ref,Ref,AT> Matrix<Symmetric,Ref,Ref,AT>::operator()(int i1, int j1, int i2, int j2) const {
00487       return operator()(Index(i1,i2),Index(j1,j2));
00488     }
00489 
00490   template <class AT> 
00491     inline Matrix<General,Ref,Ref,AT> Matrix<Symmetric,Ref,Ref,AT>::operator()(int i1, int j1, int i2, int j2) {
00492       return operator()(Index(i1,i2),Index(j1,j2));
00493     }
00494 
00495   template <class AT> 
00496     inline const Matrix<General,Ref,Ref,AT> Matrix<Symmetric,Ref,Ref,AT>::operator()(const Index &I, const Index &J) const {
00497 #ifndef FMATVEC_NO_BOUNDS_CHECK
00498       assert(I.end()<n);
00499       assert(J.end()<n);
00500 #endif
00501 
00502       if(I.start() >= J.start()) {
00503         assert(J.end() <= I.start());
00504         return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,false,memory,elePtr(I.start(),J.start()));
00505       } else {
00506         assert(I.end() <= J.start());
00507         return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,true,memory,elePtr(J.start(),I.start()));
00508       }
00509     }
00510 
00511   template <class AT> 
00512     inline Matrix<General,Ref,Ref,AT> Matrix<Symmetric,Ref,Ref,AT>::operator()(const Index &I, const Index &J) {
00513 #ifndef FMATVEC_NO_BOUNDS_CHECK
00514       assert(I.end()<n);
00515       assert(J.end()<n);
00516 #endif
00517 
00518       if(I.start() >= J.start()) {
00519         assert(J.end() <= I.start());
00520         return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,false,memory,elePtr(I.start(),J.start()));
00521       } else {
00522         assert(I.end() <= J.start());
00523         return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,true,memory,elePtr(J.start(),I.start()));
00524       }
00525     }
00526 
00527   template <class AT> 
00528     inline Matrix<Symmetric,Ref,Ref,AT> Matrix<Symmetric,Ref,Ref,AT>::operator()(const Index &I) {
00529 #ifndef FMATVEC_NO_BOUNDS_CHECK
00530       assert(I.end()<n);
00531 #endif
00532 
00533       return Matrix<Symmetric,Ref,Ref,AT>(I.end()-I.start()+1,lda,memory,elePtr(I.start(),I.start()));
00534     }
00535 
00536   template <class AT> 
00537     inline const Matrix<Symmetric,Ref,Ref,AT> Matrix<Symmetric,Ref,Ref,AT>::operator()(const Index &I) const {
00538 #ifndef FMATVEC_NO_BOUNDS_CHECK
00539       assert(I.end()<n);
00540 #endif
00541 
00542       return Matrix<Symmetric,Ref,Ref,AT>(I.end()-I.start()+1,lda,memory,elePtr(I.start(),I.start()));
00543     }
00544 
00545   template <class AT>
00546     inline Matrix<Symmetric,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() {
00547       std::vector<std::vector<AT> > ret(rows());
00548       for(int r=0; r<rows(); r++) {
00549         ret[r].resize(cols());
00550         for(int c=0; c<cols(); c++)
00551           ret[r][c]=operator()(r,c);
00552       }
00553       return ret;
00554     }
00555 
00557 
00558   template <class AT>
00559     inline void Matrix<Symmetric,Ref,Ref,AT>::deepCopy(const Matrix<Symmetric,Ref,Ref,AT> &A) { 
00560       for(int i=0; i<n; i++) 
00561         for(int j=i; j<n; j++) 
00562           ej(i,j) = A.ej(i,j);
00563     }
00564 
00565   template <class AT> template <class Type, class Row, class Col>
00566     inline void Matrix<Symmetric,Ref,Ref,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) { 
00567       for(int i=0; i<n; i++) 
00568         for(int j=i; j<n; j++) 
00569           ej(i,j) = A.e(i,j);
00570     }
00571 
00573 
00574 }
00575 
00576 #endif