All Classes Namespaces Functions Variables Typedefs Enumerations Pages
fixed_vector.h
1 /* Copyright (C) 2003-2005 Martin Förg
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef fixed_vector_h
23 #define fixed_vector_h
24 
25 #include "fixed_general_matrix.h"
26 #include <vector>
27 #include <cstring>
28 #include <stdexcept>
29 
30 namespace fmatvec {
31 
40  template <int M, class AT>
41  class Vector<Fixed<M>,AT> : public Matrix<General,Fixed<M>,Fixed<1>,AT> {
42  using Matrix<General,Fixed<M>,Fixed<1>,AT>::ele;
43 
44  public:
45 
46  typedef AT AtomicType;
47 
49 
50  protected:
51 
52  template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
53 
55 
56  public:
57 
58 // template<class Ini=All<AT> >
59 // Vector(Ini ini=All<AT>()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
60 // template<class Ini=All<AT> >
61 // Vector(int m, Ini ini=All<AT>()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
62 
63  Vector(Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
64  Vector(Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { }
65  Vector(int m, Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
66  Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { }
67 
80  Vector(const char *str) : Matrix<General,Fixed<M>,Fixed<1>,AT>(str) {
81  }
82 
90  template<class Row>
91  Vector(const Vector<Row,AT> &x) : Matrix<General,Fixed<M>,Fixed<1>,AT>(x) {
92  }
93 
98  template<class Type, class Row, class Col>
99  explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<M>,Fixed<1>,AT>(A) {
100  }
101 
102  template <class Row>
103  inline Vector<Fixed<M>,AT>& operator=(const Vector<Row,AT> &x);
104 
115  AT& operator()(int i) {
116 
117 #ifndef FMATVEC_NO_BOUNDS_CHECK
118  assert(i>=0);
119  assert(i<M);
120 #endif
121 
122  return e(i);
123  };
124 
129  const AT& operator()(int i) const {
130 
131 #ifndef FMATVEC_NO_BOUNDS_CHECK
132  assert(i>=0);
133  assert(i<M);
134 #endif
135 
136  return e(i);
137  };
138 
139  AT& e(int i) {
140  return ele[i][0];
141  };
142 
147  const AT& e(int i) const {
148  return ele[i][0];
149  };
150 
158  inline Vector<Fixed<M>,AT>& init(const AT& a=0);
159  inline Vector<Fixed<M>,AT>& init(Init, const AT& a=0) { return init(a); }
160  inline Vector<Fixed<M>,AT>& init(Noinit, const AT& a=0) { return *this; }
161 
166  int size() const {return M;};
167 
170  void resize(int m) {
171  if(m!=M)
172  throw std::runtime_error("A fixed vector cannot be resized.");
173  }
174 
181  int inc() const {return 1;};
182 
183  template <int M1, int M2>
184  inline const Vector<Fixed<M2-M1+1>,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I) const;
185 
186  using Matrix<General,Fixed<M>,Fixed<1>,AT>::operator();
187 
192  inline operator std::vector<AT>();
193 
198  inline Vector(std::vector<AT> v);
199 
203  inline const RowVector<Fixed<M>,AT> T() const;
204 
208  template<class Type, class Row, class Col>
209  inline void set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A);
210 
211  };
212 
213  template <int M, class AT>
214  inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::init(const AT &val) {
215  for(int i=0; i<M; i++)
216  e(i) = val;
217  return *this;
218  }
219 
220  template <int M, class AT> template <int M1, int M2>
221  inline const Vector<Fixed<M2-M1+1>,AT> Vector<Fixed<M>,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I) const {
222 #ifndef FMATVEC_NO_BOUNDS_CHECK
223  assert(M2<M);
224 #endif
225  Vector<Fixed<M2-M1+1>,AT> x(NONINIT);
226 
227  for(int i=0; i<x.size(); i++)
228  x.e(i) = e(M1+i);
229 
230  return x;
231  }
232 
233  template <int M, class AT> template <class Row>
234  inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::operator=(const Vector<Row,AT> &x) {
235 
236 #ifndef FMATVEC_NO_SIZE_CHECK
237  assert(x.size() == M);
238 #endif
239 
240  deepCopy(x);
241 
242  return *this;
243  }
244 
245  template <int M, class AT>
246  inline const RowVector<Fixed<M>,AT> Vector<Fixed<M>,AT>::T() const {
247  RowVector<Fixed<M>,AT> x(NONINIT);
248  for(int i=0; i<M; i++)
249  x.e(i) = e(i);
250  return x;
251  }
252 
253  template <int M, class AT>
254  inline Vector<Fixed<M>,AT>::operator std::vector<AT>() {
255  std::vector<AT> ret(size());
256  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
257  return ret;
258  }
259 
260  template <int M, class AT>
261  inline Vector<Fixed<M>,AT>::Vector(std::vector<AT> v) : Matrix<General,Fixed<M>,Fixed<1>,AT>() {
262  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
263  }
264 
266 
267  template <int M, class AT> template<class Row>
268  inline void Vector<Fixed<M>,AT>::deepCopy(const Vector<Row,AT> &x) {
269  for(int i=0; i<M; i++)
270  e(i) = x.e(i);
271  }
272 
274 
275  template<int M, class AT> template<class Type, class Row, class Col>
276  inline void Vector<Fixed<M>,AT>::set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A) {
277  Matrix<General,Fixed<M>,Fixed<1>,AT>::set(I, Range<Var,Var>(0,0), A);
278  }
279 
280 }
281 
282 #endif
Definition: fmatvec.h:38
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Definition: matrix.h:39
void resize(int m)
Definition: fixed_vector.h:170
Definition: fmatvec.h:50
Vector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_vector.h:99
Vector(const Vector< Row, AT > &x)
Copy Constructor.
Definition: fixed_vector.h:91
Definition: fmatvec.h:47
This is an index class for creating submatrices.
Definition: range.h:35
const AT & e(int i) const
Element operator.
Definition: fixed_vector.h:147
int size() const
Size.
Definition: fixed_vector.h:166
int inc() const
Increment.
Definition: fixed_vector.h:181
Definition: matrix.h:38
Basic shape class for matrices.
Definition: types.h:100
AT & operator()(int i)
Element operator.
Definition: fixed_vector.h:115
const AT & operator()(int i) const
Element operator.
Definition: fixed_vector.h:129
Vector(const char *str)
String Constructor.
Definition: fixed_vector.h:80
This is an index class for creating submatrices.
Definition: range.h:44

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML