var_fixed_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 var_fixed_general_matrix_h
00023 #define var_fixed_general_matrix_h
00024 
00025 #include "types.h"
00026 #include <stdlib.h>
00027 
00028 namespace fmatvec {
00029 
00038   template <int N, class AT> class Matrix<General,Var,Fixed<N>,AT> {
00039 
00040     public:
00041 
00043 
00044     protected:
00045 
00046       int M;
00047 
00048       AT *ele;
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<General,Var,Fixed<N>,AT> &A); 
00052 
00054  
00055     public:
00056 
00057       Matrix() : M(0), ele(0) { }
00058 
00059 //      template<class Ini=All<AT> >
00060 //      Matrix(int m, Ini ini=All<AT>()) :  M(m), ele(new AT[M*N]) {
00061 //        init(ini);
00062 //      }
00063 //      template<class Ini=All<AT> >
00064 //      Matrix(int m, int n, Ini ini=All<AT>()) :  M(m), ele(new AT[M*N]) {
00065 //        init(ini);
00066 //      }
00067 
00068       Matrix(int m, Noinit) : M(m), ele(new AT[M*N]) { }
00069       Matrix(int m, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*N]) { init(a); }
00070       Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
00071       Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*N]) { }
00072       Matrix(int m, int n, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*N]) { init(a); }
00073       Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
00074 
00082       Matrix(const Matrix<General,Var,Fixed<N>,AT> &A) : M(A.M), ele(new AT[M*N]) {
00083         deepCopy(A);
00084       }
00085 
00086       template<class Row, class Col>
00087       Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
00088 
00089         deepCopy(A);
00090       }
00091 
00092       template<class Type, class Row, class Col>
00093       explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
00094 
00095 #ifndef FMATVEC_NO_SIZE_CHECK
00096         assert(A.cols() == N); 
00097 #endif
00098 
00099         deepCopy(A);
00100       }
00101 
00114       Matrix(const char *str);
00115 
00118       ~Matrix() {
00119         delete[] ele;
00120       }
00121 
00122       Matrix<General,Var,Fixed<N>,AT>& resize() {
00123         delete[] ele;
00124         M = 0;
00125         ele = 0;
00126         return *this;
00127       }
00128 
00129       Matrix<General,Var,Fixed<N>,AT>& resize(int m, Noinit) {
00130         delete[] ele;
00131         M = m;
00132         ele = new AT[M*N];
00133         return *this;
00134       }
00135 
00136       Matrix<General,Var,Fixed<N>,AT>& resize(int m, Init ini=INIT, const AT &a=0) { return resize(m,Noinit()).init(a); }
00137 
00138       Matrix<General,Var,Fixed<N>,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
00139 
00146       inline Matrix<General,Var,Fixed<N>,AT>& operator=(const Matrix<General,Var,Fixed<N>,AT> &A);
00147 
00148       template <class Type, class Row, class Col>
00149       inline Matrix<General,Var,Fixed<N>,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
00150       
00151       template <class Type, class Row, class Col>
00152       inline Matrix<General,Var,Fixed<N>,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 
00267       inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
00268 
00269       inline const RowVector<Fixed<N>,AT> row(int j) const;
00270       inline const Vector<Var,AT> col(int j) const;
00271 
00279       inline Matrix<General,Var,Fixed<N>,AT>& init(const AT &a=0); 
00280       inline Matrix<General,Var,Fixed<N>,AT>& init(Init, const AT &a=0) { return init(a); }
00281       inline Matrix<General,Var,Fixed<N>,AT>& init(Eye, const AT &a=1);
00282       inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit, const AT &a=0) { return *this; }
00283 
00288       inline operator std::vector<std::vector<AT> >();
00289 
00295       inline Matrix(std::vector<std::vector<AT> > m);
00296 
00297       inline const Matrix<General,Fixed<N>,Var,AT> T() const;
00298 
00299       template<class Row> inline void set(int j, const Vector<Row,AT> &x);
00300 
00301       template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
00302 
00303       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);
00304 
00305       template<class Row> inline void add(int j, const Vector<Row,AT> &x);
00306 
00307       template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
00308 
00309       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);
00310 
00311   };
00312 
00313   template <int N, class AT> 
00314     Matrix<General,Var,Fixed<N>,AT>::Matrix(const char *strs) : M(0), ele(0) {
00315       // if 'strs' is a single scalar, surround it first with '[' and ']'.
00316       // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
00317       // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
00318       std::istringstream iss(strs);
00319       char c;
00320       iss>>c;
00321       if(c=='[') iss.str(strs);
00322       else iss.str(std::string("[")+strs+"]");
00323 
00324       int n=0;
00325       int buf=0;
00326       iss >> c;
00327       iss >> c;
00328       if(c!=']') {
00329         iss.putback(c);
00330         AT x;
00331         do {
00332           iss >> x;
00333           iss >> c;
00334           if(c==';') {
00335             if(buf)
00336               assert(buf == n);
00337 
00338             buf=n;
00339             n=0;
00340             M++;
00341           }
00342           else if(c==',')
00343             n++;
00344           c='0';
00345         } while(iss);
00346 
00347         n++; M++;
00348         ele = new AT[M*N];
00349         iss.clear();
00350         iss.seekg(0);
00351         iss >> c;
00352         for(int i=0; i<M; i++)
00353           for(int j=0; j<N; j++) {
00354             iss >> e(i,j);
00355             iss >> c;
00356           }
00357       }
00358       assert(n==N);
00359     }
00360 
00361   template <int N, class AT> template< class Type, class Row, class Col>
00362     inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) { 
00363 
00364 #ifndef FMATVEC_NO_SIZE_CHECK
00365       assert(N == A.cols()); 
00366 #endif
00367       if(!ele) {
00368         delete[] ele;
00369         M = A.rows(); 
00370         ele = new AT[M*N];
00371       } else {
00372 #ifndef FMATVEC_NO_SIZE_CHECK
00373         assert(M == A.rows());
00374 #endif
00375       }
00376 
00377       deepCopy(A);
00378 
00379       return *this;
00380     }
00381 
00382   template <int N, class AT>
00383     inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::operator=(const Matrix<General,Var,Fixed<N>,AT> &A) { 
00384 
00385       if(!ele) {
00386         delete[] ele;
00387         M = A.rows(); 
00388         ele = new AT[M*N];
00389       } else {
00390 #ifndef FMATVEC_NO_SIZE_CHECK
00391         assert(M == A.rows());
00392 #endif
00393       }
00394 
00395       deepCopy(A);
00396 
00397       return *this;
00398     }
00399 
00400   template <int N, class AT> template< class Type, class Row, class Col>
00401     inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) { 
00402 
00403 #ifndef FMATVEC_NO_SIZE_CHECK
00404       assert(N == A.cols());
00405 #endif
00406       if(M!=A.rows()) {
00407         delete[] ele;
00408         M = A.rows();
00409         ele = new AT[M*N];
00410       }
00411 
00412       deepCopy(A);
00413 
00414       return *this;
00415     }
00416 
00417   template <int N, class AT>
00418     inline Matrix<General,Var,Fixed<N>,AT>&  Matrix<General,Var,Fixed<N>,AT>::init(const AT &val) {
00419       for(int i=0; i<M*N; i++) 
00420         e(i) = val;
00421       return *this;
00422     }
00423 
00424   template <int N, class AT>
00425     inline Matrix<General,Var,Fixed<N>,AT>&  Matrix<General,Var,Fixed<N>,AT>::init(Eye eye, const AT &val) {
00426       for(int i=0; i<M; i++)
00427         for(int j=0; j<N; j++)
00428           e(i,j) = (i==j) ? val : 0;
00429       return *this;
00430     }
00431 
00432   template <int N, class AT>
00433     inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Fixed<N>,AT>::operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const {
00434 #ifndef FMATVEC_NO_BOUNDS_CHECK
00435       assert(I.end()<M);
00436       assert(J.end()<N);
00437 #endif
00438       Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
00439 
00440       for(int i=0; i<A.rows(); i++) 
00441         for(int j=0; j<A.cols(); j++)
00442           A.e(i,j) = e(I.start()+i,J.start()+j);
00443 
00444       return A;
00445     }
00446 
00447   template <int N, class AT>
00448     inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(int i) const {
00449 
00450 #ifndef FMATVEC_NO_BOUNDS_CHECK
00451       assert(i>=0);
00452       assert(i<M);
00453 #endif
00454 
00455       RowVector<Fixed<N>,AT> x(NONINIT);
00456 
00457       for(int j=0; j<N; j++)
00458         x.e(j) = e(i,j);
00459 
00460       return x;
00461 
00462     }
00463 
00464   template <int N, class AT>
00465     inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(int j) const {
00466 
00467 #ifndef FMATVEC_NO_BOUNDS_CHECK
00468       assert(j>=0);
00469       assert(j<N);
00470 #endif
00471 
00472       Vector<Var,AT> x(M,NONINIT);
00473 
00474       for(int i=0; i<M; i++)
00475         x.e(i) = e(i,j);
00476 
00477       return x;
00478 
00479     }
00480 
00481   template <int N, class AT>
00482     inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T() const {
00483       Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
00484       for(int i=0; i<N; i++)
00485         for(int j=0; j<M; j++)
00486           A.e(i,j) = e(j,i);
00487       return A;
00488     }
00489 
00490   template <int N, class AT> template <class Row>
00491     inline void Matrix<General,Var,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
00492 #ifndef FMATVEC_NO_BOUNDS_CHECK
00493       assert(j<cols());
00494       assert(rows()==x.size());
00495 #endif
00496       for(int i=0; i<rows(); i++)
00497         e(i,j) = x.e(i);
00498     }
00499 
00500   template <int N, class AT> template <class Col>
00501     inline void Matrix<General,Var,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
00502 #ifndef FMATVEC_NO_BOUNDS_CHECK
00503       assert(i<rows());
00504       assert(cols()==x.size());
00505 #endif
00506       for(int j=0; j<cols(); j++)
00507         e(i,j) = x.e(j);
00508     }
00509 
00510   template <int N, class AT> template<class Type, class Row, class Col>
00511     inline void Matrix<General,Var,Fixed<N>,AT>::set(const Index &I, const Index &J, const Matrix<Type,Row,Col,AT> &A) {
00512 
00513 #ifndef FMATVEC_NO_BOUNDS_CHECK
00514       assert(I.end()<rows());
00515       assert(J.end()<cols());
00516       assert(I.size()==A.rows());
00517       assert(J.size()==A.cols());
00518 #endif
00519 
00520       for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
00521         for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
00522           e(i,j) = A.e(ii,jj);
00523     }
00524 
00525   template <int N, class AT> template <class Row>
00526     inline void Matrix<General,Var,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
00527 #ifndef FMATVEC_NO_BOUNDS_CHECK
00528       assert(j<cols());
00529       assert(rows()==x.size());
00530 #endif
00531       for(int i=0; i<rows(); i++)
00532         e(i,j) += x.e(i);
00533     }
00534 
00535   template <int N, class AT> template <class Col>
00536     inline void Matrix<General,Var,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
00537 #ifndef FMATVEC_NO_BOUNDS_CHECK
00538       assert(i<rows());
00539       assert(cols()==x.size());
00540 #endif
00541       for(int j=0; j<cols(); j++)
00542         e(i,j) += x.e(j);
00543     }
00544 
00545   template <int N, class AT> template<class Type, class Row, class Col>
00546     inline void Matrix<General,Var,Fixed<N>,AT>::add(const Index &I, const Index &J, const Matrix<Type,Row,Col,AT> &A) {
00547 
00548 #ifndef FMATVEC_NO_BOUNDS_CHECK
00549       assert(I.end()<rows());
00550       assert(J.end()<cols());
00551       assert(I.size()==A.rows());
00552       assert(J.size()==A.cols());
00553 #endif
00554 
00555       for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
00556         for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
00557           e(i,j) += A.e(ii,jj);
00558     }
00559 
00560   template <int N, class AT>
00561     inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT> >() {
00562       std::vector<std::vector<AT> > ret(rows());
00563       for(int r=0; r<rows(); r++) {
00564         ret[r].resize(cols());
00565         for(int c=0; c<cols(); c++)
00566           ret[r][c]=e(r,c);
00567       }
00568       return ret;
00569     }
00570 
00571   template <int N, class AT>
00572     inline Matrix<General,Var,Fixed<N>,AT>::Matrix(std::vector<std::vector<AT> > m) {
00573 #ifndef FMATVEC_NO_SIZE_CHECK
00574       assert(m.size() == M);
00575       assert(m[0].size() == N);
00576 #endif
00577       for(int r=0; r<rows(); r++) {
00578         assert(m[r].size()==cols());
00579         for(int c=0; c<cols(); c++)
00580           e(r,c)=m[r][c];
00581       }
00582     }
00583 
00585 
00586   template <int N, class AT> template <class Type, class Row, class Col>
00587     inline void Matrix<General,Var,Fixed<N>,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) { 
00588       for(int i=0; i<M; i++) 
00589         for(int j=0; j<N; j++)
00590           e(i,j) = A.e(i,j);
00591     }
00592 
00593 //  template<int N, class AT>
00594 //    inline void Matrix<General,Var,Fixed<N>,AT>::deepCopy(const Matrix<General,Var,Fixed<N>,AT> &A) {
00595 //      for(int i=0; i<M*N; i++) 
00596 //        e(i) = A.e(i);
00597 //    }
00598 
00600 
00601 }
00602 
00603 #endif