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* iterator;
47  typedef const AT* const_iterator;
48 
49  typedef AT value_type;
50 
52 
53  protected:
54 
55  template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
56 
58 
59  public:
60 
61 // template<class Ini=All<AT> >
62 // Vector(Ini ini=All<AT>()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
63 // template<class Ini=All<AT> >
64 // Vector(int m, Ini ini=All<AT>()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
65 
66  Vector(Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
67  Vector(Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { }
68  Vector(int m, Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
69  Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { }
70 
83  Vector(const char *str) : Matrix<General,Fixed<M>,Fixed<1>,AT>(str) {
84  }
85 
93  template<class Row>
94  Vector(const Vector<Row,AT> &x) : Matrix<General,Fixed<M>,Fixed<1>,AT>(x) {
95  }
96 
101  template<class Type, class Row, class Col>
102  explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<M>,Fixed<1>,AT>(A) {
103  }
104 
105  template <class Row>
106  inline Vector<Fixed<M>,AT>& operator=(const Vector<Row,AT> &x);
107 
118  AT& operator()(int i) {
119 
120 #ifndef FMATVEC_NO_BOUNDS_CHECK
121  assert(i>=0);
122  assert(i<M);
123 #endif
124 
125  return e(i);
126  };
127 
132  const AT& operator()(int i) const {
133 
134 #ifndef FMATVEC_NO_BOUNDS_CHECK
135  assert(i>=0);
136  assert(i<M);
137 #endif
138 
139  return e(i);
140  };
141 
142  iterator begin() { return &ele[0][0]; }
143  iterator end() { return &ele[M][0]; }
144  const_iterator begin() const { return &ele[0][0]; }
145  const_iterator end() const { return &ele[M][0]; }
146  const_iterator cbegin() const noexcept { return &ele[0][0]; }
147  const_iterator cend() const noexcept { return &ele[M][0]; }
148 
149  AT& e(int i) {
150  return ele[i][0];
151  };
152 
157  const AT& e(int i) const {
158  return ele[i][0];
159  };
160 
168  inline Vector<Fixed<M>,AT>& init(const AT& val=0);
169  inline Vector<Fixed<M>,AT>& init(Init, const AT& a=0) { return init(a); }
170  inline Vector<Fixed<M>,AT>& init(Noinit, const AT& a=0) { return *this; }
171 
176  int size() const {return M;};
177 
180  void resize(int m) {
181  if(m!=M)
182  throw std::runtime_error("A fixed vector cannot be resized.");
183  }
184 
191  int inc() const {return 1;};
192 
193  template <int M1, int M2>
194  inline const Vector<Fixed<M2-M1+1>,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I) const;
195 
196  using Matrix<General,Fixed<M>,Fixed<1>,AT>::operator();
197 
202  inline operator std::vector<AT>() const;
203 
208  inline Vector(std::vector<AT> v);
209 
213  inline const RowVector<Fixed<M>,AT> T() const;
214 
218  template<class Type, class Row, class Col>
219  inline void set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A);
220 
221  };
222 
223  template <int M, class AT>
224  inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::init(const AT &val) {
225  for(int i=0; i<M; i++)
226  e(i) = val;
227  return *this;
228  }
229 
230  template <int M, class AT> template <int M1, int M2>
231  inline const Vector<Fixed<M2-M1+1>,AT> Vector<Fixed<M>,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I) const {
232 #ifndef FMATVEC_NO_BOUNDS_CHECK
233  assert(M2<M);
234 #endif
235  Vector<Fixed<M2-M1+1>,AT> x(NONINIT);
236 
237  for(int i=0; i<x.size(); i++)
238  x.e(i) = e(M1+i);
239 
240  return x;
241  }
242 
243  template <int M, class AT> template <class Row>
244  inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::operator=(const Vector<Row,AT> &x) {
245 
246 #ifndef FMATVEC_NO_SIZE_CHECK
247  assert(x.size() == M);
248 #endif
249 
250  deepCopy(x);
251 
252  return *this;
253  }
254 
255  template <int M, class AT>
256  inline const RowVector<Fixed<M>,AT> Vector<Fixed<M>,AT>::T() const {
257  RowVector<Fixed<M>,AT> x(NONINIT);
258  for(int i=0; i<M; i++)
259  x.e(i) = e(i);
260  return x;
261  }
262 
263  template <int M, class AT>
264  inline Vector<Fixed<M>,AT>::operator std::vector<AT>() const {
265  std::vector<AT> ret(size());
266  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
267  return ret;
268  }
269 
270  template <int M, class AT>
271  inline Vector<Fixed<M>,AT>::Vector(std::vector<AT> v) : Matrix<General,Fixed<M>,Fixed<1>,AT>() {
272  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
273  }
274 
276 
277  template <int M, class AT> template<class Row>
278  inline void Vector<Fixed<M>,AT>::deepCopy(const Vector<Row,AT> &x) {
279  for(int i=0; i<M; i++)
280  e(i) = x.e(i);
281  }
282 
284 
285  template<int M, class AT> template<class Type, class Row, class Col>
286  inline void Vector<Fixed<M>,AT>::set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A) {
287  Matrix<General,Fixed<M>,Fixed<1>,AT>::set(I, Range<Var,Var>(0,0), A);
288  }
289 
290 }
291 
292 #endif
Definition: fmatvec.h:38
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Definition: matrix.h:43
void resize(int m)
Definition: fixed_vector.h:180
Definition: fmatvec.h:50
Vector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_vector.h:102
Vector(const Vector< Row, AT > &x)
Copy Constructor.
Definition: fixed_vector.h:94
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:157
int size() const
Size.
Definition: fixed_vector.h:176
int inc() const
Increment.
Definition: fixed_vector.h:191
Definition: matrix.h:42
Basic shape class for matrices.
Definition: types.h:100
AT & operator()(int i)
Element operator.
Definition: fixed_vector.h:118
const AT & operator()(int i) const
Element operator.
Definition: fixed_vector.h:132
Vector(const char *str)
String Constructor.
Definition: fixed_vector.h:83
This is an index class for creating submatrices.
Definition: range.h:44

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home