fixed_var_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 fixed_var_general_matrix_h
00023 #define fixed_var_general_matrix_h
00024 
00025 #include "types.h"
00026 #include "range.h"
00027 #include <stdlib.h>
00028 
00029 namespace fmatvec {
00030 
00039   template <int M, class AT> class Matrix<General,Fixed<M>,Var,AT> {
00040 
00041     public:
00042 
00044 
00045     protected:
00046 
00047       int N;
00048 
00049       AT *ele;
00050 
00051       template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A); 
00052 
00054  
00055     public:
00056 
00061       Matrix() : N(0), ele(0) { }
00062 
00063 //      template<class Ini=All<AT> >
00064 //      Matrix(int n, Ini ini=All<AT>()) :  N(n), ele(new AT[M*N]) {
00065 //        init(ini);
00066 //      }
00067 //      template<class Ini=All<AT> >
00068 //      Matrix(int m, int n, Ini ini=All<AT>()) :  N(n), ele(new AT[M*N]) {
00069 //        init(ini);
00070 //      }
00071 
00072       Matrix(int n, Noinit) : N(n), ele(new AT[M*N]) { }
00073       Matrix(int n, Init ini=INIT, const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
00074       Matrix(int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
00075       Matrix(int m, int n, Noinit) : N(n), ele(new AT[M*N]) { }
00076       Matrix(int m, int n, Init ini=INIT, const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
00077       Matrix(int m, int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
00078 
00086       Matrix(const Matrix<General,Fixed<M>,Var,AT> &A) : N(A.N), ele(new AT[M*N]) {
00087         deepCopy(A);
00088       }
00089 
00090       template<class Row, class Col>
00091       Matrix(const Matrix<General,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
00092 
00093         deepCopy(A);
00094       }
00095 
00096       template<class Type, class Row, class Col>
00097       explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
00098 
00099 #ifndef FMATVEC_NO_SIZE_CHECK
00100         assert(A.rows() == M); 
00101 #endif
00102 
00103         deepCopy(A);
00104       }
00105 
00118       Matrix(const char *str);
00119 
00122       ~Matrix() {
00123         delete[] ele;
00124       }
00125 
00126       Matrix<General,Fixed<M>,Var,AT>& resize() { 
00127         delete[] ele;
00128         N = 0;
00129         ele = 0;
00130         return *this;
00131       }
00132 
00133       Matrix<General,Fixed<M>,Var,AT>& resize(int n, Noinit) { 
00134         delete[] ele;
00135         N = n;
00136         ele = new AT[M*N];
00137         return *this;
00138       }
00139 
00140       Matrix<General,Fixed<M>,Var,AT>& resize(int n, Init ini=INIT, const AT &a=0) { return resize(n,Noinit()).init(a); }
00141 
00142       Matrix<General,Fixed<M>,Var,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
00143 
00150       inline Matrix<General,Fixed<M>,Var,AT>& operator=(const Matrix<General,Fixed<M>,Var,AT> &A);
00151 
00152       template <class Type, class Row, class Col>
00153       inline Matrix<General,Fixed<M>,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
00154 
00155       template<class Type, class Row, class Col>
00156       inline Matrix<General,Fixed<M>,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
00157 
00169       AT& operator()(int i, int j) {
00170 #ifndef FMATVEC_NO_BOUNDS_CHECK
00171         assert(i>=0);
00172         assert(j>=0);
00173         assert(i<M);
00174         assert(j<N);
00175 #endif
00176 
00177         return e(i,j);
00178       };
00179 
00184       const AT& operator()(int i, int j) const {
00185 #ifndef FMATVEC_NO_BOUNDS_CHECK
00186         assert(i>=0);
00187         assert(j>=0);
00188         assert(i<M);
00189         assert(j<N);
00190 #endif
00191 
00192         return e(i,j);
00193       };
00194 
00195       AT& e(int i, int j) {
00196         return ele[i*N+j];
00197       };
00198 
00203       const AT& e(int i, int j) const {
00204         return ele[i*N+j];
00205       };
00206 
00207       AT& e(int i) {
00208         return ele[i];
00209       };
00210 
00215       const AT& e(int i) const {
00216         return ele[i];
00217       };
00218 
00224       AT* operator()() {return ele;};
00225 
00230       const AT* operator()() const {return ele;};
00231 
00236       int rows() const {return M;};
00237 
00242       int cols() const {return N;};
00243 
00248       int ldim() const {return M;};
00249 
00256       const CBLAS_TRANSPOSE blasTrans() const {
00257         return CblasNoTrans;
00258       };
00259 
00267       const CBLAS_ORDER blasOrder() const {
00268         return  CblasColMajor;
00269       };
00270 
00297       //inline Matrix<GeneralVar, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
00298 
00303       inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
00304 
00305       template <int M1, int M2>
00306       inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var> &J) const;
00307 
00308       inline const RowVector<Var,AT> row(int j) const;
00309       inline const Vector<Fixed<M>,AT> col(int j) const;
00310 
00318       inline Matrix<General,Fixed<M>,Var,AT>& init(const AT &a=0); 
00319       inline Matrix<General,Fixed<M>,Var,AT>& init(Init, const AT &a=0) { return init(a); }
00320       inline Matrix<General,Fixed<M>,Var,AT>& init(Eye, const AT &a=1);
00321       inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
00322 
00327       inline operator std::vector<std::vector<AT> >();
00328 
00334       inline Matrix(std::vector<std::vector<AT> > m);
00335 
00336       inline const Matrix<General,Var,Fixed<M>,AT> T() const;
00337 
00338       template<class Row> inline void set(int j, const Vector<Row,AT> &x);
00339 
00340       template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
00341 
00342       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);
00343 
00344       template<class Row> inline void add(int j, const Vector<Row,AT> &x);
00345 
00346       template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
00347 
00348       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);
00349 
00350   };
00351 
00352   template <int M, class AT> 
00353     Matrix<General,Fixed<M>,Var,AT>::Matrix(const char *strs) : N(0), ele(0) {
00354       // if 'strs' is a single scalar, surround it first with '[' and ']'.
00355       // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
00356       // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
00357       std::istringstream iss(strs);
00358       char c;
00359       iss>>c;
00360       if(c=='[') iss.str(strs);
00361       else iss.str(std::string("[")+strs+"]");
00362 
00363       int m=0;
00364       int buf=0;
00365       iss >> c;
00366       iss >> c;
00367       if(c!=']') {
00368         iss.putback(c);
00369         AT x;
00370         do {
00371           iss >> x;
00372           iss >> c;
00373           if(c==';') {
00374             if(buf)
00375               assert(buf == N);
00376 
00377             buf=N;
00378             N=0;
00379             m++;
00380           }
00381           else if(c==',')
00382             N++;
00383           c='0';
00384         } while(iss);
00385 
00386         N++; m++;
00387         ele = new AT[M*N];
00388         iss.clear();
00389         iss.seekg(0);
00390         iss >> c;
00391         for(int i=0; i<M; i++)
00392           for(int j=0; j<N; j++) {
00393             iss >> e(i,j);
00394             iss >> c;
00395           }
00396       }
00397       assert(m==M);
00398     }
00399 
00400   template <int M, class AT> template< class Type, class Row, class Col>
00401     inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) { 
00402 
00403 #ifndef FMATVEC_NO_SIZE_CHECK
00404       assert(M == A.rows()); 
00405 #endif
00406       if(!ele) {
00407         delete[] ele;
00408         N = A.cols(); 
00409         ele = new AT[M*N];
00410       } else {
00411 #ifndef FMATVEC_NO_SIZE_CHECK
00412         assert(N == A.cols());
00413 #endif
00414       }
00415 
00416       deepCopy(A);
00417 
00418       return *this;
00419     }
00420 
00421   template <int M, class AT>
00422     inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator=(const Matrix<General,Fixed<M>,Var,AT> &A) { 
00423 
00424       if(!ele) {
00425         delete[] ele;
00426         N = A.cols(); 
00427         ele = new AT[M*N];
00428       } else {
00429 #ifndef FMATVEC_NO_SIZE_CHECK
00430         assert(N == A.cols());
00431 #endif
00432       }
00433 
00434       deepCopy(A);
00435 
00436       return *this;
00437     }
00438 
00439   template <int M, class AT> template< class Type, class Row, class Col>
00440     inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) { 
00441 
00442 #ifndef FMATVEC_NO_SIZE_CHECK
00443       assert(M == A.rows());
00444 #endif
00445       if(N!=A.cols()) {
00446         delete[] ele;
00447         N = A.cols();
00448         ele = new AT[M*N];
00449       }
00450 
00451       deepCopy(A);
00452 
00453       return *this;
00454     }
00455 
00456   template <int M, class AT>
00457     inline Matrix<General,Fixed<M>,Var,AT>&  Matrix<General,Fixed<M>,Var,AT>::init(const AT &val) {
00458       for(int i=0; i<M*N; i++) 
00459         e(i) = val;
00460       return *this;
00461     }
00462 
00463   template <int M, class AT>
00464     inline Matrix<General,Fixed<M>,Var,AT>&  Matrix<General,Fixed<M>,Var,AT>::init(Eye eye, const AT &val) {
00465       for(int i=0; i<M; i++)
00466         for(int j=0; j<N; j++)
00467           e(i,j) = (i==j) ? val : 0;
00468       return *this;
00469     }
00470 
00471   template <int M, class AT> template <int M1, int M2>
00472     inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var> &J) const {
00473 #ifndef FMATVEC_NO_BOUNDS_CHECK
00474       assert(M2<M);
00475       assert(J.end()<N);
00476 #endif
00477       Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
00478 
00479       for(int i=0; i<A.rows(); i++) 
00480         for(int j=0; j<A.cols(); j++)
00481           A.e(i,j) = e(M1+i,J.start()+j);
00482 
00483       return A;
00484     }
00485 
00486   template <int M, class AT> 
00487     inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const {
00488 #ifndef FMATVEC_NO_BOUNDS_CHECK
00489       assert(I.end()<M);
00490       assert(J.end()<N);
00491 #endif
00492       Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
00493 
00494       for(int i=0; i<A.rows(); i++) 
00495         for(int j=0; j<A.cols(); j++)
00496           A.e(i,j) = e(I.start()+i,J.start()+j);
00497 
00498       return A;
00499     }
00500 
00501   template <int M, class AT>
00502     inline const RowVector<Var,AT> Matrix<General,Fixed<M>,Var,AT>::row(int i) const {
00503 
00504 #ifndef FMATVEC_NO_BOUNDS_CHECK
00505       assert(i>=0);
00506       assert(i<M);
00507 #endif
00508 
00509       RowVector<Var,AT> x(N,NONINIT);
00510 
00511       for(int j=0; j<N; j++)
00512         x.e(j) = e(i,j);
00513 
00514       return x;
00515 
00516     }
00517 
00518   template <int M, class AT>
00519     inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(int j) const {
00520 
00521 #ifndef FMATVEC_NO_BOUNDS_CHECK
00522       assert(j>=0);
00523       assert(j<N);
00524 #endif
00525 
00526       Vector<Fixed<M>,AT> x(NONINIT);
00527 
00528       for(int i=0; i<M; i++)
00529         x.e(i) = e(i,j);
00530 
00531       return x;
00532 
00533     }
00534 
00535   template <int M, class AT>
00536     inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T() const {
00537       Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
00538       for(int i=0; i<N; i++)
00539         for(int j=0; j<M; j++)
00540           A.e(i,j) = e(j,i);
00541       return A;
00542     }
00543 
00544   template <int M, class AT> template <class Row>
00545     inline void Matrix<General,Fixed<M>,Var,AT>::set(int j, const Vector<Row,AT> &x) {
00546 #ifndef FMATVEC_NO_BOUNDS_CHECK
00547       assert(j<cols());
00548       assert(rows()==x.size());
00549 #endif
00550       for(int i=0; i<rows(); i++)
00551         e(i,j) = x.e(i);
00552     }
00553 
00554   template <int M, class AT> template <class Col>
00555     inline void Matrix<General,Fixed<M>,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
00556 #ifndef FMATVEC_NO_BOUNDS_CHECK
00557       assert(i<rows());
00558       assert(cols()==x.size());
00559 #endif
00560       for(int j=0; j<cols(); j++)
00561         e(i,j) = x.e(j);
00562     }
00563 
00564   template <int M, class AT> template<class Type, class Row, class Col>
00565     inline void Matrix<General,Fixed<M>,Var,AT>::set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
00566 
00567 #ifndef FMATVEC_NO_BOUNDS_CHECK
00568       assert(I.end()<rows());
00569       assert(J.end()<cols());
00570       assert(I.size()==A.rows());
00571       assert(J.size()==A.cols());
00572 #endif
00573 
00574       for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
00575         for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
00576           e(i,j) = A.e(ii,jj);
00577     }
00578 
00579   template <int M, class AT> template <class Row>
00580     inline void Matrix<General,Fixed<M>,Var,AT>::add(int j, const Vector<Row,AT> &x) {
00581 #ifndef FMATVEC_NO_BOUNDS_CHECK
00582       assert(j<cols());
00583       assert(rows()==x.size());
00584 #endif
00585       for(int i=0; i<rows(); i++)
00586         e(i,j) += x.e(i);
00587     }
00588 
00589   template <int M, class AT> template <class Col>
00590     inline void Matrix<General,Fixed<M>,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
00591 #ifndef FMATVEC_NO_BOUNDS_CHECK
00592       assert(i<rows());
00593       assert(cols()==x.size());
00594 #endif
00595       for(int j=0; j<cols(); j++)
00596         e(i,j) += x.e(j);
00597     }
00598 
00599   template <int M, class AT> template<class Type, class Row, class Col>
00600     inline void Matrix<General,Fixed<M>,Var,AT>::add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
00601 
00602 #ifndef FMATVEC_NO_BOUNDS_CHECK
00603       assert(I.end()<rows());
00604       assert(J.end()<cols());
00605       assert(I.size()==A.rows());
00606       assert(J.size()==A.cols());
00607 #endif
00608 
00609       for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
00610         for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
00611           e(i,j) += A.e(ii,jj);
00612     }
00613 
00614   template <int M, class AT>
00615     inline Matrix<General,Fixed<M>,Var,AT>::operator std::vector<std::vector<AT> >() {
00616       std::vector<std::vector<AT> > ret(rows());
00617       for(int r=0; r<rows(); r++) {
00618         ret[r].resize(cols());
00619         for(int c=0; c<cols(); c++)
00620           ret[r][c]=e(r,c);
00621       }
00622       return ret;
00623     }
00624 
00625   template <int M, class AT>
00626     inline Matrix<General,Fixed<M>,Var,AT>::Matrix(std::vector<std::vector<AT> > m) {
00627 #ifndef FMATVEC_NO_SIZE_CHECK
00628       assert(m.size() == M);
00629       assert(m[0].size() == N);
00630 #endif
00631       for(int r=0; r<rows(); r++) {
00632         assert(m[r].size()==cols());
00633         for(int c=0; c<cols(); c++)
00634           e(r,c)=m[r][c];
00635       }
00636     }
00637 
00639 
00640   template <int M, class AT> template <class Type, class Row, class Col>
00641     inline void Matrix<General,Fixed<M>,Var,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) { 
00642       for(int i=0; i<M; i++) 
00643         for(int j=0; j<N; j++)
00644           e(i,j) = A.e(i,j);
00645     }
00646 
00648 
00649 }
00650 
00651 #endif