general_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_matrix_h
00023 #define general_matrix_h
00024 
00025 #include "_memory.h"
00026 #include "types.h"
00027 #include "matrix.h"
00028 #include <stdlib.h>
00029 
00030 namespace fmatvec {
00031 
00040   template <class AT> class Matrix<General,Ref,Ref,AT> {
00041 
00042     public:
00043 
00045 
00046       friend class Vector<Ref,AT>;
00047       friend class RowVector<Ref,AT>;
00048       friend class SquareMatrix<Ref,AT>;
00049       friend class Matrix<Symmetric,Ref,Ref,AT>;
00050       
00051       template <class T> friend Matrix<General,Ref,Ref,T> trans(const Matrix<General,Ref,Ref,T> &A);
00052 
00053     protected:
00054 
00055       Memory<AT> memory;
00056       AT *ele;
00057       int m;
00058       int n;
00059       int lda;
00060       bool tp;
00061 
00062       template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A); 
00063       inline void deepCopy(const Matrix<General,Ref,Ref,AT> &A); 
00064       inline void deepCopy(const Matrix<Symmetric,Ref,Ref,AT> &A); 
00065 
00066       const AT* elePtr(int i, int j) const {
00067         return tp ? ele+i*lda+j : ele+i+j*lda; 
00068       };
00069 
00070       AT* elePtr(int i, int j) {
00071         return tp ? ele+i*lda+j : ele+i+j*lda; 
00072       };
00073 
00074       Matrix(int m_, int n_, int lda_, bool tp_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), m(m_), n(n_), lda(lda_), tp(tp_) {
00075       }
00076 
00078  
00079     public:
00080 
00085       Matrix() : memory(), ele(0), m(0), n(0), lda(0), tp(false) { }
00086 
00087 // Works with -std=gnu++0x only
00088 //      template<class Ini=All<AT> >
00089 //      Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_), tp(false) {
00090 //        init(ini);
00091 //      }
00092 
00093       Matrix(int m_, int n_, Noinit) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_), tp(false) { }
00094       Matrix(int m_, int n_, Init ini=INIT, const AT &a=0) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_), tp(false) { init0(a); }
00095       Matrix(int m_, int n_, Eye ini, const AT &a=1) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_), tp(false) { init(ini,a); }
00096 
00104       Matrix(const Matrix<General,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) ,m(A.m), n(A.n), lda(A.lda), tp(A.tp) {
00105       }
00106 
00114       Matrix(int m_, int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_), tp(false) { 
00115       }
00116 
00129       Matrix(const char *str);
00130 
00133       ~Matrix() {
00134       }
00135 
00136       template<class Type, class Row, class Col>
00137       explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : memory(A.rows()*A.cols()), ele((AT*)memory.get()), m(A.rows()), n(A.cols()), lda(m), tp(false) {
00138 
00139         deepCopy(A);
00140       }
00141 
00142       Matrix<General,Ref,Ref,AT>& resize() {
00143         m = n = lda = 0;
00144         tp = false;
00145         memory.resize(0);
00146         ele = 0;
00147         return *this;
00148       }
00149        
00150       Matrix<General,Ref,Ref,AT>& resize(int m_, int n_, Noinit) {
00151         m = m_; n = n_; lda = m;
00152         tp = false;
00153         memory.resize(m*n);
00154         ele = (AT*)memory.get();
00155         return *this;
00156       }
00157 
00158       Matrix<General,Ref,Ref,AT>& resize(int m, int n, Init ini=INIT, const AT &a=0) { return resize(m,n,Noinit()).init0(a); }
00159 
00160       Matrix<General,Ref,Ref,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); } 
00161 
00170       inline Matrix<General,Ref,Ref,AT>& operator=(const Matrix<General,Ref,Ref,AT> &A);
00171 
00172       template<class Type, class Row, class Col>
00173       Matrix<General,Ref,Ref,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
00174 
00181       template<class Type, class Row, class Col>
00182         inline Matrix<General,Ref,Ref,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
00183 
00190       inline Matrix<General,Ref,Ref,AT>& operator>>(const Matrix<General,Ref,Ref,AT> &A);
00191 
00203       AT& operator()(int i, int j) {
00204 #ifndef FMATVEC_NO_BOUNDS_CHECK
00205         assert(i>=0);
00206         assert(j>=0);
00207         assert(i<m);
00208         assert(j<n);
00209 #endif
00210 
00211         return e(i,j);
00212       };
00213 
00218       const AT& operator()(int i, int j) const {
00219 #ifndef FMATVEC_NO_BOUNDS_CHECK
00220         assert(i>=0);
00221         assert(j>=0);
00222         assert(i<m);
00223         assert(j<n);
00224 #endif
00225 
00226         return e(i,j);
00227       };
00228 
00229       AT& er(int i, int j) {
00230         return ele[i+j*lda];
00231       };
00232 
00233       const AT& er(int i, int j) const {
00234         return ele[i+j*lda];
00235       };
00236 
00237       AT& et(int i, int j) {
00238         return ele[i*lda+j];
00239       };
00240 
00241       const AT& et(int i, int j) const {
00242         return ele[i*lda+j];
00243       };
00244 
00245       AT& e(int i, int j) {
00246         return tp ? et(i,j) : er(i,j);
00247       };
00248 
00249       const AT& e(int i, int j) const {
00250         return tp ? et(i,j) : er(i,j);
00251       };
00252 
00258       AT* operator()() {return ele;};
00259 
00264       const AT* operator()() const {return ele;};
00265 
00270       int rows() const {return m;};
00271 
00276       int cols() const {return n;};
00277 
00282       int ldim() const {return lda;};
00283 
00289       bool transposed() const {return tp;};
00290 
00297       const CBLAS_TRANSPOSE blasTrans() const {
00298   return (tp)? CblasTrans : CblasNoTrans;
00299       };
00300 
00308       const CBLAS_ORDER blasOrder() const {
00309         return  CblasColMajor;
00310       };
00311 
00323       inline Matrix<General,Ref,Ref,AT> operator()(int i1, int j1, int i2, int j2);
00324 
00329       inline const Matrix<General,Ref,Ref,AT> operator()(int i1, int j1, int i2, int j2) const;
00330 
00357       inline Matrix<General,Ref,Ref,AT> operator()(const Index &I, const Index &J);
00358 
00363       inline const Matrix<General,Ref,Ref,AT> operator()(const Index &I, const Index &J) const;
00364 
00373       inline SquareMatrix<Ref,AT> operator() (const Index &I);
00374 
00379       inline const SquareMatrix<Ref,AT> operator()(const Index &I) const;
00380 
00388       inline Vector<Ref,AT> col(int i);
00389 
00394       inline const Vector<Ref,AT> col(int i) const;
00395 
00403       inline RowVector<Ref,AT> row(int i); 
00404 
00409       inline const RowVector<Ref,AT> row(int i) const;
00410 
00416       inline Matrix<General,Ref,Ref,AT> copy() const;
00417 
00425       inline Matrix<General,Ref,Ref,AT>& init(const AT &a=0);
00426       inline Matrix<General,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
00427       inline Matrix<General,Ref,Ref,AT>& init(Eye, const AT &a=1);
00428       inline Matrix<General,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
00429       inline Matrix<General,Ref,Ref,AT>& init0(const AT &a=0);
00430       inline Matrix<General,Ref,Ref,AT>& init0(Init, const AT &a=0) { return init0(a); }
00431 
00436       inline operator std::vector<std::vector<AT> >();
00437 
00443       inline Matrix(std::vector<std::vector<AT> > m);
00444 
00445       Matrix<General,Ref,Ref,AT> T() {
00446         return Matrix<General,Ref,Ref,AT>(n,m,lda,tp?false:true,memory,ele);
00447       };
00448 
00449       const Matrix<General,Ref,Ref,AT> T() const {
00450         return Matrix<General,Ref,Ref,AT>(n,m,lda,tp?false:true,memory,ele);
00451       }
00452   };
00453 
00454   template <class AT> 
00455     Matrix<General,Ref,Ref,AT>::Matrix(const char *strs) : memory(), ele(0), m(0), n(0), lda(0) {
00456       // if 'strs' is a single scalar, surround it first with '[' and ']'.
00457       // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
00458       // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
00459       std::istringstream iss(strs);
00460       char c;
00461       iss>>c;
00462       if(c=='[') iss.str(strs);
00463       else iss.str(std::string("[")+strs+"]");
00464 
00465       int buf=0;
00466       iss >> c;
00467       iss >> c;
00468       if(c!=']') {
00469         iss.putback(c);
00470         AT x;
00471         do {
00472           iss >> x;
00473           iss >> c;
00474           if(c==';') {
00475             if(buf)
00476               assert(buf == n);
00477 
00478             buf=n;
00479             n=0;
00480             m++;
00481           }
00482           else if(c==',')
00483             n++;
00484           c='0';
00485         } while(iss);
00486 
00487         n++; m++;
00488         lda=m;
00489         tp = false;
00490         memory.resize(m*n);
00491         ele = (AT*)memory.get();
00492         iss.clear();
00493         iss.seekg(0);
00494         iss >> c;
00495         for(int i=0; i<m; i++)
00496           for(int j=0; j<n; j++) {
00497             iss >> er(i,j); 
00498             iss >> c;
00499           }
00500       }
00501     }
00502 
00503   template <class AT>
00504     inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::operator>>(const Matrix<General,Ref,Ref,AT> &A) { 
00505 
00506       m=A.m; 
00507       n=A.n;
00508       memory = A.memory;
00509       ele = A.ele;
00510       lda = A.lda;
00511       tp = A.tp; 
00512 
00513       return *this;
00514     }
00515 
00516   template <class AT>
00517     inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::operator=(const Matrix<General,Ref,Ref,AT> &A) { 
00518 
00519       if(!ele) {
00520         m = A.rows(); 
00521         n = A.cols();
00522         lda = m;
00523         tp = false;
00524         memory.resize(m*n);
00525         ele = (AT*)memory.get();
00526       } else {
00527 #ifndef FMATVEC_NO_SIZE_CHECK
00528         assert(m == A.rows());
00529         assert(n == A.cols());
00530 #endif
00531       }
00532 
00533       deepCopy(A);
00534 
00535       return *this;
00536     }
00537 
00538   template <class AT> template< class Type, class Row, class Col>
00539     inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) { 
00540 
00541       if(!ele) {
00542         m = A.rows(); 
00543         n = A.cols();
00544         lda = m;
00545         tp = false;
00546         memory.resize(m*n);
00547         ele = (AT*)memory.get();
00548       } else {
00549 #ifndef FMATVEC_NO_SIZE_CHECK
00550         assert(m == A.rows());
00551         assert(n == A.cols());
00552 #endif
00553       }
00554 
00555       deepCopy(A);
00556 
00557       return *this;
00558     }
00559 
00560   template <class AT> template< class Type, class Row, class Col>
00561     inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) { 
00562 
00563       if(m!=A.rows() || n!=A.cols()) {
00564         m = A.rows(); 
00565         n = A.cols();
00566         lda = m;
00567         tp = false;
00568         memory.resize(m*n);
00569         ele = (AT*)memory.get();
00570       }
00571 
00572       deepCopy(A);
00573 
00574       return *this;
00575     }
00576 
00577   template <class AT>
00578     inline Matrix<General,Ref,Ref,AT>&  Matrix<General,Ref,Ref,AT>::init0(const AT &val) {
00579       for(int i=0; i<m*n; i++) ele[i]=val;
00580       return *this;
00581     }
00582 
00583   template <class AT>
00584     inline Matrix<General,Ref,Ref,AT>&  Matrix<General,Ref,Ref,AT>::init(const AT &val) {
00585 
00586       if(tp) {
00587         for(int i=0; i<rows(); i++) 
00588           for(int j=0; j<cols(); j++) 
00589             et(i,j) = val; 
00590       }
00591       else {
00592         for(int i=0; i<rows(); i++) 
00593           for(int j=0; j<cols(); j++) 
00594             er(i,j) = val; 
00595       }
00596 
00597       return *this;
00598     }
00599 
00600   template <class AT>
00601     inline Matrix<General,Ref,Ref,AT>&  Matrix<General,Ref,Ref,AT>::init(Eye, const AT &val) {
00602       if(tp)
00603         for(int i=0; i<m; i++)
00604           for(int j=0; j<n; j++)
00605             et(i,j) = (i==j) ? val : 0;
00606       else 
00607         for(int i=0; i<m; i++)
00608           for(int j=0; j<n; j++)
00609             er(i,j) = (i==j) ? val : 0;
00610       return *this;
00611     }
00612 
00613   template <class AT>
00614     inline Matrix<General,Ref,Ref,AT>  Matrix<General,Ref,Ref,AT>::operator()(int i1, int j1, int i2, int j2) {
00615       return operator()(Index(i1,i2),Index(j1,j2));
00616     }
00617 
00618   template <class AT>
00619     inline const Matrix<General,Ref,Ref,AT>  Matrix<General,Ref,Ref,AT>::operator()(int i1, int j1, int i2, int j2) const {
00620       return operator()(Index(i1,i2),Index(j1,j2));
00621     }
00622 
00623   template <class AT> 
00624     inline Matrix<General,Ref,Ref,AT> Matrix<General,Ref,Ref,AT>::operator()(const Index &I, const Index &J) {
00625 #ifndef FMATVEC_NO_BOUNDS_CHECK
00626       assert(I.end()<m);
00627       assert(J.end()<n);
00628 #endif
00629       return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,tp,memory,elePtr(I.start(),J.start()));
00630     }
00631 
00632   template <class AT> 
00633     inline const Matrix<General,Ref,Ref,AT> Matrix<General,Ref,Ref,AT>::operator()(const Index &I, const Index &J) const {
00634 #ifndef FMATVEC_NO_BOUNDS_CHECK
00635       assert(I.end()<m);
00636       assert(J.end()<n);
00637 #endif
00638       return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,tp,memory,elePtr(I.start(),J.start()));
00639     }
00640 
00641   template <class AT> 
00642     inline const SquareMatrix<Ref,AT> Matrix<General,Ref,Ref,AT>::operator()(const Index &I) const {
00643 #ifndef FMATVEC_NO_BOUNDS_CHECK
00644       assert(I.end()<m);
00645 #endif
00646       return SquareMatrix<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start(),I.start()));
00647     }
00648 
00649   template <class AT>
00650     inline SquareMatrix<Ref,AT> Matrix<General,Ref,Ref,AT>::operator()(const Index &I) {
00651 #ifndef FMATVEC_NO_BOUNDS_CHECK
00652       assert(I.end()<m);
00653 #endif
00654       return SquareMatrix<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start(),I.start()));
00655     }
00656 
00657   template <class AT>
00658     inline Vector<Ref,AT>  Matrix<General,Ref,Ref,AT>::col(int i) {
00659 
00660 #ifndef FMATVEC_NO_BOUNDS_CHECK
00661       assert(i>=0);
00662       assert(i<n);
00663 #endif
00664 
00665       return Vector<Ref,AT>(m,lda,tp,memory,elePtr(0,i));
00666     }
00667 
00668   template <class AT>
00669     inline const Vector<Ref,AT>  Matrix<General,Ref,Ref,AT>::col(int i) const {
00670 
00671 #ifndef FMATVEC_NO_BOUNDS_CHECK
00672       assert(i>=0);
00673       assert(i<n);
00674 #endif
00675 
00676       return Vector<Ref,AT>(m,lda,tp,memory,elePtr(0,i));
00677     }
00678 
00679   template <class AT>
00680     inline RowVector<Ref,AT>  Matrix<General,Ref,Ref,AT>::row(int i) {
00681 
00682 #ifndef FMATVEC_NO_BOUNDS_CHECK
00683       assert(i>=0);
00684       assert(i<m);
00685 #endif
00686 
00687       return RowVector<Ref,AT>(n,lda,tp,memory,elePtr(i,0));
00688     }
00689 
00690   template <class AT>
00691     inline const RowVector<Ref,AT>  Matrix<General,Ref,Ref,AT>::row(int i) const {
00692 
00693 #ifndef FMATVEC_NO_BOUNDS_CHECK
00694       assert(i>=0);
00695       assert(i<m);
00696 #endif
00697 
00698       return RowVector<Ref,AT>(n,lda,tp,memory,elePtr(i,0));
00699     }
00700 
00701   template <class AT>
00702     inline Matrix<General,Ref,Ref,AT> Matrix<General,Ref,Ref,AT>::copy() const {
00703 
00704       Matrix<General,Ref,Ref,AT> A(m,n,NONINIT);
00705       A.deepCopy(*this);
00706 
00707       return A;
00708     }
00709 
00710   template <class AT>
00711     inline Matrix<General,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() {
00712       std::vector<std::vector<AT> > ret(rows());
00713       if(tp) {
00714         for(int r=0; r<rows(); r++) {
00715           ret[r].resize(cols());
00716           for(int c=0; c<cols(); c++)
00717             ret[r][c]= et(r,c);
00718         }
00719       }
00720       else {
00721         for(int r=0; r<rows(); r++) {
00722           ret[r].resize(cols());
00723           for(int c=0; c<cols(); c++)
00724             ret[r][c]= er(r,c);
00725         }
00726       }
00727       return ret;
00728     }
00729 
00730   template <class AT>
00731     inline Matrix<General,Ref,Ref,AT>::Matrix(std::vector<std::vector<AT> > m) : memory(m.size()*m[0].size()), ele((AT*)memory.get()), m(m.size()), n(m[0].size()), lda(m.size()), tp(false) {
00732 #ifndef FMATVEC_NO_INITIALIZATION 
00733       init(0);
00734 #endif
00735       for(int r=0; r<rows(); r++) {
00736         assert(m[r].size()==cols());
00737         for(int c=0; c<cols(); c++)
00738           er(r,c)=m[r][c];
00739       }
00740     }
00741 
00743 
00744   template <class AT> template <class Type, class Row, class Col>
00745     inline void Matrix<General,Ref,Ref,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) { 
00746       if(tp) {
00747         for(int i=0; i<m; i++) 
00748           for(int j=0; j<n; j++)
00749             et(i,j) = A.e(i,j); 
00750       }
00751       else {
00752         for(int i=0; i<m; i++) 
00753           for(int j=0; j<n; j++)
00754             er(i,j) = A.e(i,j); 
00755       }
00756     }
00757 
00758   template <class AT>
00759     inline void Matrix<General,Ref,Ref,AT>::deepCopy(const Matrix<General,Ref,Ref,AT> &A) { 
00760       if(A.tp) {
00761         if(tp) {
00762           for(int i=0; i<m; i++) 
00763             for(int j=0; j<n; j++)
00764               et(i,j) = A.et(i,j); 
00765         }
00766         else {
00767           for(int i=0; i<m; i++) 
00768             for(int j=0; j<n; j++)
00769               er(i,j) = A.et(i,j); 
00770         }
00771       } else {
00772         if(tp) {
00773           for(int i=0; i<m; i++) 
00774             for(int j=0; j<n; j++)
00775               et(i,j) = A.er(i,j); 
00776         }
00777         else {
00778           for(int i=0; i<m; i++) 
00779             for(int j=0; j<n; j++)
00780               er(i,j) = A.er(i,j); 
00781         }
00782       }
00783     }
00784 
00785   template<class AT>
00786     inline void Matrix<General,Ref,Ref,AT>::deepCopy(const Matrix<Symmetric,Ref,Ref,AT> &A) {
00787       for(int i=0; i<A.size(); i++) {
00788         er(i,i) = A.ej(i,i);
00789         for(int j=i+1; j<A.size(); j++)
00790           er(i,j) = et(i,j) = A.ej(i,j);
00791       }
00792     }
00793 
00795 
00796 }
00797 
00798 #endif