var_general_matrix.h
00001 /* Copyright (C) 2003-2012  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_general_matrix_h
00023 #define var_general_matrix_h
00024 
00025 #include "types.h"
00026 #include <stdlib.h>
00027 
00028 namespace fmatvec {
00029 
00038   template <class AT> class Matrix<General,Var,Var,AT> {
00039 
00040     public:
00041 
00043 
00044     protected:
00045 
00046       int M, N;
00047 
00048       AT *ele;
00049 
00050       template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A); 
00051 
00053  
00054     public:
00055 
00060       Matrix() : M(0), N(0), ele(0) { }
00061 
00062 // Works with -std=gnu++0x only
00063 //      template<class Ini=All<AT> >
00064 //      Matrix(int m, int n, Ini ini=All<AT>()) :  M(m), N(n), ele(new AT[M*N]) {
00065 //        init(ini);
00066 //      }
00067 
00068       Matrix(int m, int n, Noinit) :  M(m), N(n), ele(new AT[M*N]) { }
00069       Matrix(int m, int n, Init ini=INIT, const AT &a=0) :  M(m), N(n), ele(new AT[M*N]) { init(a); }
00070       Matrix(int m, int n, Eye ini, const AT &a=1) :  M(m), N(n), ele(new AT[M*N]) { init(ini,a); }
00071 
00079       Matrix(const Matrix<General,Var,Var,AT> &A) : M(A.M), N(A.N), ele(new AT[M*N]) {
00080         deepCopy(A);
00081       }
00082 
00083       template<class Row, class Col>
00084       Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
00085 
00086         deepCopy(A);
00087       }
00088 
00089       template<class Type, class Row, class Col>
00090       explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
00091 
00092         deepCopy(A);
00093       }
00094 
00107       Matrix(const char *str);
00108 
00111       ~Matrix() {
00112         delete[] ele;
00113       }
00114 
00115        Matrix<General,Var,Var,AT>& resize() {
00116         delete[] ele;
00117         M = N = 0;
00118         ele = 0;
00119         return *this;
00120       }
00121 
00122        Matrix<General,Var,Var,AT>& resize(int m, int n, Noinit) {
00123         delete[] ele;
00124         M = m;
00125         N = n;
00126         ele = new AT[M*N];
00127         return *this;
00128       }
00129 
00130       Matrix<General,Var,Var,AT>& resize(int m, int n, Init ini=INIT, const AT &a=0) { return resize(m,n,Noinit()).init(a); }
00131 
00132       Matrix<General,Var,Var,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); } 
00133 
00140       inline Matrix<General,Var,Var,AT>& operator=(const Matrix<General,Var,Var,AT> &A);
00141 
00142       template <class Type, class Row, class Col>
00143       inline Matrix<General,Var,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
00144 
00151       template<class Type, class Row, class Col>
00152         inline Matrix<General,Var,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
00153 
00165       AT& operator()(int i, int j) {
00166 #ifndef FMATVEC_NO_BOUNDS_CHECK
00167         assert(i>=0);
00168         assert(j>=0);
00169         assert(i<M);
00170         assert(j<N);
00171 #endif
00172 
00173         return e(i,j);
00174       };
00175 
00180       const AT& operator()(int i, int j) const {
00181 #ifndef FMATVEC_NO_BOUNDS_CHECK
00182         assert(i>=0);
00183         assert(j>=0);
00184         assert(i<M);
00185         assert(j<N);
00186 #endif
00187 
00188         return e(i,j);
00189       };
00190 
00191       AT& e(int i, int j) {
00192         return ele[i*N+j];
00193       };
00194 
00199       const AT& e(int i, int j) const {
00200         return ele[i*N+j];
00201       };
00202 
00203       AT& e(int i) {
00204         return ele[i];
00205       };
00206 
00211       const AT& e(int i) const {
00212         return ele[i];
00213       };
00214 
00220       AT* operator()() {return ele;};
00221 
00226       const AT* operator()() const {return ele;};
00227 
00232       int rows() const {return M;};
00233 
00238       int cols() const {return N;};
00239 
00244       int ldim() const {return M;};
00245 
00252       const CBLAS_TRANSPOSE blasTrans() const {
00253         return CblasNoTrans;
00254       };
00255 
00263       const CBLAS_ORDER blasOrder() const {
00264         return  CblasColMajor;
00265       };
00266 
00293       //inline Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
00294 
00299       inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
00300 
00301       template <int M1, int M2, int N1, int N2>
00302       inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Fixed<N1>,Fixed<N2> > &J) const;
00303 
00304       template <int M1, int M2>
00305       inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var > &J) const;
00306 
00307       template <int N1, int N2>
00308       inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2> > &J) const;
00309 
00310       inline const RowVector<Var,AT> row(int i) const;
00311       inline const Vector<Var,AT> col(int j) const;
00312 
00320       inline Matrix<General,Var,Var,AT>& init(const AT &a=0);
00321       inline Matrix<General,Var,Var,AT>& init(Init all, const AT &a=0) { return init(a); }
00322       inline Matrix<General,Var,Var,AT>& init(Eye eye, const AT &a=1);
00323       inline Matrix<General,Var,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
00324 
00329       inline operator std::vector<std::vector<AT> >();
00330 
00336       inline Matrix(std::vector<std::vector<AT> > m);
00337 
00338       inline const Matrix<General,Var,Var,AT> T() const;
00339 
00340       template<class Row> inline void set(int j, const Vector<Row,AT> &x);
00341 
00342       template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
00343 
00344       template<class Type, class Row, class Col> inline void set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
00345 
00346       template<class Row> inline void add(int j, const Vector<Row,AT> &x);
00347 
00348       template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
00349 
00350       template<class Type, class Row, class Col> inline void add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
00351 
00352   };
00353 
00354   template <class AT> 
00355     Matrix<General,Var,Var,AT>::Matrix(const char *strs) : M(0), N(0), ele(0) {
00356       // if 'strs' is a single scalar, surround it first with '[' and ']'.
00357       // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
00358       // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
00359       std::istringstream iss(strs);
00360       char c;
00361       iss>>c;
00362       if(c=='[') iss.str(strs);
00363       else iss.str(std::string("[")+strs+"]");
00364 
00365       int buf=0;
00366       iss >> c;
00367       iss >> c;
00368       if(c!=']') {
00369         iss.putback(c);
00370         AT x;
00371         do {
00372           iss >> x;
00373           iss >> c;
00374           if(c==';') {
00375             if(buf)
00376               assert(buf == N);
00377 
00378             buf=N;
00379             N=0;
00380             M++;
00381           }
00382           else if(c==',')
00383             N++;
00384           c='0';
00385         } while(iss);
00386 
00387         N++; M++;
00388         ele = new AT[M*N];
00389         iss.clear();
00390         iss.seekg(0);
00391         iss >> c;
00392         for(int i=0; i<M; i++)
00393           for(int j=0; j<N; j++) {
00394             iss >> e(i,j);
00395             iss >> c;
00396           }
00397       }
00398     }
00399 
00400   template <class AT> template< class Type, class Row, class Col>
00401     inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) { 
00402 
00403       if(!ele) {
00404         delete[] ele;
00405         M = A.rows(); 
00406         N = A.cols();
00407         ele = new AT[M*N];
00408       } else {
00409 #ifndef FMATVEC_NO_SIZE_CHECK
00410         assert(M == A.rows());
00411         assert(N == A.cols());
00412 #endif
00413       }
00414 
00415       deepCopy(A);
00416 
00417       return *this;
00418     }
00419 
00420   template <class AT>
00421     inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::operator=(const Matrix<General,Var,Var,AT> &A) { 
00422 
00423       if(!ele) {
00424         delete[] ele;
00425         M = A.rows(); 
00426         N = A.cols();
00427         ele = new AT[M*N];
00428       } else {
00429 #ifndef FMATVEC_NO_SIZE_CHECK
00430         assert(M == A.rows());
00431         assert(N == A.cols());
00432 #endif
00433       }
00434 
00435       deepCopy(A);
00436 
00437       return *this;
00438     }
00439 
00440   template <class AT>
00441     inline Matrix<General,Var,Var,AT>&  Matrix<General,Var,Var,AT>::init(const AT &val) {
00442       for(int i=0; i<M*N; i++) 
00443         e(i) = val;
00444       return *this;
00445     }
00446 
00447   template <class AT>
00448     inline Matrix<General,Var,Var,AT>&  Matrix<General,Var,Var,AT>::init(Eye eye, const AT &val) {
00449       for(int i=0; i<M; i++)
00450         for(int j=0; j<N; j++)
00451           e(i,j) = (i==j) ? val : 0;
00452       return *this;
00453     }
00454 
00455   template <class AT> template< class Type, class Row, class Col>
00456     inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) { 
00457 
00458       if(M!=A.rows() || N!=A.cols()) {
00459         delete[] ele;
00460         M = A.rows(); 
00461         N = A.cols();
00462         ele = new AT[M*N];
00463       }
00464 
00465       deepCopy(A);
00466 
00467       return *this;
00468     }
00469 
00470   template <class AT> 
00471     inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const {
00472 #ifndef FMATVEC_NO_BOUNDS_CHECK
00473       assert(I.end()<M);
00474       assert(J.end()<N);
00475 #endif
00476       Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
00477 
00478       for(int i=0; i<A.rows(); i++) 
00479         for(int j=0; j<A.cols(); j++)
00480           A.e(i,j) = e(I.start()+i,J.start()+j);
00481 
00482       return A;
00483     }
00484 
00485  template <class AT> template <int M1, int M2, int N1, int N2>
00486     inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> Matrix<General,Var,Var,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Fixed<N1>,Fixed<N2> > &J) const {
00487 #ifndef FMATVEC_NO_BOUNDS_CHECK
00488       assert(M2<M);
00489       assert(N2<N);
00490 #endif
00491       Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
00492 
00493       for(int i=0; i<A.rows(); i++) 
00494         for(int j=0; j<A.cols(); j++)
00495           A.e(i,j) = e(I.start()+i,J.start()+j);
00496 
00497       return A;
00498     }
00499 
00500  template <class AT> template <int M1, int M2>
00501     inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Var,Var,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var> &J) const {
00502 #ifndef FMATVEC_NO_BOUNDS_CHECK
00503       assert(M2<M);
00504       assert(J.end()<N);
00505 #endif
00506       Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
00507 
00508       for(int i=0; i<A.rows(); i++) 
00509         for(int j=0; j<A.cols(); j++)
00510           A.e(i,j) = e(I.start()+i,J.start()+j);
00511 
00512       return A;
00513     }
00514 
00515   template <class AT> template <int N1, int N2>
00516     inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> Matrix<General,Var,Var,AT>::operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2> > &J) const {
00517 #ifndef FMATVEC_NO_BOUNDS_CHECK
00518       assert(I.end()<M);
00519       assert(N2<N);
00520 #endif
00521       Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
00522 
00523       for(int i=0; i<A.rows(); i++) 
00524         for(int j=0; j<A.cols(); j++)
00525           A.e(i,j) = e(I.start()+i,J.start()+j);
00526 
00527       return A;
00528     }
00529 
00530   template <class AT>
00531     inline const RowVector<Var,AT> Matrix<General,Var,Var,AT>::row(int i) const {
00532 
00533 #ifndef FMATVEC_NO_BOUNDS_CHECK
00534       assert(i>=0);
00535       assert(i<M);
00536 #endif
00537 
00538       RowVector<Var,AT> x(N,NONINIT);
00539 
00540       for(int j=0; j<N; j++)
00541         x.e(j) = e(i,j);
00542 
00543       return x;
00544 
00545     }
00546 
00547   template <class AT>
00548     inline const Vector<Var,AT> Matrix<General,Var,Var,AT>::col(int j) const {
00549 
00550 #ifndef FMATVEC_NO_BOUNDS_CHECK
00551       assert(j>=0);
00552       assert(j<N);
00553 #endif
00554 
00555       Vector<Var,AT> x(M,NONINIT);
00556 
00557       for(int i=0; i<M; i++)
00558         x.e(i) = e(i,j);
00559 
00560       return x;
00561 
00562     }
00563 
00564   template <class AT>
00565     inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::T() const {
00566       Matrix<General,Var,Var,AT> A(N,M,NONINIT);
00567       for(int i=0; i<N; i++)
00568         for(int j=0; j<M; j++)
00569           A.e(i,j) = e(j,i);
00570       return A;
00571     }
00572 
00573   template <class AT> template <class Row>
00574     inline void Matrix<General,Var,Var,AT>::set(int j, const Vector<Row,AT> &x) {
00575 #ifndef FMATVEC_NO_BOUNDS_CHECK
00576       assert(j<cols());
00577       assert(rows()==x.size());
00578 #endif
00579       for(int i=0; i<rows(); i++)
00580         e(i,j) = x.e(i);
00581     }
00582 
00583   template <class AT> template <class Col>
00584     inline void Matrix<General,Var,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
00585 #ifndef FMATVEC_NO_BOUNDS_CHECK
00586       assert(i<rows());
00587       assert(cols()==x.size());
00588 #endif
00589       for(int j=0; j<cols(); j++)
00590         e(i,j) = x.e(j);
00591     }
00592 
00593   template <class AT> template<class Type, class Row, class Col>
00594     inline void Matrix<General,Var,Var,AT>::set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
00595 
00596 #ifndef FMATVEC_NO_BOUNDS_CHECK
00597       assert(I.end()<rows());
00598       assert(J.end()<cols());
00599       assert(I.size()==A.rows());
00600       assert(J.size()==A.cols());
00601 #endif
00602 
00603       for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
00604         for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
00605           e(i,j) = A.e(ii,jj);
00606     }
00607 
00608   template <class AT> template <class Row>
00609     inline void Matrix<General,Var,Var,AT>::add(int j, const Vector<Row,AT> &x) {
00610 #ifndef FMATVEC_NO_BOUNDS_CHECK
00611       assert(j<cols());
00612       assert(rows()==x.size());
00613 #endif
00614       for(int i=0; i<rows(); i++)
00615         e(i,j) += x.e(i);
00616     }
00617 
00618   template <class AT> template <class Col>
00619     inline void Matrix<General,Var,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
00620 #ifndef FMATVEC_NO_BOUNDS_CHECK
00621       assert(i<rows());
00622       assert(cols()==x.size());
00623 #endif
00624       for(int j=0; j<cols(); j++)
00625         e(i,j) += x.e(j);
00626     }
00627 
00628   template <class AT> template<class Type, class Row, class Col>
00629     inline void Matrix<General,Var,Var,AT>::add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
00630 
00631 #ifndef FMATVEC_NO_BOUNDS_CHECK
00632       assert(I.end()<rows());
00633       assert(J.end()<cols());
00634       assert(I.size()==A.rows());
00635       assert(J.size()==A.cols());
00636 #endif
00637 
00638       for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
00639         for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
00640           e(i,j) += A.e(ii,jj);
00641     }
00642 
00643   template <class AT>
00644     inline Matrix<General,Var,Var,AT>::operator std::vector<std::vector<AT> >() {
00645       std::vector<std::vector<AT> > ret(rows());
00646       for(int r=0; r<rows(); r++) {
00647         ret[r].resize(cols());
00648         for(int c=0; c<cols(); c++)
00649           ret[r][c]=e(r,c);
00650       }
00651       return ret;
00652     }
00653 
00654   template <class AT>
00655     inline Matrix<General,Var,Var,AT>::Matrix(std::vector<std::vector<AT> > m) : M(m.size()), N(m[0].size()), ele(new AT[M*N]) {
00656       for(int r=0; r<rows(); r++) {
00657         assert(m[r].size()==cols());
00658         for(int c=0; c<cols(); c++)
00659           e(r,c)=m[r][c];
00660       }
00661     }
00662 
00664 
00665   template <class AT> template <class Type, class Row, class Col>
00666     inline void Matrix<General,Var,Var,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) { 
00667       for(int i=0; i<M; i++) 
00668         for(int j=0; j<N; j++)
00669           e(i,j) = A.e(i,j);
00670     }
00671 
00673 
00674 }
00675 
00676 #endif