fixed_vector.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_vector_h
00023 #define fixed_vector_h
00024 
00025 #include "fixed_general_matrix.h"
00026 #include <vector>
00027 #include <cstring>
00028 
00029 namespace fmatvec {
00030 
00039   template <int M, class AT>
00040   class Vector<Fixed<M>,AT> : public Matrix<General,Fixed<M>,Fixed<1>,AT> {
00041     using Matrix<General,Fixed<M>,Fixed<1>,AT>::ele;
00042 
00043     public:
00044 
00046 
00047     protected:
00048 
00049     template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
00050 
00052     
00053     public:
00054 
00055 //      template<class Ini=All<AT> >
00056 //        Vector(Ini ini=All<AT>()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
00057 //      template<class Ini=All<AT> >
00058 //        Vector(int m, Ini ini=All<AT>()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
00059 
00060       Vector(Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
00061       Vector(Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { }
00062       Vector(int m, Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
00063       Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { }
00064 
00077       Vector(const char *str) : Matrix<General,Fixed<M>,Fixed<1>,AT>(str) {
00078       }
00079 
00087       template<class Row>
00088       Vector(const Vector<Row,AT> &x) : Matrix<General,Fixed<M>,Fixed<1>,AT>(x) {
00089       }
00090 
00095       template<class Type, class Row, class Col>
00096       explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<M>,Fixed<1>,AT>(A) {
00097       }
00098 
00099       template <class Row>
00100       inline Vector<Fixed<M>,AT>& operator=(const Vector<Row,AT> &x);
00101 
00112       AT& operator()(int i) {
00113 
00114 #ifndef FMATVEC_NO_BOUNDS_CHECK
00115         assert(i>=0);
00116         assert(i<M);
00117 #endif
00118 
00119         return e(i);
00120       };
00121 
00126       const AT& operator()(int i) const {
00127 
00128 #ifndef FMATVEC_NO_BOUNDS_CHECK
00129         assert(i>=0);
00130         assert(i<M);
00131 #endif
00132 
00133         return e(i);
00134       };
00135 
00136       AT& e(int i) {
00137         return ele[i][0];
00138       };
00139 
00144       const AT& e(int i) const {
00145         return ele[i][0];
00146       };
00147 
00155       inline Vector<Fixed<M>,AT>& init(const AT& a=0);
00156       inline Vector<Fixed<M>,AT>& init(Init, const AT& a=0) { return init(a); }
00157       inline Vector<Fixed<M>,AT>& init(Noinit, const AT& a=0) { return *this; }
00158 
00163       int size() const {return M;};
00164 
00171       int inc() const {return 1;};
00172 
00173       template <int M1, int M2>
00174       inline const Vector<Fixed<M2-M1+1>,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I) const;
00175 
00176       using Matrix<General,Fixed<M>,Fixed<1>,AT>::operator();
00177 
00182       inline operator std::vector<AT>();
00183 
00188       inline Vector(std::vector<AT> v);
00189 
00193       inline const RowVector<Fixed<M>,AT> T() const;
00194 
00198       template<class Type, class Row, class Col>
00199       inline void set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A);
00200 
00201   };
00202 
00203   template <int M, class AT>
00204     inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::init(const AT &val) {
00205       for(int i=0; i<M; i++) 
00206         e(i) = val; 
00207       return *this;
00208     }
00209 
00210   template <int M, class AT> template <int M1, int M2>
00211     inline const Vector<Fixed<M2-M1+1>,AT> Vector<Fixed<M>,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I) const {
00212 #ifndef FMATVEC_NO_BOUNDS_CHECK
00213       assert(M2<M);
00214 #endif
00215       Vector<Fixed<M2-M1+1>,AT> x(NONINIT);
00216 
00217       for(int i=0; i<x.size(); i++) 
00218         x.e(i) = e(M1+i);
00219 
00220       return x;
00221     }
00222 
00223   template <int M, class AT> template <class Row>
00224     inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::operator=(const Vector<Row,AT> &x) { 
00225 
00226 #ifndef FMATVEC_NO_SIZE_CHECK
00227        assert(x.size() == M);
00228 #endif
00229 
00230       deepCopy(x);
00231 
00232       return *this;
00233     }
00234 
00235   template <int M, class AT>
00236     inline const RowVector<Fixed<M>,AT> Vector<Fixed<M>,AT>::T() const {
00237       RowVector<Fixed<M>,AT> x(NONINIT);
00238       for(int i=0; i<M; i++)
00239         x.e(i) = e(i);
00240       return x;
00241     }
00242 
00243   template <int M, class AT>
00244    inline Vector<Fixed<M>,AT>::operator std::vector<AT>() {
00245       std::vector<AT> ret(size());
00246       if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
00247       return ret;
00248     }
00249 
00250   template <int M, class AT>
00251     inline Vector<Fixed<M>,AT>::Vector(std::vector<AT> v) : Matrix<General,Fixed<M>,Fixed<1>,AT>() {
00252       if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
00253     }
00254 
00256 
00257   template <int M, class AT> template<class Row>
00258     inline void Vector<Fixed<M>,AT>::deepCopy(const Vector<Row,AT> &x) {
00259       for(int i=0; i<M; i++)
00260         e(i) = x.e(i);
00261     }
00262 
00264 
00265   template<int M, class AT> template<class Type, class Row, class Col>
00266   inline void Vector<Fixed<M>,AT>::set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A) {
00267       Matrix<General,Fixed<M>,Fixed<1>,AT>::set(I, Index(0,0), A);
00268   }
00269 
00270 }
00271 
00272 #endif